diff --git a/Cargo.toml b/Cargo.toml index e4e064cc0..e1c16ed53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,10 +102,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 fb87d9291..d56af70e8 100644 --- a/src/builtins/core/now.rs +++ b/src/builtins/core/now.rs @@ -213,7 +213,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; @@ -222,9 +222,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/lib.rs b/src/lib.rs index c0d2ee535..31d7afd56 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( @@ -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 8ad309bb0..c00d2454c 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -25,25 +25,76 @@ pub struct Temporal; impl Temporal { /// Get a `Now` object for the default host system. - pub const 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 UTC host system implementation that will return the current time +/// with the a UTC time zone as fallback. +/// +/// This implementation is backed by [`std::time::SystemTime`]. +#[cfg(feature = "sys")] +pub struct UtcHostSystem; + +#[cfg(feature = "sys")] +impl HostHooks for UtcHostSystem {} + +#[cfg(feature = "sys")] +impl HostClock for UtcHostSystem { + fn get_host_epoch_nanoseconds(&self) -> TemporalResult { + get_system_nanoseconds() + } +} + +#[cfg(feature = "sys")] +impl HostTimeZone for UtcHostSystem { + fn get_host_time_zone( + &self, + provider: &(impl TimeZoneProvider + ?Sized), + ) -> TemporalResult { + Ok(TimeZone::utc_with_provider(provider)) } } -/// A default host system implementation +/// A local host system implementation that will return the current time +/// with the system time zone as a fallback. /// -/// This implementation is backed by [`SystemTime`] and [`iana_time_zone`] -pub struct DefaultHostSystem; +/// This implementation is backed by [`std::time::SystemTime`] and [`iana_time_zone`] +#[cfg(feature = "sys-local")] +pub struct LocalHostSystem; -impl HostHooks for DefaultHostSystem {} +#[cfg(feature = "sys-local")] +impl HostHooks for LocalHostSystem {} -impl HostClock for DefaultHostSystem { +#[cfg(feature = "sys-local")] +impl HostClock for LocalHostSystem { fn get_host_epoch_nanoseconds(&self) -> TemporalResult { get_system_nanoseconds() } } -impl HostTimeZone for DefaultHostSystem { +#[cfg(feature = "sys-local")] +impl HostTimeZone for LocalHostSystem { fn get_host_time_zone( &self, provider: &(impl TimeZoneProvider + ?Sized), @@ -52,6 +103,7 @@ impl HostTimeZone for DefaultHostSystem { } } +#[cfg(feature = "sys-local")] #[inline] pub(crate) fn get_system_timezone( provider: &(impl TimeZoneProvider + ?Sized),