Skip to content

Commit d8e6ed3

Browse files
rogpeppegopherbot
authored andcommitted
time: implement Compare method
Fixes #50770. Change-Id: If0104883bb409ec85827fa5b570f68099ad4fd1d Reviewed-on: https://go-review.googlesource.com/c/go/+/382734 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: hopehook <[email protected]> Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Joseph Tsai <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent b6d5831 commit d8e6ed3

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

api/next/50770.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg time, method (Time) Compare(Time) int #50770

src/time/mono_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ func TestMonotonicAdd(t *testing.T) {
106106
if !now.Before(tn1) {
107107
t.Errorf("Now().Before(Now().Add(1*Hour)) = false, want true")
108108
}
109+
if got, want := now.Compare(tn1), -1; got != want {
110+
t.Errorf("Now().Compare(Now().Add(1*Hour)) = %d, want %d", got, want)
111+
}
112+
if got, want := tn1.Compare(now), 1; got != want {
113+
t.Errorf("Now().Add(1*Hour).Compare(Now()) = %d, want %d", got, want)
114+
}
109115
}
110116

111117
func TestMonotonicSub(t *testing.T) {
@@ -155,7 +161,7 @@ func TestMonotonicSub(t *testing.T) {
155161
sub("t3", "t3", t3, t3w, t3, t3w, 0, 0)
156162

157163
cmp := func(txs, tys string, tx, txw, ty, tyw Time, c, cw int) {
158-
check := func(expr string, b, want bool) {
164+
check := func(expr string, b, want any) {
159165
if b != want {
160166
t.Errorf("%s = %v, want %v", expr, b, want)
161167
}
@@ -174,6 +180,11 @@ func TestMonotonicSub(t *testing.T) {
174180
check(txs+"w.Equal("+tys+")", txw.Equal(ty), cw == 0)
175181
check(txs+".Equal("+tys+"w)", tx.Equal(tyw), cw == 0)
176182
check(txs+"w.Equal("+tys+"w)", txw.Equal(tyw), cw == 0)
183+
184+
check(txs+".Compare("+tys+")", tx.Compare(ty), c)
185+
check(txs+"w.Compare("+tys+")", txw.Compare(ty), cw)
186+
check(txs+".Compare("+tys+"w)", tx.Compare(tyw), cw)
187+
check(txs+"w.Compare("+tys+"w)", txw.Compare(tyw), cw)
177188
}
178189

179190
cmp("t1", "t1", t1, t1w, t1, t1w, 0, 0)
@@ -229,6 +240,12 @@ func TestMonotonicOverflow(t *testing.T) {
229240
if !t2.Before(t1) {
230241
t.Errorf("Now().Add(-5*Second).Before(Now().Add(1*Hour)) = false, want true\nt1=%v\nt2=%v", t1, t2)
231242
}
243+
if got, want := t1.Compare(t2), 1; got != want {
244+
t.Errorf("Now().Add(1*Hour).Compare(Now().Add(-5*Second)) = %d, want %d\nt1=%v\nt2=%v", got, want, t1, t2)
245+
}
246+
if got, want := t2.Compare(t1), -1; got != want {
247+
t.Errorf("Now().Add(-5*Second).Before(Now().Add(1*Hour)) = %d, want %d\nt1=%v\nt2=%v", got, want, t1, t2)
248+
}
232249
}
233250

234251
var monotonicStringTests = []struct {

src/time/time.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// The canonical way to strip a monotonic clock reading is to use t = t.Round(0).
4747
//
4848
// If Times t and u both contain monotonic clock readings, the operations
49-
// t.After(u), t.Before(u), t.Equal(u), and t.Sub(u) are carried out
49+
// t.After(u), t.Before(u), t.Equal(u), t.Compare(u), and t.Sub(u) are carried out
5050
// using the monotonic clock readings alone, ignoring the wall clock
5151
// readings. If either t or u contains no monotonic clock reading, these
5252
// operations fall back to using the wall clock readings.
@@ -266,6 +266,27 @@ func (t Time) Before(u Time) bool {
266266
return ts < us || ts == us && t.nsec() < u.nsec()
267267
}
268268

269+
// Compare compares the time instant t with u. If t is before u, it returns -1;
270+
// if t is after u, it returns +1; if they're the same, it returns 0.
271+
func (t Time) Compare(u Time) int {
272+
var tc, uc int64
273+
if t.wall&u.wall&hasMonotonic != 0 {
274+
tc, uc = t.ext, u.ext
275+
} else {
276+
tc, uc = t.sec(), u.sec()
277+
if tc == uc {
278+
tc, uc = int64(t.nsec()), int64(u.nsec())
279+
}
280+
}
281+
switch {
282+
case tc < uc:
283+
return -1
284+
case tc > uc:
285+
return +1
286+
}
287+
return 0
288+
}
289+
269290
// Equal reports whether t and u represent the same time instant.
270291
// Two times can be equal even if they are in different locations.
271292
// For example, 6:00 +0200 and 4:00 UTC are Equal.

src/time/time_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ var defaultLocTests = []struct {
12821282
{"After", func(t1, t2 Time) bool { return t1.After(t2) == t2.After(t1) }},
12831283
{"Before", func(t1, t2 Time) bool { return t1.Before(t2) == t2.Before(t1) }},
12841284
{"Equal", func(t1, t2 Time) bool { return t1.Equal(t2) == t2.Equal(t1) }},
1285+
{"Compare", func(t1, t2 Time) bool { return t1.Compare(t2) == t2.Compare(t1) }},
12851286

12861287
{"IsZero", func(t1, t2 Time) bool { return t1.IsZero() == t2.IsZero() }},
12871288
{"Date", func(t1, t2 Time) bool {

0 commit comments

Comments
 (0)