Skip to content

Commit deee291

Browse files
authored
Cross boundary rounding fix #286 (#343)
Unable to reproduce the reported [bug](#286), but new tests now cover the described scenarios.
1 parent f687f16 commit deee291

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/builtins/compiled/duration/tests.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,51 @@ fn test_duration_compare_boundary() {
799799
let err = min_duration.compare(&zero, Some(RelativeTo::PlainDate(relative_to.clone())));
800800
assert!(err.is_err());
801801
}
802+
803+
#[test]
804+
fn rounding_cross_boundary() {
805+
let relative_to = PlainDate::new(2022, 1, 1, Calendar::default()).unwrap();
806+
807+
let duration = Duration::from(DateDuration::new(1, 11, 0, 24).unwrap());
808+
let options = RoundingOptions {
809+
smallest_unit: Some(Unit::Month),
810+
rounding_mode: Some(RoundingMode::Expand),
811+
..Default::default()
812+
};
813+
let result = duration
814+
.round(options, Some(RelativeTo::PlainDate(relative_to)))
815+
.unwrap();
816+
assert_duration(result, (2, 0, 0, 0, 0, 0, 0, 0, 0, 0));
817+
}
818+
819+
#[test]
820+
fn rounding_cross_boundary_negative() {
821+
let relative_to = PlainDate::new(2022, 1, 1, Calendar::default()).unwrap();
822+
823+
let duration = Duration::from(DateDuration::new(-1, -11, 0, -24).unwrap());
824+
let options = RoundingOptions {
825+
smallest_unit: Some(Unit::Month),
826+
rounding_mode: Some(RoundingMode::Expand),
827+
..Default::default()
828+
};
829+
let result = duration
830+
.round(options, Some(RelativeTo::PlainDate(relative_to)))
831+
.unwrap();
832+
assert_duration(result, (-2, 0, 0, 0, 0, 0, 0, 0, 0, 0));
833+
}
834+
835+
#[test]
836+
fn rounding_cross_boundary_time_units() {
837+
let duration = Duration::new(0, 0, 0, 0, 1, 59, 59, 900, 0, 0).unwrap();
838+
let options = RoundingOptions {
839+
smallest_unit: Some(Unit::Second),
840+
rounding_mode: Some(RoundingMode::Expand),
841+
..Default::default()
842+
};
843+
let result = duration.round(options, None).unwrap();
844+
assert_duration(result, (0, 0, 0, 0, 2, 0, 0, 0, 0, 0));
845+
846+
let neg_duration = Duration::new(0, 0, 0, 0, -1, -59, -59, -900, 0, 0).unwrap();
847+
let result = neg_duration.round(options, None).unwrap();
848+
assert_duration(result, (0, 0, 0, 0, -2, 0, 0, 0, 0, 0));
849+
}

0 commit comments

Comments
 (0)