github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/time/zoneinfo_unix.go (about)

     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 dragonfly 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  
    12  package time
    13  
    14  import (
    15  	"errors"
    16  	"runtime"
    17  	"syscall"
    18  )
    19  
    20  func initTestingZone() {
    21  	z, err := loadZoneFile(runtime.GOROOT()+"/lib/time/zoneinfo.zip", "America/Los_Angeles")
    22  	if err != nil {
    23  		panic("cannot load America/Los_Angeles for testing: " + err.Error())
    24  	}
    25  	z.name = "Local"
    26  	localLoc = *z
    27  }
    28  
    29  // Many systems use /usr/share/zoneinfo, Solaris 2 has
    30  // /usr/share/lib/zoneinfo, IRIX 6 has /usr/lib/locale/TZ.
    31  var zoneDirs = []string{
    32  	"/usr/share/zoneinfo/",
    33  	"/usr/share/lib/zoneinfo/",
    34  	"/usr/lib/locale/TZ/",
    35  	runtime.GOROOT() + "/lib/time/zoneinfo/",
    36  }
    37  
    38  func initLocal() {
    39  	// consult $TZ to find the time zone to use.
    40  	// no $TZ means use the system default /etc/localtime.
    41  	// $TZ="" means use UTC.
    42  	// $TZ="foo" means use /usr/share/zoneinfo/foo.
    43  
    44  	tz, ok := syscall.Getenv("TZ")
    45  	switch {
    46  	case !ok:
    47  		z, err := loadZoneFile("", "/etc/localtime")
    48  		if err == nil {
    49  			localLoc = *z
    50  			localLoc.name = "Local"
    51  			return
    52  		}
    53  	case tz != "" && tz != "UTC":
    54  		if z, err := loadLocation(tz); err == nil {
    55  			localLoc = *z
    56  			return
    57  		}
    58  	}
    59  
    60  	// Fall back to UTC.
    61  	localLoc.name = "UTC"
    62  }
    63  
    64  func loadLocation(name string) (*Location, error) {
    65  	for _, zoneDir := range zoneDirs {
    66  		if z, err := loadZoneFile(zoneDir, name); err == nil {
    67  			z.name = name
    68  			return z, nil
    69  		}
    70  	}
    71  	return nil, errors.New("unknown time zone " + name)
    72  }