Source file src/time/zoneinfo_unix.go

     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  var platformZoneSources = []string{
    21  	"/usr/share/zoneinfo/",
    22  	"/usr/share/lib/zoneinfo/",
    23  	"/usr/lib/locale/TZ/",
    24  }
    25  
    26  func initLocal() {
    27  	// consult $TZ to find the time zone to use.
    28  	// no $TZ means use the system default /etc/localtime.
    29  	// $TZ="" means use UTC.
    30  	// $TZ="foo" or $TZ=":foo" if foo is an absolute path, then the file pointed
    31  	// by foo will be used to initialize timezone; otherwise, file
    32  	// /usr/share/zoneinfo/foo will be used.
    33  
    34  	tz, ok := syscall.Getenv("TZ")
    35  	switch {
    36  	case !ok:
    37  		z, err := loadLocation("localtime", []string{"/etc"})
    38  		if err == nil {
    39  			localLoc = *z
    40  			localLoc.name = "Local"
    41  			return
    42  		}
    43  	case tz != "":
    44  		if tz[0] == ':' {
    45  			tz = tz[1:]
    46  		}
    47  		if tz != "" && tz[0] == '/' {
    48  			if z, err := loadLocation(tz, []string{""}); err == nil {
    49  				localLoc = *z
    50  				if tz == "/etc/localtime" {
    51  					localLoc.name = "Local"
    52  				} else {
    53  					localLoc.name = tz
    54  				}
    55  				return
    56  			}
    57  		} else if tz != "" && tz != "UTC" {
    58  			if z, err := loadLocation(tz, platformZoneSources); err == nil {
    59  				localLoc = *z
    60  				return
    61  			}
    62  		}
    63  	}
    64  
    65  	// Fall back to UTC.
    66  	localLoc.name = "UTC"
    67  }
    68  

View as plain text