github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/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  	"testing"
    11  	"time"
    12  )
    13  
    14  func init() {
    15  	if time.ZoneinfoForTesting() != nil {
    16  		panic(fmt.Errorf("zoneinfo initialized before first LoadLocation"))
    17  	}
    18  }
    19  
    20  func TestEnvVarUsage(t *testing.T) {
    21  	time.ResetZoneinfoForTesting()
    22  
    23  	const testZoneinfo = "foo.zip"
    24  	const env = "ZONEINFO"
    25  
    26  	defer os.Setenv(env, os.Getenv(env))
    27  	os.Setenv(env, testZoneinfo)
    28  
    29  	// Result isn't important, we're testing the side effect of this command
    30  	time.LoadLocation("Asia/Jerusalem")
    31  	defer time.ResetZoneinfoForTesting()
    32  
    33  	if zoneinfo := time.ZoneinfoForTesting(); testZoneinfo != *zoneinfo {
    34  		t.Errorf("zoneinfo does not match env variable: got %q want %q", zoneinfo, testZoneinfo)
    35  	}
    36  }
    37  
    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, "")
    43  
    44  	bad := []string{
    45  		"/usr/foo/Foo",
    46  		"\\UNC\foo",
    47  		"..",
    48  		"a..",
    49  	}
    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)
    54  		}
    55  	}
    56  }
    57  
    58  func TestVersion3(t *testing.T) {
    59  	time.ForceZipFileForTesting(true)
    60  	defer time.ForceZipFileForTesting(false)
    61  	_, err := time.LoadLocation("Asia/Jerusalem")
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  }
    66  
    67  // Test that we get the correct results for times before the first
    68  // transition time. To do this we explicitly check early dates in a
    69  // couple of specific timezones.
    70  func TestFirstZone(t *testing.T) {
    71  	time.ForceZipFileForTesting(true)
    72  	defer time.ForceZipFileForTesting(false)
    73  
    74  	const format = "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
    75  	var tests = []struct {
    76  		zone  string
    77  		unix  int64
    78  		want1 string
    79  		want2 string
    80  	}{
    81  		{
    82  			"PST8PDT",
    83  			-1633269601,
    84  			"Sun, 31 Mar 1918 01:59:59 -0800 (PST)",
    85  			"Sun, 31 Mar 1918 03:00:00 -0700 (PDT)",
    86  		},
    87  		{
    88  			"Pacific/Fakaofo",
    89  			1325242799,
    90  			"Thu, 29 Dec 2011 23:59:59 -1100 (TKT)",
    91  			"Sat, 31 Dec 2011 00:00:00 +1300 (TKT)",
    92  		},
    93  	}
    94  
    95  	for _, test := range tests {
    96  		z, err := time.LoadLocation(test.zone)
    97  		if err != nil {
    98  			t.Fatal(err)
    99  		}
   100  		s := time.Unix(test.unix, 0).In(z).Format(format)
   101  		if s != test.want1 {
   102  			t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want1)
   103  		}
   104  		s = time.Unix(test.unix+1, 0).In(z).Format(format)
   105  		if s != test.want2 {
   106  			t.Errorf("for %s %d got %q want %q", test.zone, test.unix, s, test.want2)
   107  		}
   108  	}
   109  }
   110  
   111  func TestLocationNames(t *testing.T) {
   112  	if time.Local.String() != "Local" {
   113  		t.Errorf(`invalid Local location name: got %q want "Local"`, time.Local)
   114  	}
   115  	if time.UTC.String() != "UTC" {
   116  		t.Errorf(`invalid UTC location name: got %q want "UTC"`, time.UTC)
   117  	}
   118  }