Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Build out support for Now component #70

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
211 changes: 211 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ bitflags = "2.6.0"
num-bigint = { version = "0.4.6", features = ["serde"] }
num-traits = "0.2.19"
ixdtf = { version = "0.2.0", features = ["duration"]}
iana-time-zone = "0.1.60"
3 changes: 3 additions & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod date;
mod datetime;
mod instant;
mod month_day;
mod now;
mod time;
mod year_month;
mod zoneddatetime;
Expand All @@ -39,6 +40,8 @@ pub use instant::Instant;
#[doc(inline)]
pub use month_day::MonthDay;
#[doc(inline)]
pub use now::Now;
#[doc(inline)]
pub use time::Time;
#[doc(inline)]
pub use year_month::YearMonth;
Expand Down
28 changes: 28 additions & 0 deletions src/components/now.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! The Temporal Now component

use num_bigint::BigInt;
use num_traits::FromPrimitive;

use crate::{sys, TemporalResult, TemporalUnwrap};

use super::Instant;

/// The Temporal Now object.
pub struct Now;

impl Now {
/// Returns the current time zone.
pub fn time_zone_id() -> TemporalResult<String> {
sys::get_system_tz_identifier()
}

/// Returns the current instant
pub fn instant() -> TemporalResult<Instant> {
system_instant()
}
}

fn system_instant() -> TemporalResult<Instant> {
let nanos = sys::get_system_nanoseconds()?;
Instant::new(BigInt::from_u128(nanos).temporal_unwrap()?)
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub(crate) mod rounding;
#[doc(hidden)]
pub(crate) mod utils;

#[doc(hidden)]
pub(crate) mod sys;

// TODO: evaluate positives and negatives of using tinystr.
// Re-exporting tinystr as a convenience, as it is currently tied into the API.
pub use tinystr::TinyAsciiStr;
Expand Down
16 changes: 16 additions & 0 deletions src/sys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

use std::time::{SystemTime, UNIX_EPOCH};

use crate::{TemporalError, TemporalResult};

// TODO: Need to implement system handling for non_std.

/// Returns the system time in nanoseconds.
pub(crate) fn get_system_nanoseconds() -> TemporalResult<u128> {
SystemTime::now().duration_since(UNIX_EPOCH).map_err(|e| TemporalError::general(e.to_string())).map(|d| d.as_nanos())
}

/// Returns the system tz identifier
pub(crate) fn get_system_tz_identifier() -> TemporalResult<String> {
iana_time_zone::get_timezone().map_err(|e| TemporalError::general(e.to_string()))
}
Loading