libgo: update to go1.9
[official-gcc.git] / libgo / go / time / zoneinfo_test.go
blob8a4caa0c44522d36df4174b33f6ac2858eebc1f9
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.
5 package time_test
7 import (
8 "fmt"
9 "os"
10 "testing"
11 "time"
14 func init() {
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))
42 os.Setenv(env, "")
44 bad := []string{
45 "/usr/foo/Foo",
46 "\\UNC\foo",
47 "..",
48 "a..",
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")
63 if err != nil {
64 t.Fatal(err)
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 {
79 zone string
80 unix int64
81 want1 string
82 want2 string
85 "PST8PDT",
86 -1633269601,
87 "Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
88 "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
91 "Pacific/Fakaofo",
92 1325242799,
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)
100 if err != nil {
101 t.Fatal(err)
103 s := time.Unix(test.unix, 0).In(z).Format(format)
104 if s != test.want1 {
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)
108 if s != test.want2 {
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)