@@ -18,8 +18,8 @@ use crate::{
1818 primitive:: { DoubleDouble , FiniteF64 } ,
1919 provider:: TimeZoneProvider ,
2020 rounding:: IncrementRounder ,
21- temporal_assert, Calendar , TemporalError , TemporalResult , TemporalUnwrap , NS_PER_DAY ,
22- NS_PER_DAY_NONZERO ,
21+ temporal_assert, Calendar , NonZeroSign , TemporalError , TemporalResult , TemporalUnwrap ,
22+ NS_PER_DAY , NS_PER_DAY_NONZERO ,
2323} ;
2424
2525use super :: { DateDuration , Duration , Sign } ;
@@ -409,7 +409,7 @@ impl InternalDurationRecord {
409409 /// `compute_and_adjust_nudge_window` in `temporal_rs` refers to step 1-12 of `NudgeToCalendarUnit`.
410410 fn compute_and_adjust_nudge_window (
411411 & self ,
412- sign : Sign ,
412+ sign : NonZeroSign ,
413413 origin_epoch_ns : EpochNanoseconds ,
414414 dest_epoch_ns : i128 ,
415415 dt : & PlainDateTime ,
@@ -430,47 +430,61 @@ impl InternalDurationRecord {
430430 // (implicitly used)
431431
432432 // 5. If sign is 1, then
433- if sign != Sign :: Negative {
434- // a. If startEpochNs ≤ destEpochNs ≤ endEpochNs is false, then
435- if !( nudge_window. start_epoch_ns <= dest_epoch_ns
436- && dest_epoch_ns <= nudge_window. end_epoch_ns )
437- {
438- // i. Set nudgeWindow to ? ComputeNudgeWindow(sign, duration, originEpochNs, isoDateTime, timeZone, calendar, increment, unit, true).
439- nudge_window =
440- self . compute_nudge_window ( sign, origin_epoch_ns, dt, time_zone, options, true ) ?;
441- // ii. Assert: nudgeWindow.[[StartEpochNs]] ≤ destEpochNs ≤ nudgeWindow.[[EndEpochNs]].
442- temporal_assert ! (
443- nudge_window. start_epoch_ns <= dest_epoch_ns
444- && dest_epoch_ns <= nudge_window. end_epoch_ns
445- ) ;
446- // iii. Set didExpandCalendarUnit to true.
447- did_expand_calendar_unit = true ;
433+ match sign {
434+ NonZeroSign :: Positive => {
435+ // a. If startEpochNs ≤ destEpochNs ≤ endEpochNs is false, then
436+ if !( nudge_window. start_epoch_ns <= dest_epoch_ns
437+ && dest_epoch_ns <= nudge_window. end_epoch_ns )
438+ {
439+ // i. Set nudgeWindow to ? ComputeNudgeWindow(sign, duration, originEpochNs, isoDateTime, timeZone, calendar, increment, unit, true).
440+ nudge_window = self . compute_nudge_window (
441+ sign,
442+ origin_epoch_ns,
443+ dt,
444+ time_zone,
445+ options,
446+ true ,
447+ ) ?;
448+ // ii. Assert: nudgeWindow.[[StartEpochNs]] ≤ destEpochNs ≤ nudgeWindow.[[EndEpochNs]].
449+ temporal_assert ! (
450+ nudge_window. start_epoch_ns <= dest_epoch_ns
451+ && dest_epoch_ns <= nudge_window. end_epoch_ns
452+ ) ;
453+ // iii. Set didExpandCalendarUnit to true.
454+ did_expand_calendar_unit = true ;
455+ }
448456 }
449- } else {
450- // a. If endEpochNs ≤ destEpochNs ≤ startEpochNs is false, then
451- if !( nudge_window. end_epoch_ns <= dest_epoch_ns
452- && dest_epoch_ns <= nudge_window. start_epoch_ns )
453- {
454- // i. Set nudgeWindow to ? ComputeNudgeWindow(sign, duration, originEpochNs, isoDateTime, timeZone, calendar, increment, unit, true).
455- nudge_window =
456- self . compute_nudge_window ( sign, origin_epoch_ns, dt, time_zone, options, true ) ?;
457- // ii. Assert: nudgeWindow.[[EndEpochNs]] ≤ destEpochNs ≤ nudgeWindow.[[StartEpochNs]].
458- temporal_assert ! (
459- nudge_window. end_epoch_ns <= dest_epoch_ns
460- && dest_epoch_ns <= nudge_window. start_epoch_ns
461- ) ;
462- // iii. Set didExpandCalendarUnit to true.
463- did_expand_calendar_unit = true ;
457+ NonZeroSign :: Negative => {
458+ // a. If endEpochNs ≤ destEpochNs ≤ startEpochNs is false, then
459+ if !( nudge_window. end_epoch_ns <= dest_epoch_ns
460+ && dest_epoch_ns <= nudge_window. start_epoch_ns )
461+ {
462+ // i. Set nudgeWindow to ? ComputeNudgeWindow(sign, duration, originEpochNs, isoDateTime, timeZone, calendar, increment, unit, true).
463+ nudge_window = self . compute_nudge_window (
464+ sign,
465+ origin_epoch_ns,
466+ dt,
467+ time_zone,
468+ options,
469+ true ,
470+ ) ?;
471+ // ii. Assert: nudgeWindow.[[EndEpochNs]] ≤ destEpochNs ≤ nudgeWindow.[[StartEpochNs]].
472+ temporal_assert ! (
473+ nudge_window. end_epoch_ns <= dest_epoch_ns
474+ && dest_epoch_ns <= nudge_window. start_epoch_ns
475+ ) ;
476+ // iii. Set didExpandCalendarUnit to true.
477+ did_expand_calendar_unit = true ;
478+ }
464479 }
465480 }
466-
467481 Ok ( ( nudge_window, did_expand_calendar_unit) )
468482 }
469483
470484 /// <https://tc39.es/proposal-temporal/#sec-temporal-computenudgewindow>
471485 fn compute_nudge_window (
472486 & self ,
473- sign : Sign ,
487+ sign : NonZeroSign ,
474488 origin_epoch_ns : EpochNanoseconds ,
475489 dt : & PlainDateTime ,
476490 time_zone : Option < ( & TimeZone , & ( impl TimeZoneProvider + ?Sized ) ) > , // ???
@@ -689,8 +703,8 @@ impl InternalDurationRecord {
689703 // 6. Assert: If sign is -1, r1 ≤ 0 and r1 > r2.
690704 // n.b. sign == 1 means nonnegative
691705 crate :: temporal_assert!(
692- ( sign != Sign :: Negative && r1 >= 0 && r1 < r2)
693- || ( sign == Sign :: Negative && r1 <= 0 && r1 > r2)
706+ ( sign != NonZeroSign :: Negative && r1 >= 0 && r1 < r2)
707+ || ( sign == NonZeroSign :: Negative && r1 <= 0 && r1 > r2)
694708 ) ;
695709
696710 let start_epoch_ns = if r1 == 0 {
@@ -749,7 +763,7 @@ impl InternalDurationRecord {
749763
750764 fn nudge_calendar_unit_total (
751765 & self ,
752- sign : Sign ,
766+ sign : NonZeroSign ,
753767 origin_epoch_ns : EpochNanoseconds ,
754768 dest_epoch_ns : i128 ,
755769 dt : & PlainDateTime ,
@@ -827,7 +841,7 @@ impl InternalDurationRecord {
827841 // TODO: Add unit tests specifically for nudge_calendar_unit if possible.
828842 fn nudge_calendar_unit (
829843 & self ,
830- sign : Sign ,
844+ sign : NonZeroSign ,
831845 origin_epoch_ns : EpochNanoseconds ,
832846 dest_epoch_ns : i128 ,
833847 dt : & PlainDateTime ,
@@ -912,7 +926,7 @@ impl InternalDurationRecord {
912926 // n.b. get_unsigned_round_mode takes is_positive, but it actually cares about nonnegative
913927 let unsigned_rounding_mode = options
914928 . rounding_mode
915- . get_unsigned_round_mode ( sign != Sign :: Negative ) ;
929+ . get_unsigned_round_mode ( sign != NonZeroSign :: Negative ) ;
916930
917931 // NOTE (nekevss):
918932 //
@@ -966,7 +980,7 @@ impl InternalDurationRecord {
966980 #[ inline]
967981 fn nudge_to_zoned_time (
968982 & self ,
969- sign : Sign ,
983+ sign : NonZeroSign ,
970984 dt : & PlainDateTime ,
971985 time_zone : & TimeZone ,
972986 options : ResolvedRoundingOptions ,
@@ -1012,7 +1026,7 @@ impl InternalDurationRecord {
10121026 let beyond_day_span = rounded_time. checked_add ( day_span. negate ( ) . 0 ) ?;
10131027 // 12. If TimeDurationSign(beyondDaySpan) ≠ -sign, then
10141028 let ( expanded, day_delta, rounded_time, nudge_ns) =
1015- if ( beyond_day_span. sign ( ) != sign. negate ( ) ) && sign != Sign :: Zero {
1029+ if beyond_day_span. sign ( ) != sign. negate ( ) {
10161030 // a. Let didRoundBeyondDay be true.
10171031 // b. Let dayDelta be sign.
10181032 // c. Set roundedTimeDuration to ? RoundTimeDurationToIncrement(beyondDaySpan, increment × unitLength, roundingMode).
@@ -1127,7 +1141,7 @@ impl InternalDurationRecord {
11271141 #[ allow( clippy:: too_many_arguments) ]
11281142 fn bubble_relative_duration (
11291143 & self ,
1130- sign : Sign ,
1144+ sign : NonZeroSign ,
11311145 nudged_epoch_ns : i128 ,
11321146 iso_date_time : & IsoDateTime ,
11331147 time_zone : Option < ( & TimeZone , & ( impl TimeZoneProvider + ?Sized ) ) > ,
@@ -1277,7 +1291,7 @@ impl InternalDurationRecord {
12771291 || ( time_zone. is_some ( ) && options. smallest_unit == Unit :: Day ) ;
12781292
12791293 // 4. If InternalDurationSign(duration) < 0, let sign be -1; else let sign be 1.
1280- let sign = duration. sign ( ) ;
1294+ let sign = duration. sign ( ) . to_nonzero_sign ( ) ;
12811295
12821296 // 5. If irregularLengthUnit is true, then
12831297 let nudge_result = if irregular_length_unit {
@@ -1336,8 +1350,8 @@ impl InternalDurationRecord {
13361350 ) -> TemporalResult < FiniteF64 > {
13371351 // 1. If IsCalendarUnit(unit) is true, or timeZone is not unset and unit is day, then
13381352 if unit. is_calendar_unit ( ) || ( time_zone. is_some ( ) && unit == Unit :: Day ) {
1339- // a. Let sign be InternalDurationSign(duration).
1340- let sign = self . sign ( ) ;
1353+ // a. If InternalDurationSign(duration) < 0, let sign be -1; else let sign be 1
1354+ let sign = self . sign ( ) . to_nonzero_sign ( ) ;
13411355 // b. Let record be ? NudgeToCalendarUnit(sign, duration, destEpochNs, isoDateTime, timeZone, calendar, 1, unit, trunc).
13421356 // c. Return record.[[Total]].
13431357 return self . nudge_calendar_unit_total (
0 commit comments