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: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ rustc-hash = { version = "2.0.0", features = ["std"] }
bitflags = "2.6.0"
num-traits = "0.2.19"
ixdtf = { version = "0.2.0", features = ["duration"]}
iana-time-zone = "0.1.61"

# log feature
log = { version = "0.4.0", optional = true }

# tzdb feature
tzif = { version = "0.2.3", optional = true }
iana-time-zone = "0.1.61"
jiff-tzdb = { version = "0.1.1", optional = true }
combine = { version = "4.6.7", optional = true }

Expand Down
22 changes: 11 additions & 11 deletions src/components/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Instant {
let nanos = i128::from_f64(result).ok_or_else(|| {
TemporalError::range().with_message("Duration added to instant exceeded valid range.")
})?;
Self::new(nanos)
Self::try_new(nanos)
}

// TODO: Add test for `diff_instant`.
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Instant {
impl Instant {
/// Create a new validated `Instant`.
#[inline]
pub fn new(epoch_nanoseconds: i128) -> TemporalResult<Self> {
pub fn try_new(epoch_nanoseconds: i128) -> TemporalResult<Self> {
if !is_valid_epoch_nanos(&epoch_nanoseconds) {
return Err(TemporalError::range()
.with_message("Instant nanoseconds are not within a valid epoch range."));
Expand Down Expand Up @@ -230,7 +230,7 @@ impl Instant {
let resolved_options = ResolvedRoundingOptions::from_instant_options(options)?;

let round_result = self.round_instant(resolved_options)?;
Self::new(round_result)
Self::try_new(round_result)
}

/// Returns the `epochSeconds` value for this `Instant`.
Expand Down Expand Up @@ -335,17 +335,17 @@ mod tests {
// valid, i.e., a valid instant is within the range of an f64.
let max = NS_MAX_INSTANT;
let min = NS_MIN_INSTANT;
let max_instant = Instant::new(max).unwrap();
let min_instant = Instant::new(min).unwrap();
let max_instant = Instant::try_new(max).unwrap();
let min_instant = Instant::try_new(min).unwrap();

assert_eq!(max_instant.epoch_nanoseconds(), max.to_f64().unwrap());
assert_eq!(min_instant.epoch_nanoseconds(), min.to_f64().unwrap());

let max_plus_one = NS_MAX_INSTANT + 1;
let min_minus_one = NS_MIN_INSTANT - 1;

assert!(Instant::new(max_plus_one).is_err());
assert!(Instant::new(min_minus_one).is_err());
assert!(Instant::try_new(max_plus_one).is_err());
assert!(Instant::try_new(min_minus_one).is_err());
}

#[test]
Expand Down Expand Up @@ -373,11 +373,11 @@ mod tests {
)
};

let earlier = Instant::new(
let earlier = Instant::try_new(
217_178_610_123_456_789, /* 1976-11-18T15:23:30.123456789Z */
)
.unwrap();
let later = Instant::new(
let later = Instant::try_new(
1_572_345_998_271_986_289, /* 2019-10-29T10:46:38.271986289Z */
)
.unwrap();
Expand Down Expand Up @@ -452,11 +452,11 @@ mod tests {
)
};

let earlier = Instant::new(
let earlier = Instant::try_new(
217_178_610_123_456_789, /* 1976-11-18T15:23:30.123456789Z */
)
.unwrap();
let later = Instant::new(
let later = Instant::try_new(
1_572_345_998_271_986_289, /* 2019-10-29T10:46:38.271986289Z */
)
.unwrap();
Expand Down
14 changes: 12 additions & 2 deletions src/components/now.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! The Temporal Now component

use crate::{sys, TemporalResult};
use alloc::string::String;

#[cfg(feature = "std")]
use num_traits::FromPrimitive;

use crate::{iso::IsoDateTime, sys, TemporalResult, TemporalUnwrap};
#[cfg(feature = "std")]
use crate::{iso::IsoDateTime, TemporalUnwrap};

#[cfg(feature = "std")]
use super::{
calendar::Calendar,
tz::{TimeZone, TzProvider},
Expand All @@ -19,7 +24,10 @@ impl Now {
pub fn time_zone_id() -> TemporalResult<String> {
sys::get_system_tz_identifier()
}
}

#[cfg(feature = "std")]
impl Now {
/// Returns the current instant
pub fn instant() -> TemporalResult<Instant> {
system_instant()
Expand All @@ -34,6 +42,7 @@ impl Now {
}
}

#[cfg(feature = "std")]
fn system_date_time(
tz: Option<TimeZone>,
provider: &mut impl TzProvider,
Expand All @@ -55,7 +64,8 @@ fn system_date_time(
)
}

#[cfg(feature = "std")]
fn system_instant() -> TemporalResult<Instant> {
let nanos = sys::get_system_nanoseconds()?;
Instant::new(i128::from_u128(nanos).temporal_unwrap()?)
Instant::try_new(i128::from_u128(nanos).temporal_unwrap()?)
}
Loading