Skip to content

Commit c783303

Browse files
authored
Fix returned duration sign for non-ISO date arithmetic (#618)
``` 'intl402/Temporal/PlainDateTime/prototype/since/wrapping-at-end-of-month-gregorian': [FAIL], 'intl402/Temporal/PlainDateTime/prototype/until/wrapping-at-end-of-month-gregorian': [FAIL], ``` Surprised this didn't break more things. Fixes #617
1 parent c7795ef commit c783303

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/builtins/core/calendar.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,16 @@ impl Calendar {
514514
.days
515515
.try_into()
516516
.map_err(|_| TemporalError::range().with_enum(ErrorMessage::DurationNotValid))?;
517-
let duration = DateDuration::new(
517+
let mut duration = DateDuration::new(
518518
added.years.into(),
519519
added.months.into(),
520520
added.weeks.into(),
521521
days,
522522
)?;
523+
524+
if added.is_negative {
525+
duration = duration.negated();
526+
}
523527
Ok(Duration::from(duration))
524528
}
525529

src/builtins/core/plain_date_time.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,24 @@ mod tests {
13091309
assert_eq!(result.minutes(), 30);
13101310
}
13111311

1312+
#[test]
1313+
fn dt_since_conflicting_signs() {
1314+
// From intl402/Temporal/PlainDateTime/prototype/since/wrapping-at-end-of-month-gregorian
1315+
// Tests that date arithmetic with conflicting signs works
1316+
let a = PlainDateTime::try_new(2023, 3, 1, 2, 0, 0, 0, 0, 0, Calendar::GREGORIAN).unwrap();
1317+
let b = PlainDateTime::try_new(2023, 1, 1, 3, 0, 0, 0, 0, 0, Calendar::GREGORIAN).unwrap();
1318+
1319+
let settings = DifferenceSettings {
1320+
largest_unit: Some(Unit::Year),
1321+
..Default::default()
1322+
};
1323+
let result = a.since(&b, settings).unwrap();
1324+
1325+
assert_eq!(result.months(), 1);
1326+
assert_eq!(result.days(), 30);
1327+
assert_eq!(result.hours(), 23);
1328+
}
1329+
13121330
#[test]
13131331
fn dt_round_basic() {
13141332
let assert_datetime =

0 commit comments

Comments
 (0)