From 9b1a9b14a7298cdec6f34d2b6e42f715389dfd50 Mon Sep 17 00:00:00 2001 From: Kevin Ness Date: Wed, 24 Sep 2025 23:19:11 -0500 Subject: [PATCH 1/4] Add two different HostSystem impls --- Cargo.toml | 5 +-- src/builtins/core/now.rs | 6 ++-- src/sys.rs | 66 ++++++++++++++++++++++++++++++++++------ 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f12b83f4d..5121b2449 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,10 +95,11 @@ zoneinfo64 = { workspace = true } resb = "0.1.0" [features] -default = ["sys"] +default = ["sys-local"] log = ["dep:log"] compiled_data = ["tzdb"] -sys = ["std", "compiled_data", "dep:web-time", "dep:iana-time-zone"] +sys = ["std", "compiled_data", "dep:web-time"] +sys-local = ["sys", "dep:iana-time-zone"] tzdb = [ "std", "timezone_provider/tzif", diff --git a/src/builtins/core/now.rs b/src/builtins/core/now.rs index bc640f50f..2801acbd7 100644 --- a/src/builtins/core/now.rs +++ b/src/builtins/core/now.rs @@ -196,7 +196,7 @@ mod tests { assert_eq!(duration.milliseconds(), 0); } - #[cfg(all(feature = "tzdb", feature = "sys", feature = "compiled_data"))] + #[cfg(all(feature = "tzdb", feature = "sys-local", feature = "compiled_data"))] #[test] fn now_datetime_test() { use crate::Temporal; @@ -205,9 +205,9 @@ mod tests { let sleep = 2; - let before = Temporal::now().plain_date_time_iso(None).unwrap(); + let before = Temporal::local_now().plain_date_time_iso(None).unwrap(); thread::sleep(StdDuration::from_secs(sleep)); - let after = Temporal::now().plain_date_time_iso(None).unwrap(); + let after = Temporal::local_now().plain_date_time_iso(None).unwrap(); let diff = after.since(&before, DifferenceSettings::default()).unwrap(); diff --git a/src/sys.rs b/src/sys.rs index a530dc883..cbe3ba557 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -25,32 +25,51 @@ use web_time::{SystemTime, UNIX_EPOCH}; #[cfg(feature = "sys")] pub struct Temporal; -#[cfg(feature = "sys")] impl Temporal { /// Get a `Now` object for the default host system. - pub fn now() -> Now { - Now::new(DefaultHostSystem) + #[cfg(feature = "sys-local")] + #[deprecated( + since = "0.1.0", + note = "`now` deprecated was not clear about the host system implementation, please use `local_now`" + )] + pub fn now() -> Now { + Now::new(LocalHostSystem) + } + + /// Get a `Now` object with a [`LocalHostSystem`], which + /// will use the host system's time zone as a fallback. + #[cfg(feature = "sys-local")] + pub fn local_now() -> Now { + Now::new(LocalHostSystem) + } + + /// Get a `Now` object with a [`UtcHostSystem`], which + /// will use a UTC time zone as a fallback. + #[cfg(feature = "sys")] + pub fn utc_now() -> Now { + Now::new(UtcHostSystem) } } -/// A default host system implementation +/// A UTC host system implementation that will return the current time +/// with the a UTC time zone as fallback. /// -/// This implementation is backed by [`SystemTime`] and [`iana_time_zone`] +/// This implementation is backed by [`std::time::SystemTime`]. #[cfg(feature = "sys")] -pub struct DefaultHostSystem; +pub struct UtcHostSystem; #[cfg(feature = "sys")] -impl HostHooks for DefaultHostSystem {} +impl HostHooks for UtcHostSystem {} #[cfg(feature = "sys")] -impl HostClock for DefaultHostSystem { +impl HostClock for UtcHostSystem { fn get_host_epoch_nanoseconds(&self) -> TemporalResult { get_system_nanoseconds() } } #[cfg(feature = "sys")] -impl HostTimeZone for DefaultHostSystem { +impl HostTimeZone for UtcHostSystem { fn get_host_time_zone( &self, provider: &impl timezone_provider::provider::TimeZoneProvider, @@ -59,7 +78,34 @@ impl HostTimeZone for DefaultHostSystem { } } -#[cfg(feature = "sys")] +/// A local host system implementation that will return the current time +/// with the system time zone as a fallback. +/// +/// This implementation is backed by [`std::time::SystemTime`] and [`iana_time_zone`] +#[cfg(feature = "sys-local")] +pub struct LocalHostSystem; + +#[cfg(feature = "sys-local")] +impl HostHooks for LocalHostSystem {} + +#[cfg(feature = "sys-local")] +impl HostClock for LocalHostSystem { + fn get_host_epoch_nanoseconds(&self) -> TemporalResult { + get_system_nanoseconds() + } +} + +#[cfg(feature = "sys-local")] +impl HostTimeZone for LocalHostSystem { + fn get_host_time_zone( + &self, + provider: &impl timezone_provider::provider::TimeZoneProvider, + ) -> TemporalResult { + get_system_timezone(provider) + } +} + +#[cfg(feature = "sys-local")] #[inline] pub(crate) fn get_system_timezone(provider: &impl TimeZoneProvider) -> TemporalResult { iana_time_zone::get_timezone() From d9a13ecd223d43ed57e375d799015ad5ff159827 Mon Sep 17 00:00:00 2001 From: Kevin Ness Date: Thu, 25 Sep 2025 19:28:57 -0500 Subject: [PATCH 2/4] Some general cleanup of the current rework --- src/lib.rs | 4 ++-- src/sys.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 49f955cd5..db6d25f74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -279,7 +279,7 @@ pub mod primitive; pub mod provider; #[cfg(feature = "sys")] -pub(crate) mod sys; +pub mod sys; mod builtins; @@ -306,7 +306,7 @@ pub use error::TemporalError; #[cfg(feature = "sys")] #[doc(inline)] -pub use sys::{DefaultHostSystem, Temporal}; +pub use sys::Temporal; pub mod partial { //! Partial date and time component records diff --git a/src/sys.rs b/src/sys.rs index cbe3ba557..034e38caa 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -74,7 +74,7 @@ impl HostTimeZone for UtcHostSystem { &self, provider: &impl timezone_provider::provider::TimeZoneProvider, ) -> TemporalResult { - get_system_timezone(provider) + Ok(TimeZone::utc_with_provider(provider)) } } From 6d5fc2cb1a024866bf6be04949d5eb3d520df5e0 Mon Sep 17 00:00:00 2001 From: Kevin Ness Date: Thu, 25 Sep 2025 19:47:15 -0500 Subject: [PATCH 3/4] Use imported TimeZoneProvider --- src/sys.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sys.rs b/src/sys.rs index 034e38caa..6cfd3f25c 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -72,7 +72,7 @@ impl HostClock for UtcHostSystem { impl HostTimeZone for UtcHostSystem { fn get_host_time_zone( &self, - provider: &impl timezone_provider::provider::TimeZoneProvider, + provider: &impl TimeZoneProvider, ) -> TemporalResult { Ok(TimeZone::utc_with_provider(provider)) } @@ -99,7 +99,7 @@ impl HostClock for LocalHostSystem { impl HostTimeZone for LocalHostSystem { fn get_host_time_zone( &self, - provider: &impl timezone_provider::provider::TimeZoneProvider, + provider: &impl TimeZoneProvider, ) -> TemporalResult { get_system_timezone(provider) } From f0736d400ec1da321d75590ee32f4eec5d3e5bc5 Mon Sep 17 00:00:00 2001 From: Kevin Ness Date: Thu, 25 Sep 2025 21:55:35 -0500 Subject: [PATCH 4/4] cargo fmt + lib fixes --- README.md | 4 ++-- src/lib.rs | 4 ++-- src/sys.rs | 10 ++-------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 58b5c3315..2851bc906 100644 --- a/README.md +++ b/README.md @@ -119,8 +119,8 @@ time. This can then be used to map to any of the above Temporal types. ```rust use core::cmp::Ordering; use temporal_rs::{Temporal, Calendar, ZonedDateTime}; -let current_instant = Temporal::now().instant().unwrap(); -let current_zoned_date_time = Temporal::now().zoned_date_time_iso(None).unwrap(); +let current_instant = Temporal::utc_now().instant().unwrap(); +let current_zoned_date_time = Temporal::utc_now().zoned_date_time_iso(None).unwrap(); /// Create a `ZonedDateTime` from the requested instant. let zoned_date_time_from_instant = ZonedDateTime::try_new( diff --git a/src/lib.rs b/src/lib.rs index db6d25f74..d479d5fcb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,8 +111,8 @@ //! # #[cfg(all(feature = "sys", feature = "compiled_data"))] { //! use core::cmp::Ordering; //! use temporal_rs::{Temporal, Calendar, ZonedDateTime}; -//! let current_instant = Temporal::now().instant().unwrap(); -//! let current_zoned_date_time = Temporal::now().zoned_date_time_iso(None).unwrap(); +//! let current_instant = Temporal::utc_now().instant().unwrap(); +//! let current_zoned_date_time = Temporal::utc_now().zoned_date_time_iso(None).unwrap(); //! //! /// Create a `ZonedDateTime` from the requested instant. //! let zoned_date_time_from_instant = ZonedDateTime::try_new( diff --git a/src/sys.rs b/src/sys.rs index 6cfd3f25c..6ff9dea89 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -70,10 +70,7 @@ impl HostClock for UtcHostSystem { #[cfg(feature = "sys")] impl HostTimeZone for UtcHostSystem { - fn get_host_time_zone( - &self, - provider: &impl TimeZoneProvider, - ) -> TemporalResult { + fn get_host_time_zone(&self, provider: &impl TimeZoneProvider) -> TemporalResult { Ok(TimeZone::utc_with_provider(provider)) } } @@ -97,10 +94,7 @@ impl HostClock for LocalHostSystem { #[cfg(feature = "sys-local")] impl HostTimeZone for LocalHostSystem { - fn get_host_time_zone( - &self, - provider: &impl TimeZoneProvider, - ) -> TemporalResult { + fn get_host_time_zone(&self, provider: &impl TimeZoneProvider) -> TemporalResult { get_system_timezone(provider) } }