github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/time/zoneinfo_test.go (about) 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. 4 5 package time_test 6 7 import ( 8 "fmt" 9 "os" 10 "reflect" 11 "testing" 12 "time" 13 ) 14 15 func init() { 16 if time.ZoneinfoForTesting() != nil { 17 panic(fmt.Errorf("zoneinfo initialized before first LoadLocation")) 18 } 19 } 20 21 func TestEnvVarUsage(t *testing.T) { 22 time.ResetZoneinfoForTesting() 23 24 const testZoneinfo = "foo.zip" 25 const env = "ZONEINFO" 26 27 defer os.Setenv(env, os.Getenv(env)) 28 os.Setenv(env, testZoneinfo) 29 30 // Result isn't important, we're testing the side effect of this command 31 time.LoadLocation("Asia/Jerusalem") 32 defer time.ResetZoneinfoForTesting() 33 34 if zoneinfo := time.ZoneinfoForTesting(); testZoneinfo != *zoneinfo { 35 t.Errorf("zoneinfo does not match env variable: got %q want %q", zoneinfo, testZoneinfo) 36 } 37 } 38 39 func TestLoadLocationValidatesNames(t *testing.T) { 40 time.ResetZoneinfoForTesting() 41 const env = "ZONEINFO" 42 defer os.Setenv(env, os.Getenv(env)) 43 os.Setenv(env, "") 44 45 bad := []string{ 46 "/usr/foo/Foo", 47 "\\UNC\foo", 48 "..", 49 "a..", 50 } 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) 55 } 56 } 57 } 58 59 func TestVersion3(t *testing.T) { 60 time.ForceZipFileForTesting(true) 61 defer time.ForceZipFileForTesting(false) 62 _, err := time.LoadLocation("Asia/Jerusalem") 63 if err != nil { 64 t.Fatal(err) 65 } 66 } 67 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 time.ForceZipFileForTesting(true) 73 defer time.ForceZipFileForTesting(false) 74 75 const format = "Mon, 02 Jan 2006 15:04:05 -0700 (MST)" 76 var tests = []struct { 77 zone string 78 unix int64 79 want1 string 80 want2 string 81 }{ 82 { 83 "PST8PDT", 84 -1633269601, 85 "Sun, 31 Mar 1918 01:59:59 -0800 (PST)", 86 "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)", 87 }, 88 { 89 "Pacific/Fakaofo", 90 1325242799, 91 "Thu, 29 Dec 2011 23:59:59 -1100 (-11)", 92 "Sat, 31 Dec 2011 00:00:00 +1300 (+13)", 93 }, 94 } 95 96 for _, test := range tests { 97 z, err := time.LoadLocation(test.zone) 98 if err != nil { 99 t.Fatal(err) 100 } 101 s := time.Unix(test.unix, 0).In(z).Format(format) 102 if s != test.want1 { 103 t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want1) 104 } 105 s = time.Unix(test.unix+1, 0).In(z).Format(format) 106 if s != test.want2 { 107 t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want2) 108 } 109 } 110 } 111 112 func TestLocationNames(t *testing.T) { 113 if time.Local.String() != "Local" { 114 t.Errorf(`invalid Local location name: got %q want "Local"`, time.Local) 115 } 116 if time.UTC.String() != "UTC" { 117 t.Errorf(`invalid UTC location name: got %q want "UTC"`, time.UTC) 118 } 119 } 120 121 func TestLoadLocationFromTZData(t *testing.T) { 122 time.ForceZipFileForTesting(true) 123 defer time.ForceZipFileForTesting(false) 124 125 const locationName = "Asia/Jerusalem" 126 reference, err := time.LoadLocation(locationName) 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 tzinfo, err := time.LoadTzinfo(locationName, time.OrigZoneSources[len(time.OrigZoneSources)-1]) 132 if err != nil { 133 t.Fatal(err) 134 } 135 sample, err := time.LoadLocationFromTZData(locationName, tzinfo) 136 if err != nil { 137 t.Fatal(err) 138 } 139 140 if !reflect.DeepEqual(reference, sample) { 141 t.Errorf("return values of LoadLocationFromTZData and LoadLocation don't match") 142 } 143 }