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 t
.Skip("gccgo does not use the zip file")
60 time
.ForceZipFileForTesting(true)
61 defer time
.ForceZipFileForTesting(false)
62 _
, err
:= time
.LoadLocation("Asia/Jerusalem")
68 // Test that we get the correct results for times before the first
69 // transition time. To do this we explicitly check early dates in a
70 // couple of specific timezones.
71 func TestFirstZone(t
*testing
.T
) {
72 t
.Skip("gccgo does not use the zip file")
74 time
.ForceZipFileForTesting(true)
75 defer time
.ForceZipFileForTesting(false)
77 const format
= "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
78 var tests
= []struct {
87 "Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
88 "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
93 "Thu, 29 Dec 2011 23:59:59 -1100 (-11)",
94 "Sat, 31 Dec 2011 00:00:00 +1300 (+13)",
98 for _
, test
:= range tests
{
99 z
, err
:= time
.LoadLocation(test
.zone
)
103 s
:= time
.Unix(test
.unix
, 0).In(z
).Format(format
)
105 t
.Errorf("for %s %d got %q want %q", test
.zone
, test
.unix
, s
, test
.want1
)
107 s
= time
.Unix(test
.unix
+1, 0).In(z
).Format(format
)
109 t
.Errorf("for %s %d got %q want %q", test
.zone
, test
.unix
, s
, test
.want2
)
114 func TestLocationNames(t
*testing
.T
) {
115 if time
.Local
.String() != "Local" {
116 t
.Errorf(`invalid Local location name: got %q want "Local"`, time
.Local
)
118 if time
.UTC
.String() != "UTC" {
119 t
.Errorf(`invalid UTC location name: got %q want "UTC"`, time
.UTC
)