Use __builtin_memmove for trivially copyable types
[official-gcc.git] / libgo / go / time / zoneinfo_test.go
blobde96416ed93e61b4f6485679fe6e46cef6cd53a9
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 "reflect"
11 "testing"
12 "time"
15 func init() {
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))
43 os.Setenv(env, "")
45 bad := []string{
46 "/usr/foo/Foo",
47 "\\UNC\foo",
48 "..",
49 "a..",
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")
64 if err != nil {
65 t.Fatal(err)
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 {
80 zone string
81 unix int64
82 want1 string
83 want2 string
86 "PST8PDT",
87 -1633269601,
88 "Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
89 "Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
92 "Pacific/Fakaofo",
93 1325242799,
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)
101 if err != nil {
102 t.Fatal(err)
104 s := time.Unix(test.unix, 0).In(z).Format(format)
105 if s != test.want1 {
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)
109 if s != test.want2 {
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)
132 if err != nil {
133 t.Fatal(err)
136 tzinfo, err := time.LoadTzinfo(locationName, time.OrigZoneSources[len(time.OrigZoneSources)-1])
137 if err != nil {
138 t.Fatal(err)
140 sample, err := time.LoadLocationFromTZData(locationName, tzinfo)
141 if err != nil {
142 t.Fatal(err)
145 if !reflect.DeepEqual(reference, sample) {
146 t.Errorf("return values of LoadLocationFromTZData and LoadLocation don't match")