github.com/s1s1ty/go@v0.0.0-20180207192209-104445e3140f/src/time/zoneinfo_windows_test.go (about)

     1  // Copyright 2013 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  	"internal/syscall/windows/registry"
     9  	"testing"
    10  	. "time"
    11  )
    12  
    13  func testZoneAbbr(t *testing.T) {
    14  	t1 := Now()
    15  	// discard nsec
    16  	t1 = Date(t1.Year(), t1.Month(), t1.Day(), t1.Hour(), t1.Minute(), t1.Second(), 0, t1.Location())
    17  
    18  	// Skip the test if we're in a timezone with no abbreviation.
    19  	// Format will fallback to the numeric abbreviation, and
    20  	// Parse(RFC1123, ..) will fail (see Issue 21183).
    21  	if tz := t1.Format("MST"); tz[0] == '-' || tz[0] == '+' {
    22  		t.Skip("No zone abbreviation")
    23  	}
    24  
    25  	t2, err := Parse(RFC1123, t1.Format(RFC1123))
    26  	if err != nil {
    27  		t.Fatalf("Parse failed: %v", err)
    28  	}
    29  	if t1 != t2 {
    30  		t.Fatalf("t1 (%v) is not equal to t2 (%v)", t1, t2)
    31  	}
    32  }
    33  
    34  func TestUSPacificZoneAbbr(t *testing.T) {
    35  	ForceUSPacificFromTZIForTesting() // reset the Once to trigger the race
    36  	defer ForceUSPacificForTesting()
    37  	testZoneAbbr(t)
    38  }
    39  
    40  func TestAusZoneAbbr(t *testing.T) {
    41  	ForceAusFromTZIForTesting()
    42  	defer ForceUSPacificForTesting()
    43  	testZoneAbbr(t)
    44  }
    45  
    46  func TestToEnglishName(t *testing.T) {
    47  	const want = "Central Europe Standard Time"
    48  	k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\`+want, registry.READ)
    49  	if err != nil {
    50  		t.Fatalf("cannot open CEST time zone information from registry: %s", err)
    51  	}
    52  	defer k.Close()
    53  
    54  	var std, dlt string
    55  	if err = registry.LoadRegLoadMUIString(); err == nil {
    56  		// Try MUI_Std and MUI_Dlt first, fallback to Std and Dlt if *any* error occurs
    57  		std, err = k.GetMUIStringValue("MUI_Std")
    58  		if err == nil {
    59  			dlt, err = k.GetMUIStringValue("MUI_Dlt")
    60  		}
    61  	}
    62  	if err != nil { // Fallback to Std and Dlt
    63  		if std, _, err = k.GetStringValue("Std"); err != nil {
    64  			t.Fatalf("cannot read CEST Std registry key: %s", err)
    65  		}
    66  		if dlt, _, err = k.GetStringValue("Dlt"); err != nil {
    67  			t.Fatalf("cannot read CEST Dlt registry key: %s", err)
    68  		}
    69  	}
    70  
    71  	name, err := ToEnglishName(std, dlt)
    72  	if err != nil {
    73  		t.Fatalf("toEnglishName failed: %s", err)
    74  	}
    75  	if name != want {
    76  		t.Fatalf("english name: %q, want: %q", name, want)
    77  	}
    78  }