Skip to content

Commit ffb3f4f

Browse files
authored
from_epoch_milliseconds_and_utc_offset and follow-ups (#6515)
Follow-ups to #6513
1 parent 2a0b6f3 commit ffb3f4f

14 files changed

+170
-9
lines changed

components/time/src/zone/zone_name_timestamp.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ use crate::{DateTime, Hour, Minute, Nanosecond, Second, Time};
2121
/// Internal intermediate type for interfacing with [`ZoneNameTimestamp`].
2222
#[derive(Debug, Copy, Clone)]
2323
struct ZoneNameTimestampParts {
24+
/// Invariant: between MIN_QUARTER_HOURS_U32 and MAX_QUARTER_HOURS_U32 (inclusive).
25+
/// This range covers almost 500 years.
2426
quarter_hours_since_local_unix_epoch: u32,
27+
/// Currently the metadata is unused. It is reserved for future use, such as:
28+
/// - A time zone UTC offset
29+
/// - Extra bits for the epoch quarter-hours
30+
/// - Bitmask to use the epoch quarter hour bits more efficiently
2531
metadata: u8,
2632
}
2733

2834
impl ZoneNameTimestampParts {
2935
/// Recovers the DateTime from these parts.
3036
fn date_time(self) -> DateTime<Iso> {
3137
let qh = self.quarter_hours_since_local_unix_epoch;
32-
// Note: the `as` casts below are trivially save because the rem_euclid is in range
38+
// Note: the `as` casts below are trivially safe because the remainder is in range
3339
let (days, remainder) = (
3440
(qh / QUARTER_HOURS_IN_DAY_U32) as i64,
3541
(qh % QUARTER_HOURS_IN_DAY_U32) as u8,
@@ -71,7 +77,6 @@ impl ZoneNameTimestampParts {
7177
0
7278
}
7379
};
74-
let metadata = if metadata > 0x7F { 0 } else { metadata };
7580
ZoneNameTimestampParts {
7681
quarter_hours_since_local_unix_epoch: qh_u32,
7782
metadata,

ffi/capi/bindings/c/ZonedIsoDateTime.h

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/cpp/icu4x/TimeZoneInfo.d.hpp

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/cpp/icu4x/ZonedIsoDateTime.d.hpp

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/cpp/icu4x/ZonedIsoDateTime.hpp

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/dart/TimeZoneInfo.g.dart

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/dart/ZonedIsoDateTime.g.dart

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/TimeZoneInfo.d.ts

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/TimeZoneInfo.mjs

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/ZonedIsoDateTime.d.ts

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/js/ZonedIsoDateTime.mjs

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/src/timezone.rs

+25
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,32 @@ pub mod ffi {
112112
Box::new(TimeZone(self.id))
113113
}
114114

115+
/// Sets the datetime at which to interpret the time zone
116+
/// for display name lookup.
117+
///
118+
/// Notes:
119+
///
120+
/// - If not set, the formatting datetime is used if possible.
121+
/// - The constraints are the same as with `ZoneNameTimestamp` in Rust.
122+
/// - Set to year 1000 or 9999 for a reference far in the past or future.
115123
#[diplomat::rust_link(icu::time::TimeZoneInfo::at_date_time_iso, FnInStruct)]
124+
#[diplomat::rust_link(icu::time::zone::ZoneNameTimestamp, Struct, compact)]
116125
#[diplomat::rust_link(
117126
icu::time::TimeZoneInfo::with_zone_name_timestamp,
118127
FnInStruct,
119128
hidden
120129
)]
130+
#[diplomat::rust_link(
131+
icu::time::zone::ZoneNameTimestamp::from_date_time_iso,
132+
FnInStruct,
133+
hidden
134+
)]
135+
#[diplomat::rust_link(
136+
icu::time::zone::ZoneNameTimestamp::far_in_future,
137+
FnInStruct,
138+
hidden
139+
)] // documented
140+
#[diplomat::rust_link(icu::time::zone::ZoneNameTimestamp::far_in_past, FnInStruct, hidden)] // documented
121141
pub fn at_date_time_iso(&self, date: &IsoDate, time: &Time) -> Box<Self> {
122142
Box::new(Self {
123143
zone_name_timestamp: Some(icu_time::zone::ZoneNameTimestamp::from_date_time_iso(
@@ -131,6 +151,11 @@ pub mod ffi {
131151
}
132152

133153
#[diplomat::rust_link(icu::time::TimeZoneInfo::zone_name_timestamp, FnInStruct)]
154+
#[diplomat::rust_link(
155+
icu::time::zone::ZoneNameTimestamp::to_date_time_iso,
156+
FnInStruct,
157+
hidden
158+
)]
134159
pub fn zone_name_date_time(&self) -> Option<IsoDateTime> {
135160
let datetime = self.zone_name_timestamp?.to_date_time_iso();
136161
Some(IsoDateTime {

ffi/capi/src/zoned_datetime.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod ffi {
1515
use crate::unstable::iana_parser::ffi::IanaParser;
1616
use crate::unstable::time::ffi::Time;
1717
use crate::unstable::timezone::ffi::TimeZoneInfo;
18-
use crate::unstable::variant_offset::ffi::VariantOffsetsCalculator;
18+
use crate::unstable::variant_offset::ffi::{UtcOffset, VariantOffsetsCalculator};
1919

2020
/// An ICU4X ZonedDateTime object capable of containing a ISO-8601 date, time, and zone.
2121
#[diplomat::rust_link(icu::time::ZonedDateTime, Struct)]
@@ -49,6 +49,29 @@ pub mod ffi {
4949
zone: Box::new(TimeZoneInfo::from(zone)),
5050
})
5151
}
52+
53+
/// Creates a new [`ZonedDateTime`] from milliseconds since epoch (timestamp) and a UTC offset.
54+
///
55+
/// Note: [`ZonedDateTime`]s created with this constructor can only be formatted using localized offset zone styles.
56+
#[diplomat::rust_link(
57+
icu::time::ZonedDateTime::from_epoch_milliseconds_and_utc_offset,
58+
FnInStruct
59+
)]
60+
#[diplomat::attr(all(supports = named_constructors, supports = fallible_constructors), named_constructor = "from_epoch_milliseconds_and_utc_offset")]
61+
pub fn from_epoch_milliseconds_and_utc_offset(
62+
epoch_milliseconds: i64,
63+
utc_offset: &UtcOffset,
64+
) -> ZonedIsoDateTime {
65+
let zdt = icu_time::ZonedDateTime::from_epoch_milliseconds_and_utc_offset(
66+
epoch_milliseconds,
67+
utc_offset.0,
68+
);
69+
ZonedIsoDateTime {
70+
date: Box::new(IsoDate(zdt.date)),
71+
time: Box::new(Time(zdt.time)),
72+
zone: Box::new(TimeZoneInfo::from(utc_offset.0)),
73+
}
74+
}
5275
}
5376

5477
/// An ICU4X DateTime object capable of containing a date, time, and zone for any calendar.

ffi/capi/tests/missing_apis.txt

-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515

1616

1717
fixed_decimal::Decimal::new#FnInTypedef
18-
icu::time::ZonedDateTime::from_epoch_milliseconds_and_utc_offset#FnInStruct
19-
icu::time::zone::ZoneNameTimestamp#Struct
20-
icu::time::zone::ZoneNameTimestamp::far_in_future#FnInStruct
21-
icu::time::zone::ZoneNameTimestamp::far_in_past#FnInStruct
22-
icu::time::zone::ZoneNameTimestamp::from_date_time_iso#FnInStruct
23-
icu::time::zone::ZoneNameTimestamp::to_date_time_iso#FnInStruct
2418
icu_provider_adapters::fork::ForkByMarkerProvider::inner#FnInTypedef
2519
icu_provider_adapters::fork::ForkByMarkerProvider::inner_mut#FnInTypedef
2620
icu_provider_adapters::fork::ForkByMarkerProvider::into_inner#FnInTypedef

0 commit comments

Comments
 (0)