1// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// +build darwin freebsd linux netbsd openbsd
6
7// Parse "zoneinfo" time zone file.
8// This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
9// See tzfile(5), http://en.wikipedia.org/wiki/Zoneinfo,
10// and ftp://munnari.oz.au/pub/oldtz/
11
12package time
13
14import (
15	"errors"
16	"runtime"
17	"syscall"
18)
19
20func initTestingZone() {
21	syscall.Setenv("TZ", "America/Los_Angeles")
22	initLocal()
23}
24
25// Many systems use /usr/share/zoneinfo, Solaris 2 has
26// /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
27var zoneDirs = []string{
28	"/usr/share/zoneinfo/",
29	"/usr/share/lib/zoneinfo/",
30	"/usr/lib/locale/TZ/",
31	runtime.GOROOT() + "/lib/time/zoneinfo/",
32}
33
34func initLocal() {
35	// consult $TZ to find the time zone to use.
36	// no $TZ means use the system default /etc/localtime.
37	// $TZ="" means use UTC.
38	// $TZ="foo" means use /usr/share/zoneinfo/foo.
39
40	tz, ok := syscall.Getenv("TZ")
41	switch {
42	case !ok:
43		z, err := loadZoneFile("", "/etc/localtime")
44		if err == nil {
45			localLoc = *z
46			localLoc.name = "Local"
47			return
48		}
49	case tz != "" && tz != "UTC":
50		if z, err := loadLocation(tz); err == nil {
51			localLoc = *z
52			return
53		}
54	}
55
56	// Fall back to UTC.
57	localLoc.name = "UTC"
58}
59
60func loadLocation(name string) (*Location, error) {
61	for _, zoneDir := range zoneDirs {
62		if z, err := loadZoneFile(zoneDir, name); err == nil {
63			z.name = name
64			return z, nil
65		}
66	}
67	return nil, errors.New("unknown time zone " + name)
68}
69