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.
16 if time
.ZoneinfoForTesting() != nil {
17 panic(fmt
.Errorf("zoneinfo initialized before first LoadLocation"))
21 func TestEnvVarUsage(t
*testing
.T
) {
22 time
.ResetZoneinfoForTesting()
24 const testZoneinfo
= "foo.zip"
25 const env
= "ZONEINFO"
27 defer os
.Setenv(env
, os
.Getenv(env
))
28 os
.Setenv(env
, testZoneinfo
)
30 // Result isn't important, we're testing the side effect of this command
31 time
.LoadLocation("Asia/Jerusalem")
32 defer time
.ResetZoneinfoForTesting()
34 if zoneinfo
:= time
.ZoneinfoForTesting(); testZoneinfo
!= *zoneinfo
{
35 t
.Errorf("zoneinfo does not match env variable: got %q want %q", zoneinfo
, testZoneinfo
)
39 func TestLoadLocationValidatesNames(t
*testing
.T
) {
40 time
.ResetZoneinfoForTesting()
41 const env
= "ZONEINFO"
42 defer os
.Setenv(env
, os
.Getenv(env
))
51 for _
, v
:= range bad
{
52 _
, err
:= time
.LoadLocation(v
)
53 if err
!= time
.ErrLocation
{
54 t
.Errorf("LoadLocation(%q) error = %v; want ErrLocation", v
, err
)
59 func TestVersion3(t
*testing
.T
) {
60 t
.Skip("gccgo does not use the zip file")
61 time
.ForceZipFileForTesting(true)
62 defer time
.ForceZipFileForTesting(false)
63 _
, err
:= time
.LoadLocation("Asia/Jerusalem")
69 // Test that we get the correct results for times before the first
70 // transition time. To do this we explicitly check early dates in a
71 // couple of specific timezones.
72 func TestFirstZone(t
*testing
.T
) {
73 t
.Skip("gccgo does not use the zip file")
75 time
.ForceZipFileForTesting(true)
76 defer time
.ForceZipFileForTesting(false)
78 const format
= "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
79 var tests
= []struct {
88 "Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
89 "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
94 "Thu, 29 Dec 2011 23:59:59 -1100 (-11)",
95 "Sat, 31 Dec 2011 00:00:00 +1300 (+13)",
99 for _
, test
:= range tests
{
100 z
, err
:= time
.LoadLocation(test
.zone
)
104 s
:= time
.Unix(test
.unix
, 0).In(z
).Format(format
)
106 t
.Errorf("for %s %d got %q want %q", test
.zone
, test
.unix
, s
, test
.want1
)
108 s
= time
.Unix(test
.unix
+1, 0).In(z
).Format(format
)
110 t
.Errorf("for %s %d got %q want %q", test
.zone
, test
.unix
, s
, test
.want2
)
115 func TestLocationNames(t
*testing
.T
) {
116 if time
.Local
.String() != "Local" {
117 t
.Errorf(`invalid Local location name: got %q want "Local"`, time
.Local
)
119 if time
.UTC
.String() != "UTC" {
120 t
.Errorf(`invalid UTC location name: got %q want "UTC"`, time
.UTC
)
124 func TestLoadLocationFromTZData(t
*testing
.T
) {
125 t
.Skip("gccgo does not use the zip file")
127 time
.ForceZipFileForTesting(true)
128 defer time
.ForceZipFileForTesting(false)
130 const locationName
= "Asia/Jerusalem"
131 reference
, err
:= time
.LoadLocation(locationName
)
136 tzinfo
, err
:= time
.LoadTzinfo(locationName
, time
.OrigZoneSources
[len(time
.OrigZoneSources
)-1])
140 sample
, err
:= time
.LoadLocationFromTZData(locationName
, tzinfo
)
145 if !reflect
.DeepEqual(reference
, sample
) {
146 t
.Errorf("return values of LoadLocationFromTZData and LoadLocation don't match")