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.
5 //go:build aix || (darwin && !ios) || dragonfly || freebsd || (linux && !android) || netbsd || openbsd || solaris
15 func TestEnvTZUsage(t
*testing
.T
) {
17 tz
, ok
:= os
.LookupEnv(env
)
19 defer os
.Unsetenv(env
)
21 defer os
.Setenv(env
, tz
)
23 defer time
.ForceUSPacificForTesting()
25 localZoneName
:= "Local"
26 // The file may not exist.
27 if _
, err
:= os
.Stat("/etc/localtime"); os
.IsNotExist(err
) {
36 // no $TZ means use the system default /etc/localtime.
37 {true, "", localZoneName
},
38 // $TZ="" means use UTC.
41 {false, "Asia/Shanghai", "Asia/Shanghai"},
42 {false, ":Asia/Shanghai", "Asia/Shanghai"},
43 {false, "/etc/localtime", localZoneName
},
44 {false, ":/etc/localtime", localZoneName
},
47 for _
, c
:= range cases
{
48 time
.ResetLocalOnceForTest()
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
)
59 time
.ResetLocalOnceForTest()
60 // The file may not exist on Solaris 2 and IRIX 6.
61 path
:= "/usr/share/zoneinfo/Asia/Shanghai"
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
)
69 if time
.Local
.String() != path
{
70 t
.Errorf(`custom path should lead to path itself: got %q want %q`, time
.Local
, path
)
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
)
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
)
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
)