summaryrefslogtreecommitdiff
path: root/src/pkg/time
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/time')
-rw-r--r--src/pkg/time/Makefile12
-rw-r--r--src/pkg/time/format.go4
-rw-r--r--src/pkg/time/sleep_test.go2
-rw-r--r--src/pkg/time/sys.go13
-rw-r--r--src/pkg/time/sys_plan9.go18
-rw-r--r--src/pkg/time/sys_posix.go18
-rw-r--r--src/pkg/time/tick.go2
-rw-r--r--src/pkg/time/time_test.go17
-rw-r--r--src/pkg/time/zoneinfo_plan9.go59
-rw-r--r--src/pkg/time/zoneinfo_posix.go62
-rw-r--r--src/pkg/time/zoneinfo_unix.go56
11 files changed, 183 insertions, 80 deletions
diff --git a/src/pkg/time/Makefile b/src/pkg/time/Makefile
index 3fa96065e..023e8775e 100644
--- a/src/pkg/time/Makefile
+++ b/src/pkg/time/Makefile
@@ -13,17 +13,29 @@ GOFILES=\
time.go\
GOFILES_freebsd=\
+ sys_posix.go\
+ zoneinfo_posix.go\
zoneinfo_unix.go\
GOFILES_darwin=\
+ sys_posix.go\
+ zoneinfo_posix.go\
zoneinfo_unix.go\
GOFILES_linux=\
+ sys_posix.go\
+ zoneinfo_posix.go\
zoneinfo_unix.go\
GOFILES_windows=\
+ sys_posix.go\
zoneinfo_windows.go\
+GOFILES_plan9=\
+ sys_plan9.go\
+ zoneinfo_posix.go\
+ zoneinfo_plan9.go\
+
GOFILES+=$(GOFILES_$(GOOS))
include ../../Make.pkg
diff --git a/src/pkg/time/format.go b/src/pkg/time/format.go
index 47d736342..26f40d141 100644
--- a/src/pkg/time/format.go
+++ b/src/pkg/time/format.go
@@ -248,7 +248,7 @@ func (t *Time) Format(layout string) string {
var p string
switch std {
case stdYear:
- p = strconv.Itoa64(t.Year % 100)
+ p = zeroPad(int(t.Year % 100))
case stdLongYear:
p = strconv.Itoa64(t.Year)
case stdMonth:
@@ -355,7 +355,7 @@ func (t *Time) String() string {
return t.Format(UnixDate)
}
-var errBad = os.ErrorString("bad") // just a marker; not returned to user
+var errBad = os.NewError("bad") // just a marker; not returned to user
// ParseError describes a problem parsing a time string.
type ParseError struct {
diff --git a/src/pkg/time/sleep_test.go b/src/pkg/time/sleep_test.go
index 25e79f9fb..a4a1a429f 100644
--- a/src/pkg/time/sleep_test.go
+++ b/src/pkg/time/sleep_test.go
@@ -172,7 +172,7 @@ func testAfterQueuing(t *testing.T) os.Error {
for _, slot := range slots {
go await(slot, result, After(int64(slot)*Delta))
}
- sort.SortInts(slots)
+ sort.Ints(slots)
for _, slot := range slots {
r := <-result
if r.slot != slot {
diff --git a/src/pkg/time/sys.go b/src/pkg/time/sys.go
index 63f4cbf3d..9fde3b3b6 100644
--- a/src/pkg/time/sys.go
+++ b/src/pkg/time/sys.go
@@ -4,10 +4,7 @@
package time
-import (
- "os"
- "syscall"
-)
+import "os"
// Seconds reports the number of seconds since the Unix epoch,
// January 1, 1970 00:00:00 UTC.
@@ -52,11 +49,3 @@ func sleep(t, ns int64) (int64, os.Error) {
}
return t, nil
}
-
-func sysSleep(t int64) os.Error {
- errno := syscall.Sleep(t)
- if errno != 0 && errno != syscall.EINTR {
- return os.NewSyscallError("sleep", errno)
- }
- return nil
-}
diff --git a/src/pkg/time/sys_plan9.go b/src/pkg/time/sys_plan9.go
new file mode 100644
index 000000000..abe8649a2
--- /dev/null
+++ b/src/pkg/time/sys_plan9.go
@@ -0,0 +1,18 @@
+// 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 {
+ err := syscall.Sleep(t)
+ if err != nil {
+ return os.NewSyscallError("sleep", err)
+ }
+ return nil
+}
diff --git a/src/pkg/time/sys_posix.go b/src/pkg/time/sys_posix.go
new file mode 100644
index 000000000..0d1eb72fc
--- /dev/null
+++ b/src/pkg/time/sys_posix.go
@@ -0,0 +1,18 @@
+// 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
+}
diff --git a/src/pkg/time/tick.go b/src/pkg/time/tick.go
index dde18000d..852bae9c9 100644
--- a/src/pkg/time/tick.go
+++ b/src/pkg/time/tick.go
@@ -160,7 +160,7 @@ var onceStartTickerLoop sync.Once
// ns must be greater than zero; if not, NewTicker will panic.
func NewTicker(ns int64) *Ticker {
if ns <= 0 {
- panic(os.ErrorString("non-positive interval for NewTicker"))
+ panic(os.NewError("non-positive interval for NewTicker"))
}
c := make(chan int64, 1) // See comment on send in tickerLoop
t := &Ticker{
diff --git a/src/pkg/time/time_test.go b/src/pkg/time/time_test.go
index eb676bf64..eec8a7a5c 100644
--- a/src/pkg/time/time_test.go
+++ b/src/pkg/time/time_test.go
@@ -142,21 +142,22 @@ type FormatTest struct {
}
var formatTests = []FormatTest{
- {"ANSIC", ANSIC, "Thu Feb 4 21:00:57 2010"},
- {"UnixDate", UnixDate, "Thu Feb 4 21:00:57 PST 2010"},
- {"RubyDate", RubyDate, "Thu Feb 04 21:00:57 -0800 2010"},
- {"RFC822", RFC822, "04 Feb 10 2100 PST"},
- {"RFC850", RFC850, "Thursday, 04-Feb-10 21:00:57 PST"},
- {"RFC1123", RFC1123, "Thu, 04 Feb 2010 21:00:57 PST"},
- {"RFC3339", RFC3339, "2010-02-04T21:00:57-08:00"},
+ {"ANSIC", ANSIC, "Wed Feb 4 21:00:57 2009"},
+ {"UnixDate", UnixDate, "Wed Feb 4 21:00:57 PST 2009"},
+ {"RubyDate", RubyDate, "Wed Feb 04 21:00:57 -0800 2009"},
+ {"RFC822", RFC822, "04 Feb 09 2100 PST"},
+ {"RFC850", RFC850, "Wednesday, 04-Feb-09 21:00:57 PST"},
+ {"RFC1123", RFC1123, "Wed, 04 Feb 2009 21:00:57 PST"},
+ {"RFC3339", RFC3339, "2009-02-04T21:00:57-08:00"},
{"Kitchen", Kitchen, "9:00PM"},
{"am/pm", "3pm", "9pm"},
{"AM/PM", "3PM", "9PM"},
+ {"two-digit year", "06 01 02", "09 02 04"},
}
func TestFormat(t *testing.T) {
// The numeric time represents Thu Feb 4 21:00:57 PST 2010
- time := SecondsToLocalTime(1265346057)
+ time := SecondsToLocalTime(1233810057)
for _, test := range formatTests {
result := time.Format(test.format)
if result != test.result {
diff --git a/src/pkg/time/zoneinfo_plan9.go b/src/pkg/time/zoneinfo_plan9.go
new file mode 100644
index 000000000..3c3e7c424
--- /dev/null
+++ b/src/pkg/time/zoneinfo_plan9.go
@@ -0,0 +1,59 @@
+// 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.
+
+// Parse Plan 9 timezone(2) files.
+
+package time
+
+import (
+ "os"
+ "strconv"
+ "strings"
+)
+
+func parseZones(s string) (zt []zonetime) {
+ f := strings.Fields(s)
+ if len(f) < 4 {
+ return
+ }
+
+ // standard timezone offset
+ o, err := strconv.Atoi(f[1])
+ if err != nil {
+ return
+ }
+ std := &zone{name: f[0], utcoff: o, isdst: false}
+
+ // alternate timezone offset
+ o, err = strconv.Atoi(f[3])
+ if err != nil {
+ return
+ }
+ dst := &zone{name: f[2], utcoff: o, isdst: true}
+
+ // transition time pairs
+ f = f[4:]
+ for i := 0; i < len(f); i++ {
+ z := std
+ if i%2 == 0 {
+ z = dst
+ }
+ t, err := strconv.Atoi(f[i])
+ if err != nil {
+ return nil
+ }
+ t -= std.utcoff
+ zt = append(zt, zonetime{time: int32(t), zone: z})
+ }
+ return
+}
+
+func setupZone() {
+ t, err := os.Getenverror("timezone")
+ if err != nil {
+ // do nothing: use UTC
+ return
+ }
+ zones = parseZones(t)
+}
diff --git a/src/pkg/time/zoneinfo_posix.go b/src/pkg/time/zoneinfo_posix.go
new file mode 100644
index 000000000..b49216410
--- /dev/null
+++ b/src/pkg/time/zoneinfo_posix.go
@@ -0,0 +1,62 @@
+// 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 "sync"
+
+// Parsed representation
+type zone struct {
+ utcoff int
+ isdst bool
+ name string
+}
+
+type zonetime struct {
+ time int32 // transition time, in seconds since 1970 GMT
+ zone *zone // the zone that goes into effect at that time
+ isstd, isutc bool // ignored - no idea what these mean
+}
+
+var zones []zonetime
+var onceSetupZone sync.Once
+
+// 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)
+ if len(zones) == 0 {
+ return "UTC", 0
+ }
+
+ // Binary search for entry with largest time <= sec
+ tz := zones
+ for len(tz) > 1 {
+ m := len(tz) / 2
+ if sec < int64(tz[m].time) {
+ tz = tz[0:m]
+ } else {
+ tz = tz[m:]
+ }
+ }
+ z := tz[0].zone
+ return z.name, z.utcoff
+}
+
+// lookupByName returns the time offset for the
+// time zone with the given abbreviation. It only considers
+// time zones that apply to the current system.
+// For example, for a system configured as being in New York,
+// it only recognizes "EST" and "EDT".
+// For a system in San Francisco, "PST" and "PDT".
+// For a system in Sydney, "EST" and "EDT", though they have
+// different meanings than they do in New York.
+func lookupByName(name string) (off int, found bool) {
+ onceSetupZone.Do(setupZone)
+ for _, z := range zones {
+ if name == z.zone.name {
+ return z.zone.utcoff, true
+ }
+ }
+ return 0, false
+}
diff --git a/src/pkg/time/zoneinfo_unix.go b/src/pkg/time/zoneinfo_unix.go
index 42659ed60..2a83e0c16 100644
--- a/src/pkg/time/zoneinfo_unix.go
+++ b/src/pkg/time/zoneinfo_unix.go
@@ -12,7 +12,6 @@ package time
import (
"io/ioutil"
"os"
- "sync"
)
const (
@@ -66,19 +65,6 @@ func byteString(p []byte) string {
return string(p)
}
-// Parsed representation
-type zone struct {
- utcoff int
- isdst bool
- name string
-}
-
-type zonetime struct {
- time int32 // transition time, in seconds since 1970 GMT
- zone *zone // the zone that goes into effect at that time
- isstd, isutc bool // ignored - no idea what these mean
-}
-
func parseinfo(bytes []byte) (zt []zonetime, ok bool) {
d := data{bytes, false}
@@ -201,9 +187,6 @@ func readinfofile(name string) ([]zonetime, bool) {
return parseinfo(buf)
}
-var zones []zonetime
-var onceSetupZone sync.Once
-
func setupZone() {
// consult $TZ to find the time zone to use.
// no $TZ means use the system default /etc/localtime.
@@ -230,42 +213,3 @@ func setupZone() {
// do nothing: use UTC
}
}
-
-// 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)
- if len(zones) == 0 {
- return "UTC", 0
- }
-
- // Binary search for entry with largest time <= sec
- tz := zones
- for len(tz) > 1 {
- m := len(tz) / 2
- if sec < int64(tz[m].time) {
- tz = tz[0:m]
- } else {
- tz = tz[m:]
- }
- }
- z := tz[0].zone
- return z.name, z.utcoff
-}
-
-// lookupByName returns the time offset for the
-// time zone with the given abbreviation. It only considers
-// time zones that apply to the current system.
-// For example, for a system configured as being in New York,
-// it only recognizes "EST" and "EDT".
-// For a system in San Francisco, "PST" and "PDT".
-// For a system in Sydney, "EST" and "EDT", though they have
-// different meanings than they do in New York.
-func lookupByName(name string) (off int, found bool) {
- onceSetupZone.Do(setupZone)
- for _, z := range zones {
- if name == z.zone.name {
- return z.zone.utcoff, true
- }
- }
- return 0, false
-}