time: use names for beginning and end of zone transition times

No functional changes, just more readable code.

LGTM=r
R=golang-codereviews, gobot, r
CC=golang-codereviews
https://golang.org/cl/59240043
This commit is contained in:
Ian Lance Taylor 2014-01-31 17:22:10 -08:00
parent 7e494f8500
commit fabd261fe2
4 changed files with 20 additions and 13 deletions

View File

@ -45,6 +45,13 @@ type zoneTrans struct {
isstd, isutc bool // ignored - no idea what these mean
}
// alpha and omega are the beginning and end of time for zone
// transitions.
const (
alpha = -1 << 63 // math.MinInt64
omega = 1<<63 - 1 // math.MaxInt64
)
// UTC represents Universal Coordinated Time (UTC).
var UTC *Location = &utcLoc
@ -83,9 +90,9 @@ func FixedZone(name string, offset int) *Location {
l := &Location{
name: name,
zone: []zone{{name, offset, false}},
tx: []zoneTrans{{-1 << 63, 0, false, false}},
cacheStart: -1 << 63,
cacheEnd: 1<<63 - 1,
tx: []zoneTrans{{alpha, 0, false, false}},
cacheStart: alpha,
cacheEnd: omega,
}
l.cacheZone = &l.zone[0]
return l
@ -105,8 +112,8 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start
name = "UTC"
offset = 0
isDST = false
start = -1 << 63
end = 1<<63 - 1
start = alpha
end = omega
return
}
@ -124,11 +131,11 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start
name = zone.name
offset = zone.offset
isDST = zone.isDST
start = -1 << 63
start = alpha
if len(l.tx) > 0 {
end = l.tx[0].when
} else {
end = 1<<63 - 1
end = omega
}
return
}
@ -136,7 +143,7 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start
// Binary search for entry with largest time <= sec.
// Not using sort.Search to avoid dependencies.
tx := l.tx
end = 1<<63 - 1
end = omega
lo := 0
hi := len(tx)
for hi-lo > 1 {

View File

@ -100,7 +100,7 @@ func loadZoneDataPlan9(s string) (l *Location, err error) {
for i := range tx {
if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
l.cacheStart = tx[i].when
l.cacheEnd = 1<<63 - 1
l.cacheEnd = omega
if i+1 < len(tx) {
l.cacheEnd = tx[i+1].when
}

View File

@ -173,7 +173,7 @@ func loadZoneData(bytes []byte) (l *Location, err error) {
if len(tx) == 0 {
// Build fake transition to cover all time.
// This happens in fixed locations like "Etc/GMT0".
tx = append(tx, zoneTrans{when: -1 << 63, index: 0})
tx = append(tx, zoneTrans{when: alpha, index: 0})
}
// Committed to succeed.
@ -185,7 +185,7 @@ func loadZoneData(bytes []byte) (l *Location, err error) {
for i := range tx {
if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
l.cacheStart = tx[i].when
l.cacheEnd = 1<<63 - 1
l.cacheEnd = omega
if i+1 < len(tx) {
l.cacheEnd = tx[i+1].when
}

View File

@ -165,8 +165,8 @@ func initLocalFromTZI(i *syscall.Timezoneinformation) {
if nzone == 1 {
// No daylight savings.
std.offset = -int(i.Bias) * 60
l.cacheStart = -1 << 63
l.cacheEnd = 1<<63 - 1
l.cacheStart = alpha
l.cacheEnd = omega
l.cacheZone = std
l.tx = make([]zoneTrans, 1)
l.tx[0].when = l.cacheStart