Skip to content

Commit a69bcd8

Browse files
committed
Ensure two SystemTimes 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.
1 parent d5321f2 commit a69bcd8

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/libstd/sys/unix/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ mod inner {
330330
// Nano calculations can't overflow because nanos are <1B which fit
331331
// in a u32.
332332
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
333-
if nsec > NSEC_PER_SEC as u32 {
333+
if nsec >= NSEC_PER_SEC as u32 {
334334
nsec -= NSEC_PER_SEC as u32;
335335
secs = secs.checked_add(1).expect("overflow when adding \
336336
duration to time");

src/libstd/time/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ mod tests {
303303
let eighty_years = second * 60 * 60 * 24 * 365 * 80;
304304
assert_almost_eq!(a - eighty_years + eighty_years, a);
305305
assert_almost_eq!(a - (eighty_years * 10) + (eighty_years * 10), a);
306+
307+
let one_second_from_epoch = UNIX_EPOCH + Duration::new(1, 0);
308+
let one_second_from_epoch2 = UNIX_EPOCH + Duration::new(0, 999_999_999) + Duration::new(0, 1);
309+
assert_eq!(one_second_from_epoch, one_second_from_epoch2);
306310
}
307311

308312
#[test]

0 commit comments

Comments
 (0)