Skip to content
Open
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 @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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 @@ -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;
Expand All @@ -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();

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: 54 additions & 14 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 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<EpochNanoseconds> {
get_system_nanoseconds()
}
}

#[cfg(feature = "sys")]
impl HostTimeZone for DefaultHostSystem {
fn get_host_time_zone(
&self,
provider: &impl timezone_provider::provider::TimeZoneProvider,
) -> TemporalResult<TimeZone> {
impl HostTimeZone for UtcHostSystem {
fn get_host_time_zone(&self, provider: &impl TimeZoneProvider) -> TemporalResult<TimeZone> {
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<EpochNanoseconds> {
get_system_nanoseconds()
}
}

#[cfg(feature = "sys-local")]
impl HostTimeZone for LocalHostSystem {
fn get_host_time_zone(&self, provider: &impl TimeZoneProvider) -> TemporalResult<TimeZone> {
get_system_timezone(provider)
}
}

#[cfg(feature = "sys")]
#[cfg(feature = "sys-local")]
#[inline]
pub(crate) fn get_system_timezone(provider: &impl TimeZoneProvider) -> TemporalResult<TimeZone> {
iana_time_zone::get_timezone()
Expand Down
Loading