2
2
// called LICENSE at the top level of the ICU4X source tree
3
3
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
4
5
- use crate :: { Transition , Zone } ;
5
+ use crate :: { Offset , PossibleOffset , Zone } ;
6
+ use chrono:: {
7
+ Datelike , FixedOffset , MappedLocalTime , NaiveDate , NaiveDateTime , TimeZone , Timelike ,
8
+ } ;
6
9
use icu_time:: zone:: UtcOffset ;
7
10
8
11
#[ derive( Clone , Copy , Debug ) ]
9
- pub struct ChronoOffset < ' a > ( Transition , Zone < ' a > ) ;
12
+ pub struct ChronoOffset < ' a > ( Offset , Zone < ' a > ) ;
10
13
11
14
impl core:: ops:: Deref for ChronoOffset < ' _ > {
12
15
type Target = UtcOffset ;
@@ -22,43 +25,61 @@ impl ChronoOffset<'_> {
22
25
}
23
26
}
24
27
25
- #[ cfg( feature = "chrono" ) ]
26
28
impl < ' a > chrono:: Offset for ChronoOffset < ' a > {
27
- fn fix ( & self ) -> chrono :: FixedOffset {
28
- chrono :: FixedOffset :: east_opt ( self . 0 . offset . to_seconds ( ) ) . unwrap ( )
29
+ fn fix ( & self ) -> FixedOffset {
30
+ FixedOffset :: east_opt ( self . 0 . offset . to_seconds ( ) ) . unwrap ( )
29
31
}
30
32
}
31
33
32
- #[ cfg( feature = "chrono" ) ]
33
- impl < ' a > chrono:: TimeZone for Zone < ' a > {
34
+ impl < ' a > TimeZone for Zone < ' a > {
34
35
type Offset = ChronoOffset < ' a > ;
35
36
36
37
fn from_offset ( offset : & Self :: Offset ) -> Self {
37
38
offset. 1
38
39
}
39
40
40
- fn offset_from_local_date (
41
- & self ,
42
- _local : & chrono:: NaiveDate ,
43
- ) -> chrono:: MappedLocalTime < Self :: Offset > {
44
- todo ! ( )
41
+ fn offset_from_local_date ( & self , local : & NaiveDate ) -> MappedLocalTime < Self :: Offset > {
42
+ match self . for_date_time (
43
+ local. year ( ) ,
44
+ local. month ( ) as u8 ,
45
+ local. day ( ) as u8 ,
46
+ 0 ,
47
+ 0 ,
48
+ 0 ,
49
+ ) {
50
+ PossibleOffset :: None => chrono:: MappedLocalTime :: None ,
51
+ PossibleOffset :: Single ( o) => chrono:: MappedLocalTime :: Single ( ChronoOffset ( o, * self ) ) ,
52
+ PossibleOffset :: Ambiguous ( a, b) => {
53
+ MappedLocalTime :: Ambiguous ( ChronoOffset ( a, * self ) , ChronoOffset ( b, * self ) )
54
+ }
55
+ }
45
56
}
46
57
47
- fn offset_from_local_datetime (
48
- & self ,
49
- _local : & chrono:: NaiveDateTime ,
50
- ) -> chrono:: MappedLocalTime < Self :: Offset > {
51
- todo ! ( )
58
+ fn offset_from_local_datetime ( & self , local : & NaiveDateTime ) -> MappedLocalTime < Self :: Offset > {
59
+ match self . for_date_time (
60
+ local. year ( ) ,
61
+ local. month ( ) as u8 ,
62
+ local. day ( ) as u8 ,
63
+ local. hour ( ) as u8 ,
64
+ local. minute ( ) as u8 ,
65
+ local. second ( ) as u8 ,
66
+ ) {
67
+ PossibleOffset :: None => chrono:: MappedLocalTime :: None ,
68
+ PossibleOffset :: Single ( o) => chrono:: MappedLocalTime :: Single ( ChronoOffset ( o, * self ) ) ,
69
+ PossibleOffset :: Ambiguous ( a, b) => {
70
+ MappedLocalTime :: Ambiguous ( ChronoOffset ( a, * self ) , ChronoOffset ( b, * self ) )
71
+ }
72
+ }
52
73
}
53
74
54
- fn offset_from_utc_date ( & self , utc : & chrono :: NaiveDate ) -> Self :: Offset {
75
+ fn offset_from_utc_date ( & self , utc : & NaiveDate ) -> Self :: Offset {
55
76
ChronoOffset (
56
- self . previous_transition ( utc. and_time ( chrono:: NaiveTime :: MIN ) . and_utc ( ) . timestamp ( ) ) ,
77
+ self . for_timestamp ( utc. and_time ( chrono:: NaiveTime :: MIN ) . and_utc ( ) . timestamp ( ) ) ,
57
78
* self ,
58
79
)
59
80
}
60
81
61
- fn offset_from_utc_datetime ( & self , utc : & chrono :: NaiveDateTime ) -> Self :: Offset {
62
- ChronoOffset ( self . previous_transition ( utc. and_utc ( ) . timestamp ( ) ) , * self )
82
+ fn offset_from_utc_datetime ( & self , utc : & NaiveDateTime ) -> Self :: Offset {
83
+ ChronoOffset ( self . for_timestamp ( utc. and_utc ( ) . timestamp ( ) ) , * self )
63
84
}
64
85
}
0 commit comments