Skip to content

Commit d499c73

Browse files
committed
Add two different HostSystem impls
1 parent 1d1b123 commit d499c73

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ zoneinfo64 = { workspace = true }
9595
resb = "0.1.0"
9696

9797
[features]
98-
default = ["sys"]
98+
default = ["sys-local"]
9999
log = ["dep:log"]
100100
compiled_data = ["tzdb"]
101-
sys = ["std", "compiled_data", "dep:web-time", "dep:iana-time-zone"]
101+
sys = ["std", "compiled_data", "dep:web-time"]
102+
sys-local = ["sys", "dep:iana-time-zone"]
102103
tzdb = [
103104
"std",
104105
"timezone_provider/tzif",

src/builtins/core/now.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ mod tests {
196196
assert_eq!(duration.milliseconds(), 0);
197197
}
198198

199-
#[cfg(all(feature = "tzdb", feature = "sys", feature = "compiled_data"))]
199+
#[cfg(all(feature = "tzdb", feature = "sys-local", feature = "compiled_data"))]
200200
#[test]
201201
fn now_datetime_test() {
202202
use crate::Temporal;
@@ -205,9 +205,9 @@ mod tests {
205205

206206
let sleep = 2;
207207

208-
let before = Temporal::now().plain_date_time_iso(None).unwrap();
208+
let before = Temporal::local_now().plain_date_time_iso(None).unwrap();
209209
thread::sleep(StdDuration::from_secs(sleep));
210-
let after = Temporal::now().plain_date_time_iso(None).unwrap();
210+
let after = Temporal::local_now().plain_date_time_iso(None).unwrap();
211211

212212
let diff = after.since(&before, DifferenceSettings::default()).unwrap();
213213

src/sys.rs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,51 @@ use web_time::{SystemTime, UNIX_EPOCH};
2525
#[cfg(feature = "sys")]
2626
pub struct Temporal;
2727

28-
#[cfg(feature = "sys")]
2928
impl Temporal {
3029
/// Get a `Now` object for the default host system.
31-
pub fn now() -> Now<DefaultHostSystem> {
32-
Now::new(DefaultHostSystem)
30+
#[cfg(feature = "sys-local")]
31+
#[deprecated(
32+
since = "0.1.0",
33+
note = "`now` deprecated was not clear about the host system implementation, please use `local_now`"
34+
)]
35+
pub fn now() -> Now<LocalHostSystem> {
36+
Now::new(LocalHostSystem)
37+
}
38+
39+
/// Get a `Now` object with a [`LocalHostSystem`], which
40+
/// will use the host system's time zone as a fallback.
41+
#[cfg(feature = "sys-local")]
42+
pub fn local_now() -> Now<LocalHostSystem> {
43+
Now::new(LocalHostSystem)
44+
}
45+
46+
/// Get a `Now` object with a [`UtcHostSystem`], which
47+
/// will use a UTC time zone as a fallback.
48+
#[cfg(feature = "sys")]
49+
pub fn utc_now() -> Now<UtcHostSystem> {
50+
Now::new(UtcHostSystem)
3351
}
3452
}
3553

36-
/// A default host system implementation
54+
/// A UTC host system implementation that will return the current time
55+
/// with the a UTC time zone as fallback.
3756
///
38-
/// This implementation is backed by [`SystemTime`] and [`iana_time_zone`]
57+
/// This implementation is backed by [`std::time::SystemTime`].
3958
#[cfg(feature = "sys")]
40-
pub struct DefaultHostSystem;
59+
pub struct UtcHostSystem;
4160

4261
#[cfg(feature = "sys")]
43-
impl HostHooks for DefaultHostSystem {}
62+
impl HostHooks for UtcHostSystem {}
4463

4564
#[cfg(feature = "sys")]
46-
impl HostClock for DefaultHostSystem {
65+
impl HostClock for UtcHostSystem {
4766
fn get_host_epoch_nanoseconds(&self) -> TemporalResult<EpochNanoseconds> {
4867
get_system_nanoseconds()
4968
}
5069
}
5170

5271
#[cfg(feature = "sys")]
53-
impl HostTimeZone for DefaultHostSystem {
72+
impl HostTimeZone for UtcHostSystem {
5473
fn get_host_time_zone(
5574
&self,
5675
provider: &impl timezone_provider::provider::TimeZoneProvider,
@@ -59,7 +78,34 @@ impl HostTimeZone for DefaultHostSystem {
5978
}
6079
}
6180

62-
#[cfg(feature = "sys")]
81+
/// A local host system implementation that will return the current time
82+
/// with the system time zone as a fallback.
83+
///
84+
/// This implementation is backed by [`std::time::SystemTime`] and [`iana_time_zone`]
85+
#[cfg(feature = "sys-local")]
86+
pub struct LocalHostSystem;
87+
88+
#[cfg(feature = "sys-local")]
89+
impl HostHooks for LocalHostSystem {}
90+
91+
#[cfg(feature = "sys-local")]
92+
impl HostClock for LocalHostSystem {
93+
fn get_host_epoch_nanoseconds(&self) -> TemporalResult<EpochNanoseconds> {
94+
get_system_nanoseconds()
95+
}
96+
}
97+
98+
#[cfg(feature = "sys-local")]
99+
impl HostTimeZone for LocalHostSystem {
100+
fn get_host_time_zone(
101+
&self,
102+
provider: &impl timezone_provider::provider::TimeZoneProvider,
103+
) -> TemporalResult<TimeZone> {
104+
get_system_timezone(provider)
105+
}
106+
}
107+
108+
#[cfg(feature = "sys-local")]
63109
#[inline]
64110
pub(crate) fn get_system_timezone(provider: &impl TimeZoneProvider) -> TemporalResult<TimeZone> {
65111
iana_time_zone::get_timezone()

0 commit comments

Comments
 (0)