Black Lives Matter. Support the Equal Justice Initiative.

Source file src/time/zoneinfo_unix.go

Documentation: time

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

View as plain text