]> Cypherpunks repositories - gostls13.git/commitdiff
time: test and fix Time.Round, Duration.Round for d > 2⁶²
authorRuss Cox <rsc@golang.org>
Fri, 31 Mar 2017 16:34:25 +0000 (12:34 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 31 Mar 2017 20:39:58 +0000 (20:39 +0000)
Round uses r+r < d to decide whether the remainder is
above or below half of d (to decide whether to round up or down).
This is wrong when r+r wraps negative, because it looks < d
but is really > d.

No one will ever care about rounding to a multiple of
d > 2⁶² (about 146 years), but might as well get it right.

Fixes #19807.

Change-Id: I1b55a742dc36e02a7465bc778bf5dd74fe71f7c0
Reviewed-on: https://go-review.googlesource.com/39151
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/time/time.go
src/time/time_test.go

index 5bb7dd961d57b57e6b04326069dbcecacb7f59e1..5283b7eb212a23a22da107496792015f2fd10354 100644 (file)
@@ -798,6 +798,12 @@ func (d Duration) Truncate(m Duration) Duration {
        return d - d%m
 }
 
+// lessThanHalf reports whether x+x < y but avoids overflow,
+// assuming x and y are both positive (Duration is signed).
+func lessThanHalf(x, y Duration) bool {
+       return uint64(x)+uint64(x) < uint64(y)
+}
+
 // Round returns the result of rounding d to the nearest multiple of m.
 // The rounding behavior for halfway values is to round away from zero.
 // If the result exceeds the maximum (or minimum)
@@ -811,7 +817,7 @@ func (d Duration) Round(m Duration) Duration {
        r := d % m
        if d < 0 {
                r = -r
-               if r+r < m {
+               if lessThanHalf(r, m) {
                        return d + r
                }
                if d1 := d - m + r; d1 < d {
@@ -819,7 +825,7 @@ func (d Duration) Round(m Duration) Duration {
                }
                return minDuration // overflow
        }
-       if r+r < m {
+       if lessThanHalf(r, m) {
                return d - r
        }
        if d1 := d + m - r; d1 > d {
@@ -1400,7 +1406,7 @@ func (t Time) Round(d Duration) Time {
                return t
        }
        _, r := div(t, d)
-       if r+r < d {
+       if lessThanHalf(r, d) {
                return t.Add(-r)
        }
        return t.Add(d - r)
index ebe28e61f4ea6dfd57f4e918732178c1cc3feb7f..dba8e0dadcdd06bf45b5a0ac81ceed0fabbeaf8a 100644 (file)
@@ -233,6 +233,7 @@ var truncateRoundTests = []struct {
        {Date(-1, January, 1, 12, 15, 31, 5e8, UTC), 3},
        {Date(2012, January, 1, 12, 15, 30, 5e8, UTC), Second},
        {Date(2012, January, 1, 12, 15, 31, 5e8, UTC), Second},
+       {Unix(-19012425939, 649146258), 7435029458905025217}, // 5.8*d rounds to 6*d, but .8*d+.8*d < 0 < d
 }
 
 func TestTruncateRound(t *testing.T) {
@@ -1107,6 +1108,7 @@ var durationRoundTests = []struct {
        {9e18, 5e18, 1<<63 - 1},
        {-8e18, 3e18, -9e18},
        {-9e18, 5e18, -1 << 63},
+       {3<<61 - 1, 3 << 61, 3 << 61},
 }
 
 func TestDurationRound(t *testing.T) {