2010-06-29 23:29:09 -06:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2011-12-21 03:44:47 -07:00
|
|
|
// +build darwin freebsd linux netbsd openbsd
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2010-06-29 23:29:09 -06:00
|
|
|
// Parse "zoneinfo" time zone file.
|
|
|
|
// This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
|
|
|
|
// See tzfile(5), http://en.wikipedia.org/wiki/Zoneinfo,
|
|
|
|
// and ftp://munnari.oz.au/pub/oldtz/
|
|
|
|
|
|
|
|
package time
|
|
|
|
|
|
|
|
import (
|
2011-11-30 09:59:44 -07:00
|
|
|
"errors"
|
2012-02-18 19:02:41 -07:00
|
|
|
"runtime"
|
2011-11-30 09:59:44 -07:00
|
|
|
"syscall"
|
2010-06-29 23:29:09 -06:00
|
|
|
)
|
|
|
|
|
2012-02-18 19:02:41 -07:00
|
|
|
func initTestingZone() {
|
|
|
|
z, err := loadZoneFile(runtime.GOROOT() + "/lib/time/zoneinfo/" + "America/Los_Angeles")
|
2011-11-30 09:59:44 -07:00
|
|
|
if err != nil {
|
2012-02-18 19:02:41 -07:00
|
|
|
panic("cannot load America/Los_Angeles for testing: " + err.Error())
|
2011-11-11 12:40:41 -07:00
|
|
|
}
|
2012-02-18 19:02:41 -07:00
|
|
|
z.name = "Local"
|
|
|
|
localLoc = *z
|
2011-08-26 13:15:23 -06:00
|
|
|
}
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
// Many systems use /usr/share/zoneinfo, Solaris 2 has
|
|
|
|
// /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
|
|
|
|
var zoneDirs = []string{
|
|
|
|
"/usr/share/zoneinfo/",
|
|
|
|
"/usr/share/lib/zoneinfo/",
|
|
|
|
"/usr/lib/locale/TZ/",
|
2012-02-18 19:02:41 -07:00
|
|
|
runtime.GOROOT() + "/lib/time/zoneinfo/",
|
2011-11-30 09:59:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func initLocal() {
|
2010-06-29 23:29:09 -06:00
|
|
|
// consult $TZ to find the time zone to use.
|
|
|
|
// no $TZ means use the system default /etc/localtime.
|
|
|
|
// $TZ="" means use UTC.
|
|
|
|
// $TZ="foo" means use /usr/share/zoneinfo/foo.
|
|
|
|
|
2011-11-30 09:59:44 -07:00
|
|
|
tz, ok := syscall.Getenv("TZ")
|
2010-06-29 23:29:09 -06:00
|
|
|
switch {
|
2011-11-30 09:59:44 -07:00
|
|
|
case !ok:
|
|
|
|
z, err := loadZoneFile("/etc/localtime")
|
|
|
|
if err == nil {
|
|
|
|
localLoc = *z
|
|
|
|
localLoc.name = "Local"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case tz != "" && tz != "UTC":
|
|
|
|
if z, err := loadLocation(tz); err == nil {
|
|
|
|
localLoc = *z
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to UTC.
|
|
|
|
localLoc.name = "UTC"
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadLocation(name string) (*Location, error) {
|
|
|
|
for _, zoneDir := range zoneDirs {
|
|
|
|
if z, err := loadZoneFile(zoneDir + name); err == nil {
|
|
|
|
z.name = name
|
|
|
|
return z, nil
|
2011-01-28 16:56:14 -07:00
|
|
|
}
|
2010-06-29 23:29:09 -06:00
|
|
|
}
|
2011-11-30 09:59:44 -07:00
|
|
|
return nil, errors.New("unknown time zone " + name)
|
2010-06-29 23:29:09 -06:00
|
|
|
}
|