2011-12-13 20:16:27 +01:00
|
|
|
// Copyright 2011 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
|
|
|
|
|
2012-03-02 21:01:37 +01:00
|
|
|
import (
|
2017-09-14 19:11:35 +02:00
|
|
|
"errors"
|
2012-03-02 21:01:37 +01:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
)
|
2011-12-13 20:16:27 +01:00
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
//go:generate env ZONEINFO=$GOROOT/lib/time/zoneinfo.zip go run genzabbrs.go -output zoneinfo_abbrs_windows.go
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// A Location maps time instants to the zone in use at that time.
|
|
|
|
// Typically, the Location represents the collection of time offsets
|
|
|
|
// in use in a geographical area, such as CEST and CET for central Europe.
|
|
|
|
type Location struct {
|
|
|
|
name string
|
|
|
|
zone []zone
|
|
|
|
tx []zoneTrans
|
|
|
|
|
|
|
|
// Most lookups will be for the current time.
|
|
|
|
// To avoid the binary search through tx, keep a
|
|
|
|
// static one-element cache that gives the correct
|
|
|
|
// zone for the time when the Location was created.
|
2016-02-03 22:58:02 +01:00
|
|
|
// if cacheStart <= t < cacheEnd,
|
2011-12-13 20:16:27 +01:00
|
|
|
// lookup can return cacheZone.
|
|
|
|
// The units for cacheStart and cacheEnd are seconds
|
|
|
|
// since January 1, 1970 UTC, to match the argument
|
|
|
|
// to lookup.
|
|
|
|
cacheStart int64
|
|
|
|
cacheEnd int64
|
|
|
|
cacheZone *zone
|
|
|
|
}
|
|
|
|
|
|
|
|
// A zone represents a single time zone such as CEST or CET.
|
|
|
|
type zone struct {
|
|
|
|
name string // abbreviated name, "CET"
|
|
|
|
offset int // seconds east of UTC
|
|
|
|
isDST bool // is this zone Daylight Savings Time?
|
|
|
|
}
|
|
|
|
|
|
|
|
// A zoneTrans represents a single time zone transition.
|
|
|
|
type zoneTrans struct {
|
|
|
|
when int64 // transition time, in seconds since 1970 GMT
|
|
|
|
index uint8 // the index of the zone that goes into effect at that time
|
|
|
|
isstd, isutc bool // ignored - no idea what these mean
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// UTC represents Universal Coordinated Time (UTC).
|
|
|
|
var UTC *Location = &utcLoc
|
|
|
|
|
|
|
|
// utcLoc is separate so that get can refer to &utcLoc
|
|
|
|
// and ensure that it never returns a nil *Location,
|
|
|
|
// even if a badly behaved client has changed UTC.
|
|
|
|
var utcLoc = Location{name: "UTC"}
|
|
|
|
|
|
|
|
// Local represents the system's local time zone.
|
|
|
|
var Local *Location = &localLoc
|
|
|
|
|
|
|
|
// localLoc is separate so that initLocal can initialize
|
|
|
|
// it even if a client has changed Local.
|
|
|
|
var localLoc Location
|
|
|
|
var localOnce sync.Once
|
|
|
|
|
|
|
|
func (l *Location) get() *Location {
|
|
|
|
if l == nil {
|
|
|
|
return &utcLoc
|
|
|
|
}
|
|
|
|
if l == &localLoc {
|
|
|
|
localOnce.Do(initLocal)
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a descriptive name for the time zone information,
|
2017-09-14 19:11:35 +02:00
|
|
|
// corresponding to the name argument to LoadLocation or FixedZone.
|
2011-12-13 20:16:27 +01:00
|
|
|
func (l *Location) String() string {
|
|
|
|
return l.get().name
|
|
|
|
}
|
|
|
|
|
|
|
|
// FixedZone returns a Location that always uses
|
|
|
|
// the given zone name and offset (seconds east of UTC).
|
|
|
|
func FixedZone(name string, offset int) *Location {
|
|
|
|
l := &Location{
|
|
|
|
name: name,
|
|
|
|
zone: []zone{{name, offset, false}},
|
2014-06-07 00:37:27 +02:00
|
|
|
tx: []zoneTrans{{alpha, 0, false, false}},
|
|
|
|
cacheStart: alpha,
|
|
|
|
cacheEnd: omega,
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
|
|
|
l.cacheZone = &l.zone[0]
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup returns information about the time zone in use at an
|
|
|
|
// instant in time expressed as seconds since January 1, 1970 00:00:00 UTC.
|
|
|
|
//
|
|
|
|
// The returned information gives the name of the zone (such as "CET"),
|
|
|
|
// the start and end times bracketing sec when that zone is in effect,
|
|
|
|
// the offset in seconds east of UTC (such as -5*60*60), and whether
|
|
|
|
// the daylight savings is being observed at that time.
|
2018-09-24 23:46:21 +02:00
|
|
|
func (l *Location) lookup(sec int64) (name string, offset int, start, end int64) {
|
2011-12-13 20:16:27 +01:00
|
|
|
l = l.get()
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if len(l.zone) == 0 {
|
2011-12-13 20:16:27 +01:00
|
|
|
name = "UTC"
|
|
|
|
offset = 0
|
2014-06-07 00:37:27 +02:00
|
|
|
start = alpha
|
|
|
|
end = omega
|
2011-12-13 20:16:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if zone := l.cacheZone; zone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
|
|
|
|
name = zone.name
|
|
|
|
offset = zone.offset
|
|
|
|
start = l.cacheStart
|
|
|
|
end = l.cacheEnd
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if len(l.tx) == 0 || sec < l.tx[0].when {
|
|
|
|
zone := &l.zone[l.lookupFirstZone()]
|
|
|
|
name = zone.name
|
|
|
|
offset = zone.offset
|
|
|
|
start = alpha
|
|
|
|
if len(l.tx) > 0 {
|
|
|
|
end = l.tx[0].when
|
|
|
|
} else {
|
|
|
|
end = omega
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// Binary search for entry with largest time <= sec.
|
|
|
|
// Not using sort.Search to avoid dependencies.
|
|
|
|
tx := l.tx
|
2014-06-07 00:37:27 +02:00
|
|
|
end = omega
|
2012-10-23 06:31:11 +02:00
|
|
|
lo := 0
|
|
|
|
hi := len(tx)
|
|
|
|
for hi-lo > 1 {
|
|
|
|
m := lo + (hi-lo)/2
|
2011-12-13 20:16:27 +01:00
|
|
|
lim := tx[m].when
|
|
|
|
if sec < lim {
|
|
|
|
end = lim
|
2012-10-23 06:31:11 +02:00
|
|
|
hi = m
|
2011-12-13 20:16:27 +01:00
|
|
|
} else {
|
2012-10-23 06:31:11 +02:00
|
|
|
lo = m
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
zone := &l.zone[tx[lo].index]
|
2011-12-13 20:16:27 +01:00
|
|
|
name = zone.name
|
|
|
|
offset = zone.offset
|
2012-10-23 06:31:11 +02:00
|
|
|
start = tx[lo].when
|
2011-12-13 20:16:27 +01:00
|
|
|
// end = maintained during the search
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
// lookupFirstZone returns the index of the time zone to use for times
|
|
|
|
// before the first transition time, or when there are no transition
|
|
|
|
// times.
|
|
|
|
//
|
|
|
|
// The reference implementation in localtime.c from
|
2018-09-24 23:46:21 +02:00
|
|
|
// https://www.iana.org/time-zones/repository/releases/tzcode2013g.tar.gz
|
2014-06-07 00:37:27 +02:00
|
|
|
// implements the following algorithm for these cases:
|
|
|
|
// 1) If the first zone is unused by the transitions, use it.
|
|
|
|
// 2) Otherwise, if there are transition times, and the first
|
|
|
|
// transition is to a zone in daylight time, find the first
|
|
|
|
// non-daylight-time zone before and closest to the first transition
|
|
|
|
// zone.
|
|
|
|
// 3) Otherwise, use the first zone that is not daylight time, if
|
|
|
|
// there is one.
|
|
|
|
// 4) Otherwise, use the first zone.
|
|
|
|
func (l *Location) lookupFirstZone() int {
|
|
|
|
// Case 1.
|
|
|
|
if !l.firstZoneUsed() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2.
|
|
|
|
if len(l.tx) > 0 && l.zone[l.tx[0].index].isDST {
|
|
|
|
for zi := int(l.tx[0].index) - 1; zi >= 0; zi-- {
|
|
|
|
if !l.zone[zi].isDST {
|
|
|
|
return zi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 3.
|
|
|
|
for zi := range l.zone {
|
|
|
|
if !l.zone[zi].isDST {
|
|
|
|
return zi
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 4.
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
// firstZoneUsed reports whether the first zone is used by some
|
2014-06-07 00:37:27 +02:00
|
|
|
// transition.
|
|
|
|
func (l *Location) firstZoneUsed() bool {
|
|
|
|
for _, tx := range l.tx {
|
|
|
|
if tx.index == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2011-12-13 20:16:27 +01:00
|
|
|
// lookupName returns information about the time zone with
|
2013-07-16 08:54:42 +02:00
|
|
|
// the given name (such as "EST") at the given pseudo-Unix time
|
|
|
|
// (what the given time of day would be in UTC).
|
2018-01-09 02:23:08 +01:00
|
|
|
func (l *Location) lookupName(name string, unix int64) (offset int, ok bool) {
|
2011-12-13 20:16:27 +01:00
|
|
|
l = l.get()
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
// First try for a zone with the right name that was actually
|
|
|
|
// in effect at the given time. (In Sydney, Australia, both standard
|
|
|
|
// and daylight-savings time are abbreviated "EST". Using the
|
|
|
|
// offset helps us pick the right one for the given time.
|
|
|
|
// It's not perfect: during the backward transition we might pick
|
|
|
|
// either one.)
|
|
|
|
for i := range l.zone {
|
|
|
|
zone := &l.zone[i]
|
|
|
|
if zone.name == name {
|
2018-09-24 23:46:21 +02:00
|
|
|
nam, offset, _, _ := l.lookup(unix - int64(zone.offset))
|
2013-07-16 08:54:42 +02:00
|
|
|
if nam == zone.name {
|
2018-01-09 02:23:08 +01:00
|
|
|
return offset, true
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise fall back to an ordinary name match.
|
2011-12-13 20:16:27 +01:00
|
|
|
for i := range l.zone {
|
|
|
|
zone := &l.zone[i]
|
|
|
|
if zone.name == name {
|
2018-01-09 02:23:08 +01:00
|
|
|
return zone.offset, true
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
// Otherwise, give up.
|
2011-12-13 20:16:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(rsc): Eventually we will need to accept the POSIX TZ environment
|
|
|
|
// syntax too, but I don't feel like implementing it today.
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
var errLocation = errors.New("time: invalid location name")
|
|
|
|
|
|
|
|
var zoneinfo *string
|
|
|
|
var zoneinfoOnce sync.Once
|
2011-12-13 20:16:27 +01:00
|
|
|
|
|
|
|
// LoadLocation returns the Location with the given name.
|
|
|
|
//
|
|
|
|
// If the name is "" or "UTC", LoadLocation returns UTC.
|
|
|
|
// If the name is "Local", LoadLocation returns Local.
|
|
|
|
//
|
|
|
|
// Otherwise, the name is taken to be a location name corresponding to a file
|
|
|
|
// in the IANA Time Zone database, such as "America/New_York".
|
2012-03-02 21:01:37 +01:00
|
|
|
//
|
|
|
|
// The time zone database needed by LoadLocation may not be
|
|
|
|
// present on all systems, especially non-Unix systems.
|
|
|
|
// LoadLocation looks in the directory or uncompressed zip file
|
|
|
|
// named by the ZONEINFO environment variable, if any, then looks in
|
|
|
|
// known installation locations on Unix systems,
|
|
|
|
// and finally looks in $GOROOT/lib/time/zoneinfo.zip.
|
2011-12-13 20:16:27 +01:00
|
|
|
func LoadLocation(name string) (*Location, error) {
|
|
|
|
if name == "" || name == "UTC" {
|
|
|
|
return UTC, nil
|
|
|
|
}
|
|
|
|
if name == "Local" {
|
|
|
|
return Local, nil
|
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
if containsDotDot(name) || name[0] == '/' || name[0] == '\\' {
|
|
|
|
// No valid IANA Time Zone name contains a single dot,
|
|
|
|
// much less dot dot. Likewise, none begin with a slash.
|
|
|
|
return nil, errLocation
|
|
|
|
}
|
|
|
|
zoneinfoOnce.Do(func() {
|
|
|
|
env, _ := syscall.Getenv("ZONEINFO")
|
|
|
|
zoneinfo = &env
|
|
|
|
})
|
2019-01-18 20:04:36 +01:00
|
|
|
var firstErr error
|
2018-01-09 02:23:08 +01:00
|
|
|
if *zoneinfo != "" {
|
|
|
|
if zoneData, err := loadTzinfoFromDirOrZip(*zoneinfo, name); err == nil {
|
|
|
|
if z, err := LoadLocationFromTZData(name, zoneData); err == nil {
|
|
|
|
return z, nil
|
|
|
|
}
|
2019-01-18 20:04:36 +01:00
|
|
|
firstErr = err
|
|
|
|
} else if err != syscall.ENOENT {
|
|
|
|
firstErr = err
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-18 20:04:36 +01:00
|
|
|
if z, err := loadLocation(name, zoneSources); err == nil {
|
|
|
|
return z, nil
|
|
|
|
} else if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
return nil, firstErr
|
2011-12-13 20:16:27 +01:00
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
|
|
|
|
// containsDotDot reports whether s contains "..".
|
|
|
|
func containsDotDot(s string) bool {
|
|
|
|
if len(s) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(s)-1; i++ {
|
|
|
|
if s[i] == '.' && s[i+1] == '.' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|