Black Lives Matter. Support the Equal Justice Initiative.

Source file src/time/zoneinfo_unix_test.go

Documentation: time

     1  // Copyright 2020 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  package time_test
     9  
    10  import (
    11  	"os"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestEnvTZUsage(t *testing.T) {
    17  	const env = "TZ"
    18  	tz, ok := os.LookupEnv(env)
    19  	if !ok {
    20  		defer os.Unsetenv(env)
    21  	} else {
    22  		defer os.Setenv(env, tz)
    23  	}
    24  	defer time.ForceUSPacificForTesting()
    25  
    26  	localZoneName := "Local"
    27  	// The file may not exist.
    28  	if _, err := os.Stat("/etc/localtime"); os.IsNotExist(err) {
    29  		localZoneName = "UTC"
    30  	}
    31  
    32  	cases := []struct {
    33  		nilFlag bool
    34  		tz      string
    35  		local   string
    36  	}{
    37  		// no $TZ means use the system default /etc/localtime.
    38  		{true, "", localZoneName},
    39  		// $TZ="" means use UTC.
    40  		{false, "", "UTC"},
    41  		{false, ":", "UTC"},
    42  		{false, "Asia/Shanghai", "Asia/Shanghai"},
    43  		{false, ":Asia/Shanghai", "Asia/Shanghai"},
    44  		{false, "/etc/localtime", localZoneName},
    45  		{false, ":/etc/localtime", localZoneName},
    46  	}
    47  
    48  	for _, c := range cases {
    49  		time.ResetLocalOnceForTest()
    50  		if c.nilFlag {
    51  			os.Unsetenv(env)
    52  		} else {
    53  			os.Setenv(env, c.tz)
    54  		}
    55  		if time.Local.String() != c.local {
    56  			t.Errorf("invalid Local location name for %q: got %q want %q", c.tz, time.Local, c.local)
    57  		}
    58  	}
    59  
    60  	time.ResetLocalOnceForTest()
    61  	// The file may not exist on Solaris 2 and IRIX 6.
    62  	path := "/usr/share/zoneinfo/Asia/Shanghai"
    63  	os.Setenv(env, path)
    64  	if _, err := os.Stat(path); os.IsNotExist(err) {
    65  		if time.Local.String() != "UTC" {
    66  			t.Errorf(`invalid path should fallback to UTC: got %q want "UTC"`, time.Local)
    67  		}
    68  		return
    69  	}
    70  	if time.Local.String() != path {
    71  		t.Errorf(`custom path should lead to path itself: got %q want %q`, time.Local, path)
    72  	}
    73  
    74  	timeInUTC := time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
    75  	sameTimeInShanghai := time.Date(2009, 1, 1, 20, 0, 0, 0, time.Local)
    76  	if !timeInUTC.Equal(sameTimeInShanghai) {
    77  		t.Errorf("invalid timezone: got %q want %q", timeInUTC, sameTimeInShanghai)
    78  	}
    79  
    80  	time.ResetLocalOnceForTest()
    81  	os.Setenv(env, ":"+path)
    82  	if time.Local.String() != path {
    83  		t.Errorf(`custom path should lead to path itself: got %q want %q`, time.Local, path)
    84  	}
    85  
    86  	time.ResetLocalOnceForTest()
    87  	os.Setenv(env, path[:len(path)-1])
    88  	if time.Local.String() != "UTC" {
    89  		t.Errorf(`invalid path should fallback to UTC: got %q want "UTC"`, time.Local)
    90  	}
    91  }
    92  

View as plain text