Commit e5646b23 authored by Russ Cox's avatar Russ Cox

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 <rsc@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarJoe Tsai <thebrokentoaster@gmail.com>
parent 3a27f28e
......@@ -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))
}
......
......@@ -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
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment