Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/builtins/core/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -279,7 +279,7 @@ pub mod primitive;
pub mod provider;

#[cfg(feature = "sys")]
pub(crate) mod sys;
pub mod sys;

mod builtins;

Expand All @@ -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
Expand Down
68 changes: 60 additions & 8 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,76 @@ pub struct Temporal;

impl Temporal {
/// Get a `Now` object for the default host system.
pub const fn now() -> Now<DefaultHostSystem> {
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<LocalHostSystem> {
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<LocalHostSystem> {
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<UtcHostSystem> {
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<EpochNanoseconds> {
get_system_nanoseconds()
}
}

#[cfg(feature = "sys")]
impl HostTimeZone for UtcHostSystem {
fn get_host_time_zone(
&self,
provider: &(impl TimeZoneProvider + ?Sized),
) -> TemporalResult<TimeZone> {
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<EpochNanoseconds> {
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),
Expand All @@ -52,6 +103,7 @@ impl HostTimeZone for DefaultHostSystem {
}
}

#[cfg(feature = "sys-local")]
#[inline]
pub(crate) fn get_system_timezone(
provider: &(impl TimeZoneProvider + ?Sized),
Expand Down