Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions components/datetime/src/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,7 @@ impl<Tz: chrono::TimeZone> GetField<Option<UtcOffset>> for chrono::DateTime<Tz>
impl<Tz: chrono::TimeZone> GetField<ZoneNameTimestamp> for chrono::DateTime<Tz> {
#[inline]
fn get_field(&self) -> ZoneNameTimestamp {
ZoneNameTimestamp::from_zoned_date_time_iso(
ZonedDateTime::from_epoch_milliseconds_and_utc_offset(
self.timestamp() * 1000,
UtcOffset::zero(),
),
)
ZoneNameTimestamp::from_epoch_seconds(self.timestamp())
}
}

Expand Down
26 changes: 11 additions & 15 deletions components/datetime/src/fieldsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ macro_rules! impl_zone_marker {
/// // Time zone info for America/Chicago in the summer
/// let zone = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-5 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 8, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1661724000));
///
/// assert_writeable_eq!(
/// fmt.format(&zone),
Expand Down Expand Up @@ -1338,7 +1338,7 @@ pub mod zone {
/// // Time zone info for Europe/Istanbul in the winter
/// let zone = TimeZone::from_iana_id("Europe/Istanbul")
/// .with_offset(UtcOffset::try_from_seconds(3 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 1, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1643410800));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand All @@ -1363,7 +1363,7 @@ pub mod zone {
/// // Time zone info for America/Chicago with a wrong offset
/// let wrong_offset = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-4 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 8, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1661724000));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand Down Expand Up @@ -1434,7 +1434,7 @@ pub mod zone {
/// // Time zone info for Asia/Tokyo
/// let zone = TimeZone::from_iana_id("Asia/Tokyo")
/// .with_offset(UtcOffset::try_from_seconds(9 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 1, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1643410800));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand All @@ -1460,7 +1460,7 @@ pub mod zone {
/// // Time zone info for America/Chicago with a wrong offset
/// let wrong_offset = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-4 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 8, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1661724000));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand Down Expand Up @@ -1528,9 +1528,7 @@ pub mod zone {
/// let utc_offset = UtcOffset::try_from_seconds(-6 * 3600).unwrap();
/// let time_zone_basic = TimeZone::from_iana_id("America/Chicago").with_offset(Some(utc_offset));
///
/// let date = Date::try_new_iso(2024, 10, 18).unwrap();
/// let time = Time::start_of_day();
/// let time_zone_at_time = time_zone_basic.at_date_time_iso(DateTime{ date, time });
/// let time_zone_at_time = time_zone_basic.with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1729202400));
///
/// let formatter = NoCalendarFormatter::try_new(
/// locale!("en-US").into(),
Expand Down Expand Up @@ -1575,9 +1573,7 @@ pub mod zone {
/// let utc_offset = UtcOffset::try_from_seconds(-6 * 3600).unwrap();
/// let time_zone_basic = TimeZone::from_iana_id("America/Chicago").with_offset(Some(utc_offset));
///
/// let date = Date::try_new_iso(2024, 10, 18).unwrap();
/// let time = Time::start_of_day();
/// let time_zone_at_time = time_zone_basic.at_date_time_iso(DateTime{ date, time });
/// let time_zone_at_time = time_zone_basic.with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1729202400));
///
/// let formatter = NoCalendarFormatter::try_new(
/// locale!("en-US").into(),
Expand Down Expand Up @@ -1621,7 +1617,7 @@ pub mod zone {
/// // Time zone info for Europe/Istanbul in the winter
/// let zone = TimeZone::from_iana_id("Europe/Istanbul")
/// .with_offset(UtcOffset::try_from_seconds(3 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 1, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1643410800));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand All @@ -1646,7 +1642,7 @@ pub mod zone {
/// // Time zone info for America/Chicago with a wrong offset
/// let wrong_offset = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-4 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 8, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1661724000));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand Down Expand Up @@ -1718,7 +1714,7 @@ pub mod zone {
/// // Time zone info for Asia/Tokyo
/// let zone = TimeZone::from_iana_id("Asia/Tokyo")
/// .with_offset(UtcOffset::try_from_seconds(9 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 1, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1643410800));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand All @@ -1744,7 +1740,7 @@ pub mod zone {
/// // Time zone info for America/Chicago with a wrong offset
/// let wrong_offset = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-4 * 3600).ok())
/// .at_date_time_iso(DateTime{ date: Date::try_new_iso(2022, 8, 29).unwrap(), time: Time::start_of_day() });
/// .with_zone_name_timestamp(icu::time::zone::ZoneNameTimestamp::from_epoch_seconds(1661724000));
///
/// let fmt = NoCalendarFormatter::try_new(
/// locale!("en").into(),
Expand Down
11 changes: 3 additions & 8 deletions components/datetime/src/jiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use icu_calendar::{AnyCalendar, Date, Gregorian};
#[cfg(feature = "compiled_data")]
use icu_time::zone::models::AtTime;
use icu_time::zone::{UtcOffset, ZoneNameTimestamp};
use icu_time::{DateTime, Hour, Minute, Nanosecond, Second, Time, ZonedDateTime};
use icu_time::{DateTime, Hour, Minute, Nanosecond, Second, Time};
#[cfg(feature = "compiled_data")]
use icu_time::{TimeZone, TimeZoneInfo};
use icu_time::{TimeZone, TimeZoneInfo, ZonedDateTime};

impl UnstableSealed for jiff::civil::Time {}
impl InFixedCalendar<()> for jiff::civil::Time {}
Expand Down Expand Up @@ -273,12 +273,7 @@ impl GetField<Option<UtcOffset>> for jiff::Zoned {
impl GetField<ZoneNameTimestamp> for jiff::Zoned {
#[inline]
fn get_field(&self) -> ZoneNameTimestamp {
ZoneNameTimestamp::from_zoned_date_time_iso(
ZonedDateTime::from_epoch_milliseconds_and_utc_offset(
self.timestamp().as_millisecond(),
UtcOffset::zero(),
),
)
ZoneNameTimestamp::from_epoch_seconds(self.timestamp().as_second())
}
}

Expand Down
38 changes: 16 additions & 22 deletions components/datetime/src/provider/time_zones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,6 @@ pub(crate) mod legacy {

#[test]
fn test_metazone_timezone_compat() {
use icu_time::ZonedDateTime;

let converted = metazone_timezone_compat(
&icu_provider_blob::BlobDataProvider::try_new_from_static_blob(
// icu4x-datagen --markers TimezoneMetazonePeriodsV1 --format blob
Expand All @@ -398,26 +396,22 @@ pub(crate) mod legacy {
.payload;

let tz = TimeZone::from_iana_id("Antarctica/Casey");
for timestamp in [
"1970-01-01 00:00Z",
"2009-10-17 18:00Z",
"2010-03-04 15:00Z",
"2011-10-27 18:00Z",
"2012-02-21 17:00Z",
"2016-10-21 16:00Z",
"2018-03-10 17:00Z",
"2018-10-06 20:00Z",
"2019-03-16 16:00Z",
"2019-10-03 19:00Z",
"2020-03-07 16:00Z",
"2021-03-13 13:00Z",
"2022-03-12 13:00Z",
"2023-03-08 16:00Z",
for t in [
ZoneNameTimestamp::from_epoch_seconds(0),
ZoneNameTimestamp::from_epoch_seconds(1255802400),
ZoneNameTimestamp::from_epoch_seconds(1267714800),
ZoneNameTimestamp::from_epoch_seconds(1319738400),
ZoneNameTimestamp::from_epoch_seconds(1329843600),
ZoneNameTimestamp::from_epoch_seconds(1477065600),
ZoneNameTimestamp::from_epoch_seconds(1520701200),
ZoneNameTimestamp::from_epoch_seconds(1538856000),
ZoneNameTimestamp::from_epoch_seconds(1552752000),
ZoneNameTimestamp::from_epoch_seconds(1570129200),
ZoneNameTimestamp::from_epoch_seconds(1583596800),
ZoneNameTimestamp::from_epoch_seconds(1615640400),
ZoneNameTimestamp::from_epoch_seconds(1647090000),
ZoneNameTimestamp::from_epoch_seconds(1678291200),
] {
let t = ZoneNameTimestamp::from_zoned_date_time_iso(
ZonedDateTime::try_offset_only_from_str(timestamp, icu_calendar::Iso).unwrap(),
);

assert_eq!(
converted
.get()
Expand All @@ -435,7 +429,7 @@ pub(crate) mod legacy {
.unwrap()
.1
.map(|mz| mz.id.get()),
"{timestamp:?}",
"{t:?}",
);
}
}
Expand Down
9 changes: 4 additions & 5 deletions components/time/src/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use icu_calendar::Gregorian;

use crate::zone::models::{AtTime, Base};
use crate::zone::UtcOffset;
use crate::zone::{UtcOffset, ZoneNameTimestamp};
use crate::{DateTime, Hour, Minute, Nanosecond, Second, Time};
use crate::{TimeZone, TimeZoneInfo, ZonedDateTime};

Expand Down Expand Up @@ -64,10 +64,9 @@ where
ZonedDateTime {
date,
time,
zone: zone.at_date_time_iso(DateTime {
date: date.to_calendar(icu_calendar::Iso),
time,
}),
zone: zone.with_zone_name_timestamp(ZoneNameTimestamp::from_epoch_seconds(
chrono.timestamp(),
)),
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions components/time/src/jiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use icu_calendar::Gregorian;
#[cfg(feature = "compiled_data")]
use crate::zone::models::{AtTime, Base};
use crate::zone::UtcOffset;
#[cfg(feature = "compiled_data")]
use crate::zone::ZoneNameTimestamp;
use crate::{DateTime, Hour, Minute, Nanosecond, Second, Time};
#[cfg(feature = "compiled_data")]
use crate::{TimeZone, TimeZoneInfo, ZonedDateTime};
Expand Down Expand Up @@ -69,10 +71,9 @@ impl From<&jiff::Zoned> for ZonedDateTime<Gregorian, TimeZoneInfo<AtTime>> {
ZonedDateTime {
date,
time,
zone: zone.at_date_time_iso(DateTime {
date: date.to_calendar(icu_calendar::Iso),
time,
}),
zone: zone.with_zone_name_timestamp(ZoneNameTimestamp::from_epoch_seconds(
jiff.timestamp().as_second(),
)),
}
}
}
24 changes: 11 additions & 13 deletions components/time/src/zone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ impl<'a> zerovec::maps::ZeroMapKV<'a> for TimeZone {
/// use icu::time::Time;
/// use icu::time::TimeZone;
/// use icu::time::zone::UtcOffset;
/// use icu::time::zone::ZoneNameTimestamp;
///
/// // Parse the IANA ID
/// let id = TimeZone::from_iana_id("America/Chicago");
Expand All @@ -278,7 +279,12 @@ impl<'a> zerovec::maps::ZeroMapKV<'a> for TimeZone {
/// // Create a TimeZoneInfo<Base> by associating the ID with an offset
/// let time_zone = id.with_offset(UtcOffset::try_from_seconds(-6 * 3600).ok());
///
/// // Extend to a TimeZoneInfo<AtTime> by adding a local time
/// // Extend to a `TimeZoneInfo<AtTime>` by adding a timestamp ...
/// let time_zone_at_time = time_zone.with_zone_name_timestamp(
/// ZoneNameTimestamp::from_epoch_seconds(1701493200)
/// );
///
/// // ... or by adding a local time
/// let time_zone_at_time = time_zone.at_date_time_iso(DateTime {
/// date: Date::try_new_iso(2023, 12, 2).unwrap(),
/// time: Time::start_of_day(),
Expand Down Expand Up @@ -494,6 +500,7 @@ impl TimeZoneInfo<models::AtTime> {
/// ```
/// use icu::calendar::Date;
/// use icu::time::zone::TimeZoneVariant;
/// use icu::time::zone::ZoneNameTimestamp;
/// use icu::time::zone::VariantOffsetsCalculator;
/// use icu::time::DateTime;
/// use icu::time::Time;
Expand All @@ -503,32 +510,23 @@ impl TimeZoneInfo<models::AtTime> {
/// // Chicago at UTC-6
/// let info = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-6 * 3600).ok())
/// .at_date_time_iso(DateTime {
/// date: Date::try_new_iso(2023, 12, 2).unwrap(),
/// time: Time::start_of_day(),
/// })
/// .with_zone_name_timestamp(ZoneNameTimestamp::from_epoch_seconds(1701493200))
/// .infer_variant(VariantOffsetsCalculator::new());
///
/// assert_eq!(info.variant(), TimeZoneVariant::Standard);
///
/// // Chicago at at UTC-5
/// let info = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-5 * 3600).ok())
/// .at_date_time_iso(DateTime {
/// date: Date::try_new_iso(2023, 6, 2).unwrap(),
/// time: Time::start_of_day(),
/// })
/// .with_zone_name_timestamp(ZoneNameTimestamp::from_epoch_seconds(1685678400))
/// .infer_variant(VariantOffsetsCalculator::new());
///
/// assert_eq!(info.variant(), TimeZoneVariant::Daylight);
///
/// // Chicago at UTC-7
/// let info = TimeZone::from_iana_id("America/Chicago")
/// .with_offset(UtcOffset::try_from_seconds(-7 * 3600).ok())
/// .at_date_time_iso(DateTime {
/// date: Date::try_new_iso(2023, 12, 2).unwrap(),
/// time: Time::start_of_day(),
/// })
/// .with_zone_name_timestamp(ZoneNameTimestamp::from_epoch_seconds(1701493200))
/// .infer_variant(VariantOffsetsCalculator::new());
///
/// // Whatever it is, it's not Chicago
Expand Down
37 changes: 16 additions & 21 deletions components/time/src/zone/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ pub use crate::provider::VariantOffsets;
#[test]
#[allow(deprecated)]
pub fn test_legacy_offsets_data() {
use crate::ZonedDateTime;
use icu_locale_core::subtags::subtag;
use icu_provider_blob::BlobDataProvider;

Expand All @@ -417,32 +416,28 @@ pub fn test_legacy_offsets_data() {

let tz = TimeZone(subtag!("aqcas"));

for timestamp in [
"1970-01-01 00:00Z",
"2009-10-17 18:00Z",
"2010-03-04 15:00Z",
"2011-10-27 18:00Z",
"2012-02-21 17:00Z",
"2016-10-21 16:00Z",
"2018-03-10 17:00Z",
"2018-10-06 20:00Z",
"2019-03-16 16:00Z",
"2019-10-03 19:00Z",
"2020-03-07 16:00Z",
"2021-03-13 13:00Z",
"2022-03-12 13:00Z",
"2023-03-08 16:00Z",
for t in [
ZoneNameTimestamp::from_epoch_seconds(0),
ZoneNameTimestamp::from_epoch_seconds(1255802400),
ZoneNameTimestamp::from_epoch_seconds(1267714800),
ZoneNameTimestamp::from_epoch_seconds(1319738400),
ZoneNameTimestamp::from_epoch_seconds(1329843600),
ZoneNameTimestamp::from_epoch_seconds(1477065600),
ZoneNameTimestamp::from_epoch_seconds(1520701200),
ZoneNameTimestamp::from_epoch_seconds(1538856000),
ZoneNameTimestamp::from_epoch_seconds(1552752000),
ZoneNameTimestamp::from_epoch_seconds(1570129200),
ZoneNameTimestamp::from_epoch_seconds(1583596800),
ZoneNameTimestamp::from_epoch_seconds(1615640400),
ZoneNameTimestamp::from_epoch_seconds(1647090000),
ZoneNameTimestamp::from_epoch_seconds(1678291200),
] {
let t = ZoneNameTimestamp::from_zoned_date_time_iso(
ZonedDateTime::try_offset_only_from_str(timestamp, icu_calendar::Iso).unwrap(),
);

assert_eq!(
c.as_borrowed()
.compute_offsets_from_time_zone_and_name_timestamp(tz, t),
VariantOffsetsCalculator::new()
.compute_offsets_from_time_zone_and_name_timestamp(tz, t),
"{timestamp:?}",
"{t:?}",
);
}
}
Loading