Skip to content

Commit dfd58db

Browse files
committed
reverse
1 parent 36bc1c4 commit dfd58db

File tree

4 files changed

+185
-59
lines changed

4 files changed

+185
-59
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

utils/zoneinfo64/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ icu_time = { workspace = true }
2222
potential_utf = { workspace = true }
2323
resb = { workspace = true }
2424
serde = { workspace = true, features = ["derive"] }
25+
calendrical_calculations = { workspace = true }
2526

2627
chrono = { version = "0.4", optional = true }
2728

2829
[dev-dependencies]
2930
chrono-tz = { git = "https://github.com/robertbastian/chrono-tz", branch = "fix" }
3031

3132
[features]
32-
chrono = ["dep:chrono"]
33+
chrono = ["dep:chrono"]

utils/zoneinfo64/src/chrono_impls.rs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
// called LICENSE at the top level of the ICU4X source tree
33
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
44

5-
use crate::{Transition, Zone};
5+
use crate::{Offset, PossibleOffset, Zone};
6+
use chrono::{
7+
Datelike, FixedOffset, MappedLocalTime, NaiveDate, NaiveDateTime, TimeZone, Timelike,
8+
};
69
use icu_time::zone::UtcOffset;
710

811
#[derive(Clone, Copy, Debug)]
9-
pub struct ChronoOffset<'a>(Transition, Zone<'a>);
12+
pub struct ChronoOffset<'a>(Offset, Zone<'a>);
1013

1114
impl core::ops::Deref for ChronoOffset<'_> {
1215
type Target = UtcOffset;
@@ -22,43 +25,61 @@ impl ChronoOffset<'_> {
2225
}
2326
}
2427

25-
#[cfg(feature = "chrono")]
2628
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()
2931
}
3032
}
3133

32-
#[cfg(feature = "chrono")]
33-
impl<'a> chrono::TimeZone for Zone<'a> {
34+
impl<'a> TimeZone for Zone<'a> {
3435
type Offset = ChronoOffset<'a>;
3536

3637
fn from_offset(offset: &Self::Offset) -> Self {
3738
offset.1
3839
}
3940

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+
}
4556
}
4657

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+
}
5273
}
5374

54-
fn offset_from_utc_date(&self, utc: &chrono::NaiveDate) -> Self::Offset {
75+
fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset {
5576
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()),
5778
*self,
5879
)
5980
}
6081

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)
6384
}
6485
}

0 commit comments

Comments
 (0)