Skip to content

Commit 49c2f59

Browse files
authoredNov 3, 2024
Implement ZonedDateTime's add and subtract methods (#102)
Re: title, depends on #99 and #100 This PR implements `ZonedDateTime`'s add and subtract methods along with related `TimeZone` operations that were needed. There is also one test to verify the results based off test262.
1 parent 149ecb3 commit 49c2f59

File tree

9 files changed

+478
-72
lines changed

9 files changed

+478
-72
lines changed
 

‎Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ rustc-hash = { version = "2.0.0", features = ["std"] }
2424
bitflags = "2.6.0"
2525
num-traits = "0.2.19"
2626
ixdtf = { version = "0.2.0", features = ["duration"]}
27+
iana-time-zone = "0.1.61"
28+
29+
# log feature
2730
log = { version = "0.4.0", optional = true }
2831

32+
# tzdb feature
2933
tzif = { version = "0.2.3", optional = true }
30-
iana-time-zone = "0.1.61"
3134
jiff-tzdb = { version = "0.1.1", optional = true }
3235
combine = { version = "4.6.7", optional = true }
3336

‎src/components/instant.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Instant {
4848
let nanos = i128::from_f64(result).ok_or_else(|| {
4949
TemporalError::range().with_message("Duration added to instant exceeded valid range.")
5050
})?;
51-
Self::new(nanos)
51+
Self::try_new(nanos)
5252
}
5353

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

232232
let round_result = self.round_instant(resolved_options)?;
233-
Self::new(round_result)
233+
Self::try_new(round_result)
234234
}
235235

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

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

344344
let max_plus_one = NS_MAX_INSTANT + 1;
345345
let min_minus_one = NS_MIN_INSTANT - 1;
346346

347-
assert!(Instant::new(max_plus_one).is_err());
348-
assert!(Instant::new(min_minus_one).is_err());
347+
assert!(Instant::try_new(max_plus_one).is_err());
348+
assert!(Instant::try_new(min_minus_one).is_err());
349349
}
350350

351351
#[test]
@@ -373,11 +373,11 @@ mod tests {
373373
)
374374
};
375375

376-
let earlier = Instant::new(
376+
let earlier = Instant::try_new(
377377
217_178_610_123_456_789, /* 1976-11-18T15:23:30.123456789Z */
378378
)
379379
.unwrap();
380-
let later = Instant::new(
380+
let later = Instant::try_new(
381381
1_572_345_998_271_986_289, /* 2019-10-29T10:46:38.271986289Z */
382382
)
383383
.unwrap();
@@ -452,11 +452,11 @@ mod tests {
452452
)
453453
};
454454

455-
let earlier = Instant::new(
455+
let earlier = Instant::try_new(
456456
217_178_610_123_456_789, /* 1976-11-18T15:23:30.123456789Z */
457457
)
458458
.unwrap();
459-
let later = Instant::new(
459+
let later = Instant::try_new(
460460
1_572_345_998_271_986_289, /* 2019-10-29T10:46:38.271986289Z */
461461
)
462462
.unwrap();

‎src/components/now.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
//! The Temporal Now component
22
3+
use crate::{sys, TemporalResult};
34
use alloc::string::String;
5+
6+
#[cfg(feature = "std")]
47
use num_traits::FromPrimitive;
58

6-
use crate::{iso::IsoDateTime, sys, TemporalResult, TemporalUnwrap};
9+
#[cfg(feature = "std")]
10+
use crate::{iso::IsoDateTime, TemporalUnwrap};
711

12+
#[cfg(feature = "std")]
813
use super::{
914
calendar::Calendar,
1015
tz::{TimeZone, TzProvider},
@@ -19,7 +24,10 @@ impl Now {
1924
pub fn time_zone_id() -> TemporalResult<String> {
2025
sys::get_system_tz_identifier()
2126
}
27+
}
2228

29+
#[cfg(feature = "std")]
30+
impl Now {
2331
/// Returns the current instant
2432
pub fn instant() -> TemporalResult<Instant> {
2533
system_instant()
@@ -34,6 +42,7 @@ impl Now {
3442
}
3543
}
3644

45+
#[cfg(feature = "std")]
3746
fn system_date_time(
3847
tz: Option<TimeZone>,
3948
provider: &mut impl TzProvider,
@@ -55,7 +64,8 @@ fn system_date_time(
5564
)
5665
}
5766

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

0 commit comments

Comments
 (0)