Skip to content

Commit 8ef18fe

Browse files
authored
Updates to instant and its methods (#85)
This PR updates Instant to some of the newer specification changes. TLDR of the below: - Updates `Instant` struct - Updates diffing ops and adds tests - Implements `FromStr`. Primarily, it brings `since` and `until` up to date with both `temporal_rs` and the specification as a whole (along with at least 2 basic tests for `since` and `until`) This PR also changes Instant inner nanoseconds from `BigInt` to `i128`. My thought is to eventually remove all usage of `BigInt` from `temporal_rs` in general. Primarily motivated by the thought that if `NormalizedTimeDuration` is using a `i128`, I don't see a huge reason why `Instant` should differ, unless I'm overlooking something and someone thinks otherwise. Finally, it implements `FromStr` for `Instant`. This will allow parsing of `JsString` in Boa, and would unblock `to_temporal_instant`.
1 parent 2e6750a commit 8ef18fe

File tree

8 files changed

+341
-99
lines changed

8 files changed

+341
-99
lines changed

src/components/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl DateTime {
6464
offset: f64,
6565
calendar: Calendar,
6666
) -> TemporalResult<Self> {
67-
let iso = IsoDateTime::from_epoch_nanos(&instant.nanos, offset)?;
67+
let iso = IsoDateTime::from_epoch_nanos(&instant.epoch_nanos, offset)?;
6868
Ok(Self { iso, calendar })
6969
}
7070

src/components/duration/normalized.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ impl NormalizedTimeDuration {
4949
Self(nanoseconds)
5050
}
5151

52+
/// Equivalent to 7.5.27 NormalizedTimeDurationFromEpochNanosecondsDifference ( one, two )
53+
pub(crate) fn from_nanosecond_difference(one: i128, two: i128) -> TemporalResult<Self> {
54+
let result = one - two;
55+
if result.abs() > MAX_TIME_DURATION {
56+
return Err(TemporalError::range()
57+
.with_message("normalizedTimeDuration exceeds maxTimeDuration."));
58+
}
59+
Ok(Self(result))
60+
}
61+
5262
// NOTE: `days: f64` should be an integer -> `i64`.
5363
/// Equivalent: 7.5.23 Add24HourDaysToNormalizedTimeDuration ( d, days )
5464
#[allow(unused)]

src/components/duration/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use num_traits::{Euclid, FromPrimitive, MulAdd};
2323
/// [spec]: https://tc39.es/proposal-temporal/#sec-temporal-time-duration-records
2424
/// [field spec]: https://tc39.es/proposal-temporal/#sec-properties-of-temporal-duration-instances
2525
#[non_exhaustive]
26-
#[derive(Debug, Default, Clone, Copy)]
26+
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
2727
pub struct TimeDuration {
2828
/// `TimeDuration`'s internal hour value.
2929
pub hours: f64,

0 commit comments

Comments
 (0)