]> Cypherpunks repositories - gostls13.git/commitdiff
time: strip monotonic clock reading in t.UTC, t.Local, t.In
authorRuss Cox <rsc@golang.org>
Mon, 5 Jun 2017 15:00:34 +0000 (11:00 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 6 Jun 2017 18:23:14 +0000 (18:23 +0000)
Fixes #18991.

Change-Id: I46ded007b0c6a6e1173a55f3938007ab3a928dd9
Reviewed-on: https://go-review.googlesource.com/44858
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
src/time/mono_test.go
src/time/time.go

index 794f1d1b1cd3e4ceca10485dcfe1a788836198da..8778ab78a03ac4886c07eb8bf1af0d55f4c3bc46 100644 (file)
@@ -51,9 +51,9 @@ func TestHasMonotonicClock(t *testing.T) {
        yes("tm.Add(1)", tm.Add(1))
        no("tm.AddDate(1, 1, 1)", tm.AddDate(1, 1, 1))
        no("tm.AddDate(0, 0, 0)", tm.AddDate(0, 0, 0))
-       yes("tm.In(UTC)", tm.In(UTC))
-       yes("tm.Local()", tm.Local())
-       yes("tm.UTC()", tm.UTC())
+       no("tm.In(UTC)", tm.In(UTC))
+       no("tm.Local()", tm.Local())
+       no("tm.UTC()", tm.UTC())
        no("tm.Round(2)", tm.Round(2))
        no("tm.Truncate(2)", tm.Truncate(2))
 }
index ff016a6cad9eeb3ff436adbf22b4e8ae525b5a22..583198278dd55cdc552576b1ee6a79c3d5310f3b 100644 (file)
 // The Time returned by time.Now contains a monotonic clock reading.
 // If Time t has a monotonic clock reading, t.Add adds the same duration to
 // both the wall clock and monotonic clock readings to compute the result.
-// Similarly, t.In, t.Local, and t.UTC, which are defined to change only the Time's
-// Location, pass any monotonic clock reading through unmodified.
 // Because t.AddDate(y, m, d), t.Round(d), and t.Truncate(d) are wall time
 // computations, they always strip any monotonic clock reading from their results.
+// Because t.In, t.Local, and t.UTC are used for their effect on the interpretation
+// of the wall time, they also strip any monotonic clock reading from their results.
+// The canonical way to strip a monotonic clock reading is to use t = t.Round(0).
 //
 // If Times t and u both contain monotonic clock readings, the operations
 // t.After(u), t.Before(u), t.Equal(u), and t.Sub(u) are carried out
@@ -64,7 +65,7 @@
 // constructed by other means (for example, by time.Parse or time.Unix)
 // are meant to compare equal when used as map keys, the times returned
 // by time.Now must have the monotonic clock reading stripped, by setting
-// t = t.AddDate(0, 0, 0). In general, prefer t.Equal(u) to t == u, since
+// t = t.Round(0). In general, prefer t.Equal(u) to t == u, since
 // t.Equal uses the most accurate comparison available and correctly
 // handles the case when only one of its arguments has a monotonic clock
 // reading.
@@ -186,6 +187,7 @@ func (t *Time) setLoc(loc *Location) {
        if loc == &utcLoc {
                loc = nil
        }
+       t.stripMono()
        t.loc = loc
 }