Commit 0e335590 authored by Russ Cox's avatar Russ Cox

time: record monotonic clock reading in time.Now, for more accurate comparisons

See https://golang.org/design/12914-monotonic for details.

Fixes #12914.

Change-Id: I80edc2e6c012b4ace7161c84cf067d444381a009
Reviewed-on: https://go-review.googlesource.com/36255
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarCaleb Spare <cespare@gmail.com>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 8179b9b4
......@@ -94,13 +94,16 @@ func TestRoundTrip(t *testing.T) {
var b bytes.Buffer
tw := NewWriter(&b)
hdr := &Header{
Name: "file.txt",
Uid: 1 << 21, // too big for 8 octal digits
Size: int64(len(data)),
ModTime: time.Now(),
Name: "file.txt",
Uid: 1 << 21, // too big for 8 octal digits
Size: int64(len(data)),
// AddDate to strip monotonic clock reading,
// and Round to discard sub-second precision,
// both of which are not included in the tar header
// and would otherwise break the round-trip check
// below.
ModTime: time.Now().AddDate(0, 0, 0).Round(1 * time.Second),
}
// tar only supports second precision.
hdr.ModTime = hdr.ModTime.Add(-time.Duration(hdr.ModTime.Nanosecond()) * time.Nanosecond)
if err := tw.WriteHeader(hdr); err != nil {
t.Fatalf("tw.WriteHeader: %v", err)
}
......
......@@ -746,7 +746,7 @@ func (i *isZeroBugInterface) GobDecode(data []byte) error {
}
func TestGobEncodeIsZero(t *testing.T) {
x := isZeroBug{time.Now(), "hello", -55, isZeroBugArray{1, 2}, isZeroBugInterface{}}
x := isZeroBug{time.Unix(1e9, 0), "hello", -55, isZeroBugArray{1, 2}, isZeroBugInterface{}}
b := new(bytes.Buffer)
enc := NewEncoder(b)
err := enc.Encode(x)
......
......@@ -207,7 +207,7 @@ func struct_literals() {
_ = time.Time{}
_ = time.Time{sec /* ERROR "unknown field" */ : 0}
_ = time.Time{
0 /* ERROR implicit assignment to unexported field sec in time.Time literal */,
0 /* ERROR implicit assignment to unexported field wall in time.Time literal */,
0 /* ERROR implicit assignment */ ,
nil /* ERROR implicit assignment */ ,
}
......
......@@ -241,8 +241,7 @@ func stackBarrier()
// in asm_*.s
func return0()
//go:linkname time_now time.now
func time_now() (sec int64, nsec int32)
func walltime() (sec int64, nsec int32)
// in asm_*.s
// not called directly; definitions here supply type information for traceback.
......@@ -281,7 +280,7 @@ func prefetcht2(addr uintptr)
func prefetchnta(addr uintptr)
func unixnanotime() int64 {
sec, nsec := time_now()
sec, nsec := walltime()
return sec*1e9 + int64(nsec)
}
......
......@@ -217,8 +217,8 @@ inreg:
ADCL $0, DX
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$0
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$0
CALL runtime·now(SB)
MOVL $1000000000, CX
DIVL CX
......
......@@ -198,8 +198,8 @@ TEXT runtime·nanotime(SB),NOSPLIT,$0-8
MOVQ AX, ret+0(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$0-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$0-12
CALL nanotime<>(SB)
// generated code for
......
......@@ -159,7 +159,7 @@ TEXT runtime·mincore(SB),NOSPLIT,$0
MOVW R0, ret+12(FP)
RET
TEXT time·now(SB), 7, $32
TEXT runtime·walltime(SB), 7, $32
MOVW $8(R13), R0 // timeval
MOVW $0, R1 // zone
MOVW $0, R2 // see issue 16570
......
......@@ -151,7 +151,7 @@ TEXT runtime·setitimer(SB),NOSPLIT,$0
SVC $0x80
RET
TEXT time·now(SB),NOSPLIT,$40-12
TEXT runtime·walltime(SB),NOSPLIT,$40-12
MOVD RSP, R0 // timeval
MOVD R0, R9 // this is how dyld calls gettimeofday
MOVW $0, R1 // zone
......
......@@ -148,8 +148,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-8
SYSCALL
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $232, AX // clock_gettime
MOVQ $0, DI // CLOCK_REALTIME
LEAQ 8(SP), SI
......
......@@ -159,8 +159,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-4
INT $0x80
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $232, AX // clock_gettime
LEAL 12(SP), BX
MOVL $0, 4(SP) // CLOCK_REALTIME
......
......@@ -142,8 +142,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-8
SYSCALL
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $232, AX // clock_gettime
MOVQ $0, DI // CLOCK_REALTIME
LEAQ 8(SP), SI
......
......@@ -166,8 +166,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-8
SWI $0
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW $0, R0 // CLOCK_REALTIME
MOVW $8(R13), R1
MOVW $SYS_clock_gettime, R7
......
......@@ -151,8 +151,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-16
MOVL AX, ret+12(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVL $265, AX // syscall - clock_gettime
MOVL $0, BX // CLOCK_REALTIME
LEAL 8(SP), CX
......
......@@ -135,8 +135,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-28
MOVL AX, ret+24(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
// Be careful. We're calling a function with gcc calling convention here.
// We're guaranteed 128 bytes on entry, and we've taken 16, and the
// call uses another 8.
......
......@@ -197,7 +197,7 @@ TEXT runtime·mincore(SB),NOSPLIT,$0
MOVW R0, ret+12(FP)
RET
TEXT time·now(SB), NOSPLIT, $32
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW $0, R0 // CLOCK_REALTIME
MOVW $8(R13), R1 // timespec
MOVW $SYS_clock_gettime, R7
......
......@@ -182,8 +182,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$-8-28
MOVW R0, ret+24(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$24-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$24-12
MOVW $0, R0 // CLOCK_REALTIME
MOVD RSP, R1
MOVD $SYS_clock_gettime, R8
......
......@@ -172,8 +172,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$-8-28
MOVW R2, ret+24(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVW $0, R4 // CLOCK_REALTIME
MOVV $0(R29), R5
MOVV $SYS_clock_gettime, R2
......
......@@ -175,8 +175,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-16
MOVW R2, ret+12(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
MOVW $0, R4 // CLOCK_REALTIME
MOVW $4(R29), R5
MOVW $SYS_clock_gettime, R2
......
......@@ -157,8 +157,8 @@ TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
MOVW R3, ret+24(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVD $0, R3 // CLOCK_REALTIME
MOVD $0(R1), R4
SYSCALL $SYS_clock_gettime
......
......@@ -169,8 +169,8 @@ TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
MOVW R2, ret+24(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$16
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVW $0, R2 // CLOCK_REALTIME
MOVD $tp-16(SP), R3
MOVW $SYS_clock_gettime, R1
......
......@@ -233,7 +233,7 @@ TEXT runtime·mmap(SB),NOSPLIT,$32
MOVL AX, ret+24(FP)
RET
TEXT time·now(SB),NOSPLIT,$20
TEXT runtime·walltime(SB),NOSPLIT,$20
MOVL $0, 0(SP) // real time clock
LEAL 8(SP), AX
MOVL AX, 4(SP) // timespec
......@@ -249,7 +249,7 @@ TEXT time·now(SB),NOSPLIT,$20
RET
TEXT syscall·now(SB),NOSPLIT,$0
JMP time·now(SB)
JMP runtime·walltime(SB)
TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$8
MOVL arg1+0(FP), AX
......
......@@ -242,7 +242,7 @@ TEXT runtime·mmap(SB),NOSPLIT,$8
MOVL AX, ret+24(FP)
RET
TEXT time·now(SB),NOSPLIT,$16
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVQ runtime·faketime(SB), AX
CMPQ AX, $0
JEQ realtime
......@@ -268,7 +268,7 @@ realtime:
RET
TEXT syscall·now(SB),NOSPLIT,$0
JMP time·now(SB)
JMP runtime·walltime(SB)
TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$0
MOVL arg1+0(FP), DI
......
......@@ -196,7 +196,7 @@ TEXT runtime·mmap(SB),NOSPLIT,$8
MOVW R0, ret+24(FP)
RET
TEXT time·now(SB),NOSPLIT,$16
TEXT runtime·walltime(SB),NOSPLIT,$16
MOVW $0, R0 // real time clock
MOVW $4(R13), R1
NACL_SYSCALL(SYS_clock_gettime)
......@@ -209,7 +209,7 @@ TEXT time·now(SB),NOSPLIT,$16
RET
TEXT syscall·now(SB),NOSPLIT,$0
B time·now(SB)
B runtime·walltime(SB)
TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$0
MOVW arg1+0(FP), R0
......
......@@ -134,8 +134,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-4
INT $0x80
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
LEAL 12(SP), BX
MOVL $0, 4(SP) // arg 1 - clock_id
MOVL BX, 8(SP) // arg 2 - tp
......
......@@ -169,8 +169,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-8
SYSCALL
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVQ $0, DI // arg 1 - clock_id
LEAQ 8(SP), SI // arg 2 - tp
MOVL $427, AX // sys_clock_gettime
......
......@@ -137,8 +137,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-4
SWI $0xa001a9 // sys_setitimer
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW $0, R0 // CLOCK_REALTIME
MOVW $8(R13), R1
SWI $0xa001ab // clock_gettime
......
......@@ -140,8 +140,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-4
INT $0x80
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
LEAL 12(SP), BX
MOVL $0, 4(SP) // arg 1 - clock_id
MOVL BX, 8(SP) // arg 2 - tp
......
......@@ -180,8 +180,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$-8
SYSCALL
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVQ $0, DI // arg 1 - clock_id
LEAQ 8(SP), SI // arg 2 - tp
MOVL $87, AX // sys_clock_gettime
......
......@@ -150,8 +150,8 @@ TEXT runtime·setitimer(SB),NOSPLIT,$0
SWI $0
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB), NOSPLIT, $32
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB), NOSPLIT, $32
MOVW CLOCK_REALTIME, R0 // arg 1 - clock_id
MOVW $8(R13), R1 // arg 2 - tp
MOVW $87, R12 // sys_clock_gettime
......
......@@ -102,8 +102,8 @@ TEXT runtime·nsec(SB),NOSPLIT,$8
MOVL $-1, ret_hi+8(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·nanotime(SB)
MOVL 0(SP), AX
MOVL 4(SP), DX
......
......@@ -92,8 +92,8 @@ TEXT runtime·nsec(SB),NOSPLIT,$0
MOVQ AX, ret+8(FP)
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·nanotime(SB)
MOVQ 0(SP), AX
......
......@@ -139,7 +139,7 @@ TEXT runtime·nsec(SB),NOSPLIT,$-4-12
RET
// time.now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$12-12
TEXT runtime·walltime(SB),NOSPLIT,$12-12
// use nsec system call to get current time in nanoseconds
MOVW $sysnsec_lo-8(SP), R0 // destination addr
MOVW R0,res-12(SP)
......
......@@ -354,8 +354,8 @@ TEXT runtime·osyield1(SB),NOSPLIT,$0
CALL AX
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·nanotime(SB)
MOVQ 0(SP), AX
......
......@@ -432,8 +432,8 @@ TEXT runtime·switchtothread(SB),NOSPLIT,$0
MOVL BP, SP
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·unixnano(SB)
MOVL 0(SP), AX
MOVL 4(SP), DX
......
......@@ -465,8 +465,8 @@ TEXT runtime·switchtothread(SB),NOSPLIT|NOFRAME,$0
MOVQ 32(SP), SP
RET
// func now() (sec int64, nsec int32)
TEXT time·now(SB),NOSPLIT,$8-12
// func walltime() (sec int64, nsec int32)
TEXT runtime·walltime(SB),NOSPLIT,$8-12
CALL runtime·unixnano(SB)
MOVQ 0(SP), AX
......
......@@ -301,3 +301,11 @@ func net_runtimeNano() int64 {
func time_runtimeNano() int64 {
return nanotime()
}
var startNano = nanotime()
//go:linkname time_now time.now
func time_now() (sec int64, nsec int32, mono uint64) {
sec, nsec = walltime()
return sec, nsec, uint64(nanotime() - startNano + 1)
}
......@@ -30,4 +30,6 @@ func ResetZoneinfoForTesting() {
var (
ForceZipFileForTesting = forceZipFileForTesting
ParseTimeZone = parseTimeZone
SetMono = (*Time).setMono
GetMono = (*Time).mono
)
......@@ -424,8 +424,41 @@ func formatNano(b []byte, nanosec uint, n int, trim bool) []byte {
// String returns the time formatted using the format string
// "2006-01-02 15:04:05.999999999 -0700 MST"
//
// If the time has a monotonic clock reading, the returned string
// includes a final field "m±<value>", where value is the monotonic
// clock reading formatted as a decimal number of seconds.
func (t Time) String() string {
return t.Format("2006-01-02 15:04:05.999999999 -0700 MST")
s := t.Format("2006-01-02 15:04:05.999999999 -0700 MST")
// Format monotonic clock reading as m=±ddd.nnnnnnnnn.
if t.wall&hasMonotonic != 0 {
m2 := t.ext
m1, m2 := m2/1e9, m2%1e9
if m2 < 0 {
m2 += 1e9
m1--
}
sign := byte('+')
if m1 < 0 {
sign = '-'
m1 = -m1
}
m0, m1 := m1/1e9, m1%1e9
var buf []byte
buf = append(buf, " m="...)
buf = append(buf, sign)
wid := 0
if m0 != 0 {
buf = appendInt(buf, int(m0), 0)
wid = 9
}
buf = appendInt(buf, int(m1), wid)
buf = append(buf, '.')
buf = appendInt(buf, int(m2), 9)
s += string(buf)
}
return s
}
// Format returns a textual representation of the time value formatted
......@@ -1022,11 +1055,11 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
if zoneOffset != -1 {
t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
t.sec -= int64(zoneOffset)
t.addSec(-int64(zoneOffset))
// Look for local zone with the given offset.
// If that zone was in effect at the given time, use it.
name, offset, _, _, _ := local.lookup(t.sec + internalToUnix)
name, offset, _, _, _ := local.lookup(t.unixSec())
if offset == zoneOffset && (zoneName == "" || name == zoneName) {
t.setLoc(local)
return t, nil
......@@ -1041,9 +1074,9 @@ func parse(layout, value string, defaultLocation, local *Location) (Time, error)
t := Date(year, Month(month), day, hour, min, sec, nsec, UTC)
// Look for local zone with the given offset.
// If that zone was in effect at the given time, use it.
offset, _, ok := local.lookupName(zoneName, t.sec+internalToUnix)
offset, _, ok := local.lookupName(zoneName, t.unixSec())
if ok {
t.sec -= int64(offset)
t.addSec(-int64(offset))
t.setLoc(local)
return t, nil
}
......
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package time_test
import (
"regexp"
"testing"
. "time"
)
func TestHasMonotonicClock(t *testing.T) {
yes := func(expr string, tt Time) {
if GetMono(&tt) == 0 {
t.Errorf("%s: missing monotonic clock reading", expr)
}
}
no := func(expr string, tt Time) {
if GetMono(&tt) != 0 {
t.Errorf("%s: unexpected monotonic clock reading", expr)
}
}
yes("<-After(1)", <-After(1))
ticker := NewTicker(1)
yes("<-Tick(1)", <-ticker.C)
ticker.Stop()
no("Date(2009, 11, 23, 0, 0, 0, 0, UTC)", Date(2009, 11, 23, 0, 0, 0, 0, UTC))
tp, _ := Parse(UnixDate, "Sat Mar 7 11:06:39 PST 2015")
no(`Parse(UnixDate, "Sat Mar 7 11:06:39 PST 2015")`, tp)
no("Unix(1486057371, 0)", Unix(1486057371, 0))
yes("Now()", Now())
tu := Unix(1486057371, 0)
tm := tu
SetMono(&tm, 123456)
no("tu", tu)
yes("tm", tm)
no("tu.Add(1)", tu.Add(1))
no("tu.In(UTC)", tu.In(UTC))
no("tu.AddDate(1, 1, 1)", tu.AddDate(1, 1, 1))
no("tu.AddDate(0, 0, 0)", tu.AddDate(0, 0, 0))
no("tu.Local()", tu.Local())
no("tu.UTC()", tu.UTC())
no("tu.Round(2)", tu.Round(2))
no("tu.Truncate(2)", tu.Truncate(2))
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())
yes("tm.Round(2)", tm.Round(2))
yes("tm.Truncate(2)", tm.Truncate(2))
}
func TestMonotonicAdd(t *testing.T) {
tm := Unix(1486057371, 123456)
SetMono(&tm, 123456789012345)
t2 := tm.Add(1e8)
if t2.Nanosecond() != 100123456 {
t.Errorf("t2.Nanosecond() = %d, want 100123456", t2.Nanosecond())
}
if GetMono(&t2) != 123456889012345 {
t.Errorf("t2.mono = %d, want 123456889012345", GetMono(&t2))
}
t3 := tm.Add(-9e18) // wall now out of range
if t3.Nanosecond() != 123456 {
t.Errorf("t3.Nanosecond() = %d, want 123456", t3.Nanosecond())
}
if GetMono(&t3) != 0 {
t.Errorf("t3.mono = %d, want 0 (wall time out of range for monotonic reading)", GetMono(&t3))
}
t4 := tm.Add(+9e18) // wall now out of range
if t4.Nanosecond() != 123456 {
t.Errorf("t4.Nanosecond() = %d, want 123456", t4.Nanosecond())
}
if GetMono(&t4) != 0 {
t.Errorf("t4.mono = %d, want 0 (wall time out of range for monotonic reading)", GetMono(&t4))
}
tn := Now()
tn1 := tn.Add(1 * Hour)
Sleep(100 * Millisecond)
d := Until(tn1)
if d < 59*Minute {
t.Errorf("Until(Now().Add(1*Hour)) = %v, wanted at least 59m", d)
}
now := Now()
if now.After(tn1) {
t.Errorf("Now().After(Now().Add(1*Hour)) = true, want false")
}
if !tn1.After(now) {
t.Errorf("Now().Add(1*Hour).After(now) = false, want true")
}
if tn1.Before(now) {
t.Errorf("Now().Add(1*Hour).Before(Now()) = true, want false")
}
if !now.Before(tn1) {
t.Errorf("Now().Before(Now().Add(1*Hour)) = false, want true")
}
}
func TestMonotonicSub(t *testing.T) {
t1 := Unix(1483228799, 995e6)
SetMono(&t1, 123456789012345)
t2 := Unix(1483228799, 5e6)
SetMono(&t2, 123456789012345+10e6)
t3 := Unix(1483228799, 995e6)
SetMono(&t3, 123456789012345+1e9)
t1w := t1.AddDate(0, 0, 0)
if GetMono(&t1w) != 0 {
t.Fatalf("AddDate didn't strip monotonic clock reading")
}
t2w := t2.AddDate(0, 0, 0)
if GetMono(&t2w) != 0 {
t.Fatalf("AddDate didn't strip monotonic clock reading")
}
t3w := t3.AddDate(0, 0, 0)
if GetMono(&t3w) != 0 {
t.Fatalf("AddDate didn't strip monotonic clock reading")
}
sub := func(txs, tys string, tx, txw, ty, tyw Time, d, dw Duration) {
check := func(expr string, d, want Duration) {
if d != want {
t.Errorf("%s = %v, want %v", expr, d, want)
}
}
check(txs+".Sub("+tys+")", tx.Sub(ty), d)
check(txs+"w.Sub("+tys+")", txw.Sub(ty), dw)
check(txs+".Sub("+tys+"w)", tx.Sub(tyw), dw)
check(txs+"w.Sub("+tys+"w)", txw.Sub(tyw), dw)
}
sub("t1", "t1", t1, t1w, t1, t1w, 0, 0)
sub("t1", "t2", t1, t1w, t2, t2w, -10*Millisecond, 990*Millisecond)
sub("t1", "t3", t1, t1w, t3, t3w, -1000*Millisecond, 0)
sub("t2", "t1", t2, t2w, t1, t1w, 10*Millisecond, -990*Millisecond)
sub("t2", "t2", t2, t2w, t2, t2w, 0, 0)
sub("t2", "t3", t2, t2w, t3, t3w, -990*Millisecond, -990*Millisecond)
sub("t3", "t1", t3, t3w, t1, t1w, 1000*Millisecond, 0)
sub("t3", "t2", t3, t3w, t2, t2w, 990*Millisecond, 990*Millisecond)
sub("t3", "t3", t3, t3w, t3, t3w, 0, 0)
cmp := func(txs, tys string, tx, txw, ty, tyw Time, c, cw int) {
check := func(expr string, b, want bool) {
if b != want {
t.Errorf("%s = %v, want %v", expr, b, want)
}
}
check(txs+".After("+tys+")", tx.After(ty), c > 0)
check(txs+"w.After("+tys+")", txw.After(ty), cw > 0)
check(txs+".After("+tys+"w)", tx.After(tyw), cw > 0)
check(txs+"w.After("+tys+"w)", txw.After(tyw), cw > 0)
check(txs+".Before("+tys+")", tx.Before(ty), c < 0)
check(txs+"w.Before("+tys+")", txw.Before(ty), cw < 0)
check(txs+".Before("+tys+"w)", tx.Before(tyw), cw < 0)
check(txs+"w.Before("+tys+"w)", txw.Before(tyw), cw < 0)
check(txs+".Equal("+tys+")", tx.Equal(ty), c == 0)
check(txs+"w.Equal("+tys+")", txw.Equal(ty), cw == 0)
check(txs+".Equal("+tys+"w)", tx.Equal(tyw), cw == 0)
check(txs+"w.Equal("+tys+"w)", txw.Equal(tyw), cw == 0)
}
cmp("t1", "t1", t1, t1w, t1, t1w, 0, 0)
cmp("t1", "t2", t1, t1w, t2, t2w, -1, +1)
cmp("t1", "t3", t1, t1w, t3, t3w, -1, 0)
cmp("t2", "t1", t2, t2w, t1, t1w, +1, -1)
cmp("t2", "t2", t2, t2w, t2, t2w, 0, 0)
cmp("t2", "t3", t2, t2w, t3, t3w, -1, -1)
cmp("t3", "t1", t3, t3w, t1, t1w, +1, 0)
cmp("t3", "t2", t3, t3w, t2, t2w, +1, +1)
cmp("t3", "t3", t3, t3w, t3, t3w, 0, 0)
}
func TestMonotonicOverflow(t *testing.T) {
t1 := Now().Add(-30 * Second)
d := Until(t1)
if d < -35*Second || -30*Second < d {
t.Errorf("Until(Now().Add(-30s)) = %v, want roughly -30s (-35s to -30s)", d)
}
t1 = Now().Add(30 * Second)
d = Until(t1)
if d < 25*Second || 30*Second < d {
t.Errorf("Until(Now().Add(-30s)) = %v, want roughly 30s (25s to 30s)", d)
}
t0 := Now()
t1 = t0.Add(Duration(1<<63 - 1))
if GetMono(&t1) != 0 {
t.Errorf("Now().Add(maxDuration) has monotonic clock reading (%v => %v %d %d)", t0.String(), t1.String(), t0.Unix(), t1.Unix())
}
t2 := t1.Add(-Duration(1<<63 - 1))
d = Since(t2)
if d < -10*Second || 10*Second < d {
t.Errorf("Since(Now().Add(max).Add(-max)) = %v, want [-10s, 10s]", d)
}
t0 = Now()
t1 = t0.Add(1 * Hour)
Sleep(100 * Millisecond)
t2 = Now().Add(-5 * Second)
if !t1.After(t2) {
t.Errorf("Now().Add(1*Hour).After(Now().Add(-5*Second)) = false, want true\nt1=%v\nt2=%v", t1, t2)
}
if t2.After(t1) {
t.Errorf("Now().Add(-5*Second).After(Now().Add(1*Hour)) = true, want false\nt1=%v\nt2=%v", t1, t2)
}
if t1.Before(t2) {
t.Errorf("Now().Add(1*Hour).Before(Now().Add(-5*Second)) = true, want false\nt1=%v\nt2=%v", t1, t2)
}
if !t2.Before(t1) {
t.Errorf("Now().Add(-5*Second).Before(Now().Add(1*Hour)) = false, want true\nt1=%v\nt2=%v", t1, t2)
}
}
func TestMonotonicString(t *testing.T) {
t1 := Now()
re := regexp.MustCompile(` m=\+[0-9]+\.[0-9]{9}$`)
if !re.MatchString(t1.String()) {
t.Errorf("Now().String() = %q, want match for /%s/", t1.String(), re)
}
t2 := Now().Add(-5 * Hour)
re = regexp.MustCompile(` m=-[0-9]+\.[0-9]{9}$`)
if !re.MatchString(t2.String()) {
t.Errorf("Now().Add(-5*Hour).String() = %q, want match for /%s/", t2.String(), re)
}
t3 := Now().Add(1.2e18)
re = regexp.MustCompile(` m=\+120[0-9]{7}\.[0-9]{9}$`)
if !re.MatchString(t3.String()) {
t.Errorf("Now().Add(12e17).String() = %q, want match for /%s/", t3.String(), re)
}
}
This diff is collapsed.
......@@ -95,7 +95,7 @@ func loadZoneDataPlan9(s string) (l *Location, err error) {
// Fill in the cache with information about right now,
// since that will be the most common lookup.
sec, _ := now()
sec, _, _ := now()
for i := range tx {
if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
l.cacheStart = tx[i].when
......
......@@ -188,7 +188,7 @@ func loadZoneData(bytes []byte) (l *Location, err error) {
// Fill in the cache with information about right now,
// since that will be the most common lookup.
sec, _ := now()
sec, _, _ := now()
for i := range tx {
if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
l.cacheStart = tx[i].when
......
......@@ -132,7 +132,7 @@ func pseudoUnix(year int, d *syscall.Systemtime) int64 {
day -= 7
}
}
return t.sec + int64(day-1)*secondsPerDay + internalToUnix
return t.sec() + int64(day-1)*secondsPerDay + internalToUnix
}
func initLocalFromTZI(i *syscall.Timezoneinformation) {
......
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