From: Russ Cox Date: Mon, 5 Jun 2017 15:00:34 +0000 (-0400) Subject: time: strip monotonic clock reading in t.UTC, t.Local, t.In X-Git-Tag: go1.9beta1~107 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e5646b23dee5eb6f896a3eef45959aa130857988;p=gostls13.git time: strip monotonic clock reading in t.UTC, t.Local, t.In Fixes #18991. Change-Id: I46ded007b0c6a6e1173a55f3938007ab3a928dd9 Reviewed-on: https://go-review.googlesource.com/44858 Run-TryBot: Russ Cox Reviewed-by: Brad Fitzpatrick Reviewed-by: Joe Tsai --- diff --git a/src/time/mono_test.go b/src/time/mono_test.go index 794f1d1b1c..8778ab78a0 100644 --- a/src/time/mono_test.go +++ b/src/time/mono_test.go @@ -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)) } diff --git a/src/time/time.go b/src/time/time.go index ff016a6cad..583198278d 100644 --- a/src/time/time.go +++ b/src/time/time.go @@ -39,10 +39,11 @@ // 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 }