Source file src/time/zoneinfo_unix_test.go

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

View as plain text