| 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 |
//go:build unix && !ios && !android |
| 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), https://en.wikipedia.org/wiki/Zoneinfo, |
| 10 |
// and ftp://munnari.oz.au/pub/oldtz/ |
| 11 |
|
| 12 |
package time |
| 13 |
|
| 14 |
import ( |
| 15 |
"syscall" |
| 16 |
) |
| 17 |
|
| 18 |
// Many systems use /usr/share/zoneinfo, Solaris 2 has |
| 19 |
// /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ, |
| 20 |
// NixOS has /etc/zoneinfo. |
| 21 |
var platformZoneSources = []string{ |
| 22 |
"/usr/share/zoneinfo/", |
| 23 |
"/usr/share/lib/zoneinfo/", |
| 24 |
"/usr/lib/locale/TZ/", |
| 25 |
"/etc/zoneinfo", |
| 26 |
} |
| 27 |
|
| 28 |
func initLocal() { |
| 29 |
// consult $TZ to find the time zone to use. |
| 30 |
// no $TZ means use the system default /etc/localtime. |
| 31 |
// $TZ="" means use UTC. |
| 32 |
// $TZ="foo" or $TZ=":foo" if foo is an absolute path, then the file pointed |
| 33 |
// by foo will be used to initialize timezone; otherwise, file |
| 34 |
// /usr/share/zoneinfo/foo will be used. |
| 35 |
|
| 36 |
tz, ok := syscall.Getenv("TZ") |
| 37 |
switch { |
| 38 |
case !ok: |
| 39 |
z, err := loadLocation("localtime", []string{"/etc"}) |
| 40 |
if err == nil { |
| 41 |
localLoc = *z |
| 42 |
localLoc.name = "Local" |
| 43 |
return |
| 44 |
} |
| 45 |
case tz != "": |
| 46 |
if tz[0] == ':' { |
| 47 |
tz = tz[1:] |
| 48 |
} |
| 49 |
if tz != "" && tz[0] == '/' { |
| 50 |
if z, err := loadLocation(tz, []string{""}); err == nil { |
| 51 |
localLoc = *z |
| 52 |
if tz == "/etc/localtime" { |
| 53 |
localLoc.name = "Local" |
| 54 |
} else { |
| 55 |
localLoc.name = tz |
| 56 |
} |
| 57 |
return |
| 58 |
} |
| 59 |
} else if tz != "" && tz != "UTC" { |
| 60 |
if z, err := loadLocation(tz, platformZoneSources); err == nil { |
| 61 |
localLoc = *z |
| 62 |
return |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// Fall back to UTC. |
| 68 |
localLoc.name = "UTC" |
| 69 |
} |