Ensure two SystemTime
s are equal when nanos add to exactly 1B
Currently if you add a duration which should lead to 0 nanos and 1 additional second, we end up with no additional seconds, and 1000000000 nanos.
This commit is contained in:
parent
d5321f2abe
commit
a69bcd885b
2 changed files with 5 additions and 1 deletions
|
@ -330,7 +330,7 @@ mod inner {
|
||||||
// Nano calculations can't overflow because nanos are <1B which fit
|
// Nano calculations can't overflow because nanos are <1B which fit
|
||||||
// in a u32.
|
// in a u32.
|
||||||
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
|
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
|
||||||
if nsec > NSEC_PER_SEC as u32 {
|
if nsec >= NSEC_PER_SEC as u32 {
|
||||||
nsec -= NSEC_PER_SEC as u32;
|
nsec -= NSEC_PER_SEC as u32;
|
||||||
secs = secs.checked_add(1).expect("overflow when adding \
|
secs = secs.checked_add(1).expect("overflow when adding \
|
||||||
duration to time");
|
duration to time");
|
||||||
|
|
|
@ -303,6 +303,10 @@ mod tests {
|
||||||
let eighty_years = second * 60 * 60 * 24 * 365 * 80;
|
let eighty_years = second * 60 * 60 * 24 * 365 * 80;
|
||||||
assert_almost_eq!(a - eighty_years + eighty_years, a);
|
assert_almost_eq!(a - eighty_years + eighty_years, a);
|
||||||
assert_almost_eq!(a - (eighty_years * 10) + (eighty_years * 10), a);
|
assert_almost_eq!(a - (eighty_years * 10) + (eighty_years * 10), a);
|
||||||
|
|
||||||
|
let one_second_from_epoch = UNIX_EPOCH + Duration::new(1, 0);
|
||||||
|
let one_second_from_epoch2 = UNIX_EPOCH + Duration::new(0, 999_999_999) + Duration::new(0, 1);
|
||||||
|
assert_eq!(one_second_from_epoch, one_second_from_epoch2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue