1 // Copyright 2014 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.
15 if time.ZoneinfoForTesting() != nil {
16 panic(fmt.Errorf("zoneinfo initialized before first LoadLocation"))
20 func TestEnvVarUsage(t *testing.T) {
21 time.ResetZoneinfoForTesting()
23 const testZoneinfo = "foo.zip"
24 const env = "ZONEINFO"
26 defer os.Setenv(env, os.Getenv(env))
27 os.Setenv(env, testZoneinfo)
29 // Result isn't important, we're testing the side effect of this command
30 time.LoadLocation("Asia/Jerusalem")
31 defer time.ResetZoneinfoForTesting()
33 if zoneinfo := time.ZoneinfoForTesting(); testZoneinfo != *zoneinfo {
34 t.Errorf("zoneinfo does not match env variable: got %q want %q", zoneinfo, testZoneinfo)
38 func TestLoadLocationValidatesNames(t *testing.T) {
39 time.ResetZoneinfoForTesting()
40 const env = "ZONEINFO"
41 defer os.Setenv(env, os.Getenv(env))
50 for _, v := range bad {
51 _, err := time.LoadLocation(v)
52 if err != time.ErrLocation {
53 t.Errorf("LoadLocation(%q) error = %v; want ErrLocation", v, err)
58 func TestVersion3(t *testing.T) {
59 time.ForceZipFileForTesting(true)
60 defer time.ForceZipFileForTesting(false)
61 _, err := time.LoadLocation("Asia/Jerusalem")
67 // Test that we get the correct results for times before the first
68 // transition time. To do this we explicitly check early dates in a
69 // couple of specific timezones.
70 func TestFirstZone(t *testing.T) {
71 time.ForceZipFileForTesting(true)
72 defer time.ForceZipFileForTesting(false)
74 const format = "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
75 var tests = []struct {
84 "Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
85 "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
90 "Thu, 29 Dec 2011 23:59:59 -1100 (-11)",
91 "Sat, 31 Dec 2011 00:00:00 +1300 (+13)",
95 for _, test := range tests {
96 z, err := time.LoadLocation(test.zone)
100 s := time.Unix(test.unix, 0).In(z).Format(format)
102 t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want1)
104 s = time.Unix(test.unix+1, 0).In(z).Format(format)
106 t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want2)
111 func TestLocationNames(t *testing.T) {
112 if time.Local.String() != "Local" {
113 t.Errorf(`invalid Local location name: got %q want "Local"`, time.Local)
115 if time.UTC.String() != "UTC" {
116 t.Errorf(`invalid UTC location name: got %q want "UTC"`, time.UTC)