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.
17 if time.ZoneinfoForTesting() != nil {
18 panic(fmt.Errorf("zoneinfo initialized before first LoadLocation"))
22 func TestEnvVarUsage(t *testing.T) {
23 time.ResetZoneinfoForTesting()
25 const testZoneinfo = "foo.zip"
26 const env = "ZONEINFO"
28 defer os.Setenv(env, os.Getenv(env))
29 os.Setenv(env, testZoneinfo)
31 // Result isn't important, we're testing the side effect of this command
32 time.LoadLocation("Asia/Jerusalem")
33 defer time.ResetZoneinfoForTesting()
35 if zoneinfo := time.ZoneinfoForTesting(); testZoneinfo != *zoneinfo {
36 t.Errorf("zoneinfo does not match env variable: got %q want %q", *zoneinfo, testZoneinfo)
40 func TestBadLocationErrMsg(t *testing.T) {
41 time.ResetZoneinfoForTesting()
42 loc := "Asia/SomethingNotExist"
43 want := errors.New("unknown time zone " + loc)
44 _, err := time.LoadLocation(loc)
45 if err.Error() != want.Error() {
46 t.Errorf("LoadLocation(%q) error = %v; want %v", loc, err, want)
50 func TestLoadLocationValidatesNames(t *testing.T) {
51 time.ResetZoneinfoForTesting()
52 const env = "ZONEINFO"
53 defer os.Setenv(env, os.Getenv(env))
62 for _, v := range bad {
63 _, err := time.LoadLocation(v)
64 if err != time.ErrLocation {
65 t.Errorf("LoadLocation(%q) error = %v; want ErrLocation", v, err)
70 func TestVersion3(t *testing.T) {
71 time.ForceZipFileForTesting(true)
72 defer time.ForceZipFileForTesting(false)
73 _, err := time.LoadLocation("Asia/Jerusalem")
79 // Test that we get the correct results for times before the first
80 // transition time. To do this we explicitly check early dates in a
81 // couple of specific timezones.
82 func TestFirstZone(t *testing.T) {
83 time.ForceZipFileForTesting(true)
84 defer time.ForceZipFileForTesting(false)
86 const format = "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
87 var tests = []struct {
96 "Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
97 "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
102 "Thu, 29 Dec 2011 23:59:59 -1100 (-11)",
103 "Sat, 31 Dec 2011 00:00:00 +1300 (+13)",
107 for _, test := range tests {
108 z, err := time.LoadLocation(test.zone)
112 s := time.Unix(test.unix, 0).In(z).Format(format)
114 t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want1)
116 s = time.Unix(test.unix+1, 0).In(z).Format(format)
118 t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want2)
123 func TestLocationNames(t *testing.T) {
124 if time.Local.String() != "Local" {
125 t.Errorf(`invalid Local location name: got %q want "Local"`, time.Local)
127 if time.UTC.String() != "UTC" {
128 t.Errorf(`invalid UTC location name: got %q want "UTC"`, time.UTC)
132 func TestLoadLocationFromTZData(t *testing.T) {
133 time.ForceZipFileForTesting(true)
134 defer time.ForceZipFileForTesting(false)
136 const locationName = "Asia/Jerusalem"
137 reference, err := time.LoadLocation(locationName)
142 tzinfo, err := time.LoadTzinfo(locationName, time.OrigZoneSources[len(time.OrigZoneSources)-1])
146 sample, err := time.LoadLocationFromTZData(locationName, tzinfo)
151 if !reflect.DeepEqual(reference, sample) {
152 t.Errorf("return values of LoadLocationFromTZData and LoadLocation don't match")