]> Cypherpunks repositories - gostls13.git/commitdiff
time: fix zone during windows test
authorRuss Cox <rsc@golang.org>
Fri, 26 Aug 2011 19:15:23 +0000 (15:15 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 26 Aug 2011 19:15:23 +0000 (15:15 -0400)
Factor out sleep interrupt.

Fixes #1109.

R=alex.brainman, go.peter.90, mattn.jp
CC=golang-dev
https://golang.org/cl/4968041

src/pkg/Makefile
src/pkg/time/Makefile
src/pkg/time/internal_test.go [new file with mode: 0644]
src/pkg/time/sleep_test.go
src/pkg/time/sys_plan9.go
src/pkg/time/sys_unix.go [new file with mode: 0644]
src/pkg/time/sys_windows.go [moved from src/pkg/time/sys_posix.go with 84% similarity]
src/pkg/time/time_test.go
src/pkg/time/zoneinfo_plan9.go
src/pkg/time/zoneinfo_unix.go
src/pkg/time/zoneinfo_windows.go

index 84d5607837f0456aadfe6993e9e2286873b58182..9bd920ea08d0e7b8f7d15612fb787c38fd964727 100644 (file)
@@ -230,7 +230,6 @@ NOBENCH+=\
 ifeq ($(GOOS),windows)
 NOTEST+=os/signal    # no signals
 NOTEST+=syslog       # no network
-NOTEST+=time         # no syscall.Kill, syscall.SIGCHLD for sleep tests
 endif
 
 TEST=\
index a6fce3fa1ffce019fe6082395f4f9234e67f0fa4..473e7ea937f338bca8d61e64411029b5e0279a2c 100644 (file)
@@ -13,27 +13,27 @@ GOFILES=\
        time.go\
 
 GOFILES_freebsd=\
-       sys_posix.go\
+       sys_unix.go\
        zoneinfo_posix.go\
        zoneinfo_unix.go\
 
 GOFILES_darwin=\
-       sys_posix.go\
+       sys_unix.go\
        zoneinfo_posix.go\
        zoneinfo_unix.go\
 
 GOFILES_linux=\
-       sys_posix.go\
+       sys_unix.go\
        zoneinfo_posix.go\
        zoneinfo_unix.go\
 
 GOFILES_openbsd=\
-       sys_posix.go\
+       sys_unix.go\
        zoneinfo_posix.go\
        zoneinfo_unix.go\
 
 GOFILES_windows=\
-       sys_posix.go\
+       sys_windows.go\
        zoneinfo_windows.go\
 
 GOFILES_plan9=\
diff --git a/src/pkg/time/internal_test.go b/src/pkg/time/internal_test.go
new file mode 100644 (file)
index 0000000..d7e7076
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package time
+
+func init() {
+       // force US/Pacific for time zone tests
+       onceSetupZone.Do(setupTestingZone)
+}
+
+var Interrupt = interrupt
index a4a1a429fde1c4a34c6f0313c8cf7d3ab9103c93..b6b88f6cab64e12b0fdc6d789706c284b4c729b0 100644 (file)
@@ -7,7 +7,6 @@ package time_test
 import (
        "fmt"
        "os"
-       "syscall"
        "testing"
        "sort"
        . "time"
@@ -17,7 +16,7 @@ func TestSleep(t *testing.T) {
        const delay = int64(100e6)
        go func() {
                Sleep(delay / 2)
-               syscall.Kill(os.Getpid(), syscall.SIGCHLD)
+               Interrupt()
        }()
        start := Nanoseconds()
        Sleep(delay)
index abe8649a24bbc3934f2ff0d90bc6fa5bbb31e242..9ae0161ba84a200b938222abc4034c55a1a6697e 100644 (file)
@@ -16,3 +16,8 @@ func sysSleep(t int64) os.Error {
        }
        return nil
 }
+
+// for testing: whatever interrupts a sleep
+func interrupt() {
+       // cannot predict pid, don't want to kill group
+}
diff --git a/src/pkg/time/sys_unix.go b/src/pkg/time/sys_unix.go
new file mode 100644 (file)
index 0000000..0f9128e
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package time
+
+import (
+       "os"
+       "syscall"
+)
+
+func sysSleep(t int64) os.Error {
+       errno := syscall.Sleep(t)
+       if errno != 0 && errno != syscall.EINTR {
+               return os.NewSyscallError("sleep", errno)
+       }
+       return nil
+}
+
+// for testing: whatever interrupts a sleep
+func interrupt() {
+       syscall.Kill(os.Getpid(), syscall.SIGCHLD)
+}
similarity index 84%
rename from src/pkg/time/sys_posix.go
rename to src/pkg/time/sys_windows.go
index 0d1eb72fcf5f4e96f2fb84602f1fe9848d02eead..feff90b8b09ba6c8945d6f539d4a15e87d2ac065 100644 (file)
@@ -16,3 +16,7 @@ func sysSleep(t int64) os.Error {
        }
        return nil
 }
+
+// for testing: whatever interrupts a sleep
+func interrupt() {
+}
index dceed491aa0567ad692c90cf20163b1d6dc96cf2..07d75983324492a38ac9afc61c328885902c8fda 100644 (file)
@@ -5,7 +5,6 @@
 package time_test
 
 import (
-       "os"
        "strconv"
        "strings"
        "testing"
@@ -13,13 +12,6 @@ import (
        . "time"
 )
 
-func init() {
-       // Force US Pacific time for daylight-savings
-       // tests below (localtests).  Needs to be set
-       // before the first call into the time library.
-       os.Setenv("TZ", "America/Los_Angeles")
-}
-
 // We should be in PST/PDT, but if the time zone files are missing we
 // won't be. The purpose of this test is to at least explain why some of
 // the subsequent tests fail.
index 3c3e7c42446f98c03fc992a88c35726b1e51579f..57e605e0d63efef9755fd2893b588c468c556c53 100644 (file)
@@ -57,3 +57,19 @@ func setupZone() {
        }
        zones = parseZones(t)
 }
+
+func setupTestingZone() {
+       f, err := os.Open("/adm/timezone/US_Pacific")
+       if err != nil {
+               return
+       }
+       defer f.Close()
+       l, _ := f.Seek(0, 2)
+       f.Seek(0, 0)
+       buf := make([]byte, l)
+       _, err := f.Read(buf)
+       if err != nil {
+               return
+       }
+       zones = parseZones(buf)
+}
index f3ea7b6fdabe4d807f634f0d201eb8d8e3c16155..ce4d9f13af164e2390e2eacb1c3b3998d4a44e79 100644 (file)
@@ -185,6 +185,11 @@ func readinfofile(name string) ([]zonetime, bool) {
        return parseinfo(buf)
 }
 
+func setupTestingZone() {
+       os.Setenv("TZ", "America/Los_Angeles")
+       setupZone()
+}
+
 func setupZone() {
        // consult $TZ to find the time zone to use.
        // no $TZ means use the system default /etc/localtime.
index fabc006011064e2a15a633d30f35ab217768aca1..ab3e7df59cc65f2b2b9ce701b1ebf5010b488b67 100644 (file)
@@ -27,9 +27,30 @@ type zone struct {
        prev                  *zone
 }
 
+// BUG(rsc): On Windows, time zone abbreviations are unavailable.
+// This package constructs them using the capital letters from a longer
+// time zone description.
+
 // Populate zone struct with Windows supplied information. Returns true, if data is valid.
 func (z *zone) populate(bias, biasdelta int32, d *syscall.Systemtime, name []uint16) (dateisgood bool) {
-       z.name = syscall.UTF16ToString(name)
+       // name is 'Pacific Standard Time' but we want 'PST'.
+       // Extract just capital letters.  It's not perfect but the
+       // information we need is not available from the kernel.
+       // Because time zone abbreviations are not unique,
+       // Windows refuses to expose them.
+       //
+       // http://social.msdn.microsoft.com/Forums/eu/vclanguage/thread/a87e1d25-fb71-4fe0-ae9c-a9578c9753eb
+       // http://stackoverflow.com/questions/4195948/windows-time-zone-abbreviations-in-asp-net
+       short := make([]uint16, len(name))
+       w := 0
+       for _, c := range name {
+               if 'A' <= c && c <= 'Z' {
+                       short[w] = c
+                       w++
+               }
+       }
+       z.name = syscall.UTF16ToString(short[:w])
+
        z.offset = int(bias)
        z.year = int64(d.Year)
        z.month = int(d.Month)
@@ -129,6 +150,10 @@ func setupZone() {
                initError = os.NewSyscallError("GetTimeZoneInformation", e)
                return
        }
+       setupZoneFromTZI(&i)
+}
+
+func setupZoneFromTZI(i *syscall.Timezoneinformation) {
        if !tz.std.populate(i.Bias, i.StandardBias, &i.StandardDate, i.StandardName[0:]) {
                tz.disabled = true
                tz.offsetIfDisabled = tz.std.offset
@@ -144,6 +169,23 @@ func setupZone() {
        tz.januaryIsStd = tz.dst.cutoffSeconds(t.Year) < tz.std.cutoffSeconds(t.Year)
 }
 
+var usPacific = syscall.Timezoneinformation{
+       Bias: 8 * 60,
+       StandardName: [32]uint16{
+               'P', 'a', 'c', 'i', 'f', 'i', 'c', ' ', 'S', 't', 'a', 'n', 'd', 'a', 'r', 'd', ' ', 'T', 'i', 'm', 'e',
+       },
+       StandardDate: syscall.Systemtime{Month: 11, Day: 1, Hour: 2},
+       DaylightName: [32]uint16{
+               'P', 'a', 'c', 'i', 'f', 'i', 'c', ' ', 'D', 'a', 'y', 'l', 'i', 'g', 'h', 't', ' ', 'T', 'i', 'm', 'e',
+       },
+       DaylightDate: syscall.Systemtime{Month: 3, Day: 2, Hour: 2},
+       DaylightBias: -60,
+}
+
+func setupTestingZone() {
+       setupZoneFromTZI(&usPacific)
+}
+
 // Look up the correct time zone (daylight savings or not) for the given unix time, in the current location.
 func lookupTimezone(sec int64) (zone string, offset int) {
        onceSetupZone.Do(setupZone)