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