Skip gnat.dg/prot7.adb on hppa.
[official-gcc.git] / libgo / go / time / zoneinfo_unix_test.go
blobde95295cd3f6bdb24ce224a2cb310d0f0e42bddc
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
7 package time_test
9 import (
10 "os"
11 "testing"
12 "time"
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)
23 defer time.ForceUSPacificForTesting()
25 localZoneName := "Local"
26 // The file may not exist.
27 if _, err := os.Stat("/etc/localtime"); os.IsNotExist(err) {
28 localZoneName = "UTC"
31 cases := []struct {
32 nilFlag bool
33 tz string
34 local string
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},
47 for _, c := range cases {
48 time.ResetLocalOnceForTest()
49 if c.nilFlag {
50 os.Unsetenv(env)
51 } else {
52 os.Setenv(env, c.tz)
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"
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)
67 return
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)