Skip to content

Commit 55a4e05

Browse files
committed
Auto merge of #30173 - sgrif:sg-fix-time-bug, r=alexcrichton
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.
2 parents 68c15be + 0747142 commit 55a4e05

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/libstd/sys/unix/time.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ mod inner {
111111
// Nano calculations can't overflow because nanos are <1B which fit
112112
// in a u32.
113113
let mut usec = (other.subsec_nanos() / 1000) + self.t.tv_usec as u32;
114-
if usec > USEC_PER_SEC as u32 {
114+
if usec >= USEC_PER_SEC as u32 {
115115
usec -= USEC_PER_SEC as u32;
116116
secs = secs.checked_add(1).expect("overflow when adding \
117117
duration to time");
@@ -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

+5
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,11 @@ 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, 500_000_000)
309+
+ Duration::new(0, 500_000_000);
310+
assert_eq!(one_second_from_epoch, one_second_from_epoch2);
306311
}
307312

308313
#[test]

0 commit comments

Comments
 (0)