Skip to content

Commit 37b5a51

Browse files
committed
time: revise for go1.12
Update #3
1 parent 109ad9c commit 37b5a51

File tree

7 files changed

+72
-20
lines changed

7 files changed

+72
-20
lines changed

gosrc/1.11.5/time/format.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,8 @@ func parseTimeZone(value string) (length int, ok bool) {
11201120
// Special Case 3: Some time zones are not named, but have +/-00 format
11211121
if value[0] == '+' || value[0] == '-' {
11221122
length = parseSignedOffset(value)
1123-
return length, true
1123+
ok := length > 0 // parseSignedOffset returns 0 in case of bad input
1124+
return length, ok
11241125
}
11251126
// How many upper-case letters are there? Need at least three, at most five.
11261127
var nUpper int
@@ -1152,7 +1153,7 @@ func parseTimeZone(value string) (length int, ok bool) {
11521153

11531154
// parseGMT parses a GMT time zone. The input string is known to start "GMT".
11541155
// The function checks whether that is followed by a sign and a number in the
1155-
// range -14 through 12 excluding zero.
1156+
// range -23 through +23 excluding zero.
11561157
func parseGMT(value string) int {
11571158
value = value[3:]
11581159
if len(value) == 0 {
@@ -1163,21 +1164,23 @@ func parseGMT(value string) int {
11631164
}
11641165

11651166
// parseSignedOffset parses a signed timezone offset (e.g. "+03" or "-04").
1166-
// The function checks for a signed number in the range -14 through +12 excluding zero.
1167+
// The function checks for a signed number in the range -23 through +23 excluding zero.
11671168
// Returns length of the found offset string or 0 otherwise
11681169
func parseSignedOffset(value string) int {
11691170
sign := value[0]
11701171
if sign != '-' && sign != '+' {
11711172
return 0
11721173
}
11731174
x, rem, err := leadingInt(value[1:])
1174-
if err != nil {
1175+
1176+
// fail if nothing consumed by leadingInt
1177+
if err != nil || value[1:] == rem {
11751178
return 0
11761179
}
11771180
if sign == '-' {
11781181
x = -x
11791182
}
1180-
if x == 0 || x < -14 || 12 < x {
1183+
if x < -23 || 23 < x {
11811184
return 0
11821185
}
11831186
return len(value) - len(rem)

gosrc/1.11.5/time/sleep.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ package time
88
// A negative or zero duration causes Sleep to return immediately.
99
func Sleep(d Duration)
1010

11-
// runtimeNano returns the current value of the runtime clock in nanoseconds.
12-
func runtimeNano() int64
13-
1411
// Interface to timers implemented in package runtime.
1512
// Must be in sync with ../runtime/time.go:/^type timer
1613
type runtimeTimer struct {

gosrc/1.11.5/time/sys_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
5+
// +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris
66

77
package time
88

gosrc/1.11.5/time/time.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@
7575
//
7676
package time
7777

78-
import "errors"
78+
import (
79+
"errors"
80+
_ "unsafe" // for go:linkname
81+
)
7982

8083
// A Time represents an instant in time with nanosecond precision.
8184
//
@@ -102,6 +105,10 @@ import "errors"
102105
// change the instant in time being denoted and therefore does not affect the
103106
// computations described in earlier paragraphs.
104107
//
108+
// Representations of a Time value saved by the GobEncode, MarshalBinary,
109+
// MarshalJSON, and MarshalText methods store the Time.Location's offset, but not
110+
// the location name. They therefore lose information about Daylight Saving Time.
111+
//
105112
// In addition to the required “wall clock” reading, a Time may contain an optional
106113
// reading of the current process's monotonic clock, to provide additional precision
107114
// for comparison or subtraction.
@@ -908,13 +915,27 @@ func (t Time) Sub(u Time) Duration {
908915
// Since returns the time elapsed since t.
909916
// It is shorthand for time.Now().Sub(t).
910917
func Since(t Time) Duration {
911-
return Now().Sub(t)
918+
var now Time
919+
if t.wall&hasMonotonic != 0 {
920+
// Common case optimization: if t has monotomic time, then Sub will use only it.
921+
now = Time{hasMonotonic, runtimeNano() - startNano, nil}
922+
} else {
923+
now = Now()
924+
}
925+
return now.Sub(t)
912926
}
913927

914928
// Until returns the duration until t.
915929
// It is shorthand for t.Sub(time.Now()).
916930
func Until(t Time) Duration {
917-
return t.Sub(Now())
931+
var now Time
932+
if t.wall&hasMonotonic != 0 {
933+
// Common case optimization: if t has monotomic time, then Sub will use only it.
934+
now = Time{hasMonotonic, runtimeNano() - startNano, nil}
935+
} else {
936+
now = Now()
937+
}
938+
return t.Sub(now)
918939
}
919940

920941
// AddDate returns the time corresponding to adding the
@@ -933,7 +954,7 @@ func (t Time) AddDate(years int, months int, days int) Time {
933954

934955
const (
935956
secondsPerMinute = 60
936-
secondsPerHour = 60 * 60
957+
secondsPerHour = 60 * secondsPerMinute
937958
secondsPerDay = 24 * secondsPerHour
938959
secondsPerWeek = 7 * secondsPerDay
939960
daysPer400Years = 365*400 + 97
@@ -1050,9 +1071,22 @@ func daysIn(m Month, year int) int {
10501071
// Provided by package runtime.
10511072
func now() (sec int64, nsec int32, mono int64)
10521073

1074+
// runtimeNano returns the current value of the runtime clock in nanoseconds.
1075+
//go:linkname runtimeNano runtime.nanotime
1076+
func runtimeNano() int64
1077+
1078+
// Monotonic times are reported as offsets from startNano.
1079+
// We initialize startNano to runtimeNano() - 1 so that on systems where
1080+
// monotonic time resolution is fairly low (e.g. Windows 2008
1081+
// which appears to have a default resolution of 15ms),
1082+
// we avoid ever reporting a monotonic time of 0.
1083+
// (Callers may want to use 0 as "time not set".)
1084+
var startNano int64 = runtimeNano() - 1
1085+
10531086
// Now returns the current local time.
10541087
func Now() Time {
10551088
sec, nsec, mono := now()
1089+
mono -= startNano
10561090
sec += unixToInternal - minWall
10571091
if uint64(sec)>>33 != 0 {
10581092
return Time{uint64(nsec), sec + minWall, Local}
@@ -1076,7 +1110,7 @@ func (t Time) Local() Time {
10761110
return t
10771111
}
10781112

1079-
// In returns a copy of t representating the same time instant, but
1113+
// In returns a copy of t representing the same time instant, but
10801114
// with the copy's location information set to loc for display
10811115
// purposes.
10821116
//

gosrc/1.11.5/time/zoneinfo.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (l *Location) lookupFirstZone() int {
205205
return 0
206206
}
207207

208-
// firstZoneUsed returns whether the first zone is used by some
208+
// firstZoneUsed reports whether the first zone is used by some
209209
// transition.
210210
func (l *Location) firstZoneUsed() bool {
211211
for _, tx := range l.tx {
@@ -288,14 +288,23 @@ func LoadLocation(name string) (*Location, error) {
288288
env, _ := syscall.Getenv("ZONEINFO")
289289
zoneinfo = &env
290290
})
291+
var firstErr error
291292
if *zoneinfo != "" {
292293
if zoneData, err := loadTzinfoFromDirOrZip(*zoneinfo, name); err == nil {
293294
if z, err := LoadLocationFromTZData(name, zoneData); err == nil {
294295
return z, nil
295296
}
297+
firstErr = err
298+
} else if err != syscall.ENOENT {
299+
firstErr = err
296300
}
297301
}
298-
return loadLocation(name, zoneSources)
302+
if z, err := loadLocation(name, zoneSources); err == nil {
303+
return z, nil
304+
} else if firstErr == nil {
305+
firstErr = err
306+
}
307+
return nil, firstErr
299308
}
300309

301310
// containsDotDot reports whether s contains "..".

gosrc/1.11.5/time/zoneinfo_read.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package time
1111

1212
import (
1313
"errors"
14+
"runtime"
1415
"syscall"
1516
)
1617

@@ -55,7 +56,7 @@ func (d *dataIO) big4() (n uint32, ok bool) {
5556
d.error = true
5657
return 0, false
5758
}
58-
return uint32(p[0])<<24 | uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]), true
59+
return uint32(p[3]) | uint32(p[2])<<8 | uint32(p[1])<<16 | uint32(p[0])<<24, true
5960
}
6061

6162
func (d *dataIO) byte() (n byte, ok bool) {
@@ -172,6 +173,14 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
172173
return nil, badData
173174
}
174175
zone[i].name = byteString(abbrev[b:])
176+
if runtime.GOOS == "aix" && len(name) > 8 && (name[:8] == "Etc/GMT+" || name[:8] == "Etc/GMT-") {
177+
// There is a bug with AIX 7.2 TL 0 with files in Etc,
178+
// GMT+1 will return GMT-1 instead of GMT+1 or -01.
179+
if name != "Etc/GMT+0" {
180+
// GMT+0 is OK
181+
zone[i].name = name[4:]
182+
}
183+
}
175184
}
176185

177186
// Now the transition time info.
@@ -262,7 +271,7 @@ func get2(b []byte) int {
262271
func loadTzinfoFromZip(zipfile, name string) ([]byte, error) {
263272
fd, err := open(zipfile)
264273
if err != nil {
265-
return nil, errors.New("open " + zipfile + ": " + err.Error())
274+
return nil, err
266275
}
267276
defer closefd(fd)
268277

@@ -364,7 +373,7 @@ func loadTzinfoFromZip(zipfile, name string) ([]byte, error) {
364373
return buf, nil
365374
}
366375

367-
return nil, errors.New("cannot find " + name + " in zip file " + zipfile)
376+
return nil, syscall.ENOENT
368377
}
369378

370379
// loadTzinfoFromTzdata returns the time zone information of the time zone

gosrc/1.11.5/time/zoneinfo_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build darwin,386 darwin,amd64 dragonfly freebsd js,wasm linux,!android nacl netbsd openbsd solaris
5+
// +build aix darwin,386 darwin,amd64 dragonfly freebsd linux,!android nacl netbsd openbsd solaris
66

77
// Parse "zoneinfo" time zone file.
88
// This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.

0 commit comments

Comments
 (0)