localOnce.Do(initTestingZone)
}
+func ZoneinfoForTesting() *string {
+ return zoneinfo
+}
+
+func ResetZoneinfoForTesting() {
+ zoneinfo = nil
+ zoneinfoOnce = sync.Once{}
+}
+
var (
ForceZipFileForTesting = forceZipFileForTesting
ParseTimeZone = parseTimeZone
// NOTE(rsc): Eventually we will need to accept the POSIX TZ environment
// syntax too, but I don't feel like implementing it today.
-var zoneinfo, _ = syscall.Getenv("ZONEINFO")
+var zoneinfo *string
+var zoneinfoOnce sync.Once
// LoadLocation returns the Location with the given name.
//
if name == "Local" {
return Local, nil
}
- if zoneinfo != "" {
- if z, err := loadZoneFile(zoneinfo, name); err == nil {
+ zoneinfoOnce.Do(func() {
+ env, _ := syscall.Getenv("ZONEINFO")
+ zoneinfo = &env
+ })
+ if zoneinfo != nil && *zoneinfo != "" {
+ if z, err := loadZoneFile(*zoneinfo, name); err == nil {
z.name = name
return z, nil
}
package time_test
import (
+ "fmt"
+ "os"
"testing"
"time"
)
+func init() {
+ if time.ZoneinfoForTesting() != nil {
+ panic(fmt.Errorf("zoneinfo initialized before first LoadLocation"))
+ }
+}
+
+func TestEnvVarUsage(t *testing.T) {
+ time.ResetZoneinfoForTesting()
+
+ testZoneinfo := "foo.zip"
+ env := "ZONEINFO"
+
+ defer os.Setenv(env, os.Getenv(env))
+ os.Setenv(env, testZoneinfo)
+
+ // Result isn't important, we're testing the side effect of this command
+ time.LoadLocation("Asia/Jerusalem")
+ defer time.ResetZoneinfoForTesting()
+
+ if zoneinfo := time.ZoneinfoForTesting(); testZoneinfo != *zoneinfo {
+ t.Errorf("zoneinfo does not match env variable: got %q want %q", zoneinfo, testZoneinfo)
+ }
+}
+
func TestVersion3(t *testing.T) {
time.ForceZipFileForTesting(true)
defer time.ForceZipFileForTesting(false)