Commit fabd261f authored by Ian Lance Taylor's avatar Ian Lance Taylor

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
parent 7e494f85
...@@ -45,6 +45,13 @@ type zoneTrans struct { ...@@ -45,6 +45,13 @@ type zoneTrans struct {
isstd, isutc bool // ignored - no idea what these mean 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). // UTC represents Universal Coordinated Time (UTC).
var UTC *Location = &utcLoc var UTC *Location = &utcLoc
...@@ -83,9 +90,9 @@ func FixedZone(name string, offset int) *Location { ...@@ -83,9 +90,9 @@ func FixedZone(name string, offset int) *Location {
l := &Location{ l := &Location{
name: name, name: name,
zone: []zone{{name, offset, false}}, zone: []zone{{name, offset, false}},
tx: []zoneTrans{{-1 << 63, 0, false, false}}, tx: []zoneTrans{{alpha, 0, false, false}},
cacheStart: -1 << 63, cacheStart: alpha,
cacheEnd: 1<<63 - 1, cacheEnd: omega,
} }
l.cacheZone = &l.zone[0] l.cacheZone = &l.zone[0]
return l return l
...@@ -105,8 +112,8 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start ...@@ -105,8 +112,8 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start
name = "UTC" name = "UTC"
offset = 0 offset = 0
isDST = false isDST = false
start = -1 << 63 start = alpha
end = 1<<63 - 1 end = omega
return return
} }
...@@ -124,11 +131,11 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start ...@@ -124,11 +131,11 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start
name = zone.name name = zone.name
offset = zone.offset offset = zone.offset
isDST = zone.isDST isDST = zone.isDST
start = -1 << 63 start = alpha
if len(l.tx) > 0 { if len(l.tx) > 0 {
end = l.tx[0].when end = l.tx[0].when
} else { } else {
end = 1<<63 - 1 end = omega
} }
return return
} }
...@@ -136,7 +143,7 @@ func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start ...@@ -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. // Binary search for entry with largest time <= sec.
// Not using sort.Search to avoid dependencies. // Not using sort.Search to avoid dependencies.
tx := l.tx tx := l.tx
end = 1<<63 - 1 end = omega
lo := 0 lo := 0
hi := len(tx) hi := len(tx)
for hi-lo > 1 { for hi-lo > 1 {
......
...@@ -100,7 +100,7 @@ func loadZoneDataPlan9(s string) (l *Location, err error) { ...@@ -100,7 +100,7 @@ func loadZoneDataPlan9(s string) (l *Location, err error) {
for i := range tx { for i := range tx {
if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) { if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
l.cacheStart = tx[i].when l.cacheStart = tx[i].when
l.cacheEnd = 1<<63 - 1 l.cacheEnd = omega
if i+1 < len(tx) { if i+1 < len(tx) {
l.cacheEnd = tx[i+1].when l.cacheEnd = tx[i+1].when
} }
......
...@@ -173,7 +173,7 @@ func loadZoneData(bytes []byte) (l *Location, err error) { ...@@ -173,7 +173,7 @@ func loadZoneData(bytes []byte) (l *Location, err error) {
if len(tx) == 0 { if len(tx) == 0 {
// Build fake transition to cover all time. // Build fake transition to cover all time.
// This happens in fixed locations like "Etc/GMT0". // 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. // Committed to succeed.
...@@ -185,7 +185,7 @@ func loadZoneData(bytes []byte) (l *Location, err error) { ...@@ -185,7 +185,7 @@ func loadZoneData(bytes []byte) (l *Location, err error) {
for i := range tx { for i := range tx {
if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) { if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
l.cacheStart = tx[i].when l.cacheStart = tx[i].when
l.cacheEnd = 1<<63 - 1 l.cacheEnd = omega
if i+1 < len(tx) { if i+1 < len(tx) {
l.cacheEnd = tx[i+1].when l.cacheEnd = tx[i+1].when
} }
......
...@@ -165,8 +165,8 @@ func initLocalFromTZI(i *syscall.Timezoneinformation) { ...@@ -165,8 +165,8 @@ func initLocalFromTZI(i *syscall.Timezoneinformation) {
if nzone == 1 { if nzone == 1 {
// No daylight savings. // No daylight savings.
std.offset = -int(i.Bias) * 60 std.offset = -int(i.Bias) * 60
l.cacheStart = -1 << 63 l.cacheStart = alpha
l.cacheEnd = 1<<63 - 1 l.cacheEnd = omega
l.cacheZone = std l.cacheZone = std
l.tx = make([]zoneTrans, 1) l.tx = make([]zoneTrans, 1)
l.tx[0].when = l.cacheStart l.tx[0].when = l.cacheStart
......
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