diff --git a/Cargo.toml b/Cargo.toml index b9d4cf63e..f423af389 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,10 +98,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/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/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/lib.rs b/src/lib.rs index 49f955cd5..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( @@ -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 a530dc883..6ff9dea89 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -25,41 +25,81 @@ 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 { - fn get_host_time_zone( - &self, - provider: &impl timezone_provider::provider::TimeZoneProvider, - ) -> TemporalResult { +impl HostTimeZone for UtcHostSystem { + fn get_host_time_zone(&self, provider: &impl TimeZoneProvider) -> TemporalResult { + Ok(TimeZone::utc_with_provider(provider)) + } +} + +/// 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 TimeZoneProvider) -> TemporalResult { get_system_timezone(provider) } } -#[cfg(feature = "sys")] +#[cfg(feature = "sys-local")] #[inline] pub(crate) fn get_system_timezone(provider: &impl TimeZoneProvider) -> TemporalResult { iana_time_zone::get_timezone()