summaryrefslogtreecommitdiff
path: root/src/pkg/time
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-06-30 15:34:22 +0200
committerOndřej Surý <ondrej@sury.org>2011-06-30 15:34:22 +0200
commitd39f5aa373a4422f7a5f3ee764fb0f6b0b719d61 (patch)
tree1833f8b72a4b3a8f00d0d143b079a8fcad01c6ae /src/pkg/time
parent8652e6c371b8905498d3d314491d36c58d5f68d5 (diff)
downloadgolang-d39f5aa373a4422f7a5f3ee764fb0f6b0b719d61.tar.gz
Imported Upstream version 58upstream/58
Diffstat (limited to 'src/pkg/time')
-rw-r--r--src/pkg/time/format.go31
-rw-r--r--src/pkg/time/sleep.go2
-rw-r--r--src/pkg/time/tick.go2
-rw-r--r--src/pkg/time/time_test.go60
-rw-r--r--src/pkg/time/zoneinfo_windows.go8
5 files changed, 91 insertions, 12 deletions
diff --git a/src/pkg/time/format.go b/src/pkg/time/format.go
index 7b5a8f3b6..47d736342 100644
--- a/src/pkg/time/format.go
+++ b/src/pkg/time/format.go
@@ -272,9 +272,19 @@ func (t *Time) Format(layout string) string {
case stdHour:
p = zeroPad(t.Hour)
case stdHour12:
- p = strconv.Itoa(t.Hour % 12)
+ // Noon is 12PM, midnight is 12AM.
+ hr := t.Hour % 12
+ if hr == 0 {
+ hr = 12
+ }
+ p = strconv.Itoa(hr)
case stdZeroHour12:
- p = zeroPad(t.Hour % 12)
+ // Noon is 12PM, midnight is 12AM.
+ hr := t.Hour % 12
+ if hr == 0 {
+ hr = 12
+ }
+ p = zeroPad(hr)
case stdMinute:
p = strconv.Itoa(t.Minute)
case stdZeroMinute:
@@ -429,6 +439,7 @@ func skip(value, prefix string) (string, os.Error) {
func Parse(alayout, avalue string) (*Time, os.Error) {
var t Time
rangeErrString := "" // set if a value is out of range
+ amSet := false // do we need to subtract 12 from the hour for midnight?
pmSet := false // do we need to add 12 to the hour?
layout, value := alayout, avalue
// Each iteration processes one std value.
@@ -558,9 +569,12 @@ func Parse(alayout, avalue string) (*Time, os.Error) {
break
}
p, value = value[0:2], value[2:]
- if p == "PM" {
+ switch p {
+ case "PM":
pmSet = true
- } else if p != "AM" {
+ case "AM":
+ amSet = true
+ default:
err = errBad
}
case stdpm:
@@ -569,9 +583,12 @@ func Parse(alayout, avalue string) (*Time, os.Error) {
break
}
p, value = value[0:2], value[2:]
- if p == "pm" {
+ switch p {
+ case "pm":
pmSet = true
- } else if p != "am" {
+ case "am":
+ amSet = true
+ default:
err = errBad
}
case stdTZ:
@@ -613,6 +630,8 @@ func Parse(alayout, avalue string) (*Time, os.Error) {
}
if pmSet && t.Hour < 12 {
t.Hour += 12
+ } else if amSet && t.Hour == 12 {
+ t.Hour = 0
}
return &t, nil
}
diff --git a/src/pkg/time/sleep.go b/src/pkg/time/sleep.go
index 3bc253c94..314622d0d 100644
--- a/src/pkg/time/sleep.go
+++ b/src/pkg/time/sleep.go
@@ -91,7 +91,7 @@ func (e *Timer) Stop() (ok bool) {
// It assumes that f will not block.
func after(ns int64, f func(int64)) (e *Timer) {
now := Nanoseconds()
- t := Nanoseconds() + ns
+ t := now + ns
if ns > 0 && t < now {
panic("time: time overflow")
}
diff --git a/src/pkg/time/tick.go b/src/pkg/time/tick.go
index 6c21bf19b..dde18000d 100644
--- a/src/pkg/time/tick.go
+++ b/src/pkg/time/tick.go
@@ -88,7 +88,7 @@ func wakeLoop(wakeMeAt chan int64, wakeUp chan bool) {
// A single tickerLoop serves all ticks to Tickers. It waits for two events:
// either the creation of a new Ticker or a tick from the alarm,
-// signalling a time to wake up one or more Tickers.
+// signaling a time to wake up one or more Tickers.
func tickerLoop() {
// Represents the next alarm to be delivered.
var alarm alarmer
diff --git a/src/pkg/time/time_test.go b/src/pkg/time/time_test.go
index 1d83291c0..eb676bf64 100644
--- a/src/pkg/time/time_test.go
+++ b/src/pkg/time/time_test.go
@@ -302,6 +302,66 @@ func TestParseErrors(t *testing.T) {
}
}
+func TestNoonIs12PM(t *testing.T) {
+ noon := Time{Hour: 12}
+ const expect = "12:00PM"
+ got := noon.Format("3:04PM")
+ if got != expect {
+ t.Errorf("got %q; expect %q", got, expect)
+ }
+ got = noon.Format("03:04PM")
+ if got != expect {
+ t.Errorf("got %q; expect %q", got, expect)
+ }
+}
+
+func TestMidnightIs12AM(t *testing.T) {
+ midnight := Time{Hour: 0}
+ expect := "12:00AM"
+ got := midnight.Format("3:04PM")
+ if got != expect {
+ t.Errorf("got %q; expect %q", got, expect)
+ }
+ got = midnight.Format("03:04PM")
+ if got != expect {
+ t.Errorf("got %q; expect %q", got, expect)
+ }
+}
+
+func Test12PMIsNoon(t *testing.T) {
+ noon, err := Parse("3:04PM", "12:00PM")
+ if err != nil {
+ t.Fatal("error parsing date:", err)
+ }
+ if noon.Hour != 12 {
+ t.Errorf("got %d; expect 12", noon.Hour)
+ }
+ noon, err = Parse("03:04PM", "12:00PM")
+ if err != nil {
+ t.Fatal("error parsing date:", err)
+ }
+ if noon.Hour != 12 {
+ t.Errorf("got %d; expect 12", noon.Hour)
+ }
+}
+
+func Test12AMIsMidnight(t *testing.T) {
+ midnight, err := Parse("3:04PM", "12:00AM")
+ if err != nil {
+ t.Fatal("error parsing date:", err)
+ }
+ if midnight.Hour != 0 {
+ t.Errorf("got %d; expect 0", midnight.Hour)
+ }
+ midnight, err = Parse("03:04PM", "12:00AM")
+ if err != nil {
+ t.Fatal("error parsing date:", err)
+ }
+ if midnight.Hour != 0 {
+ t.Errorf("got %d; expect 0", midnight.Hour)
+ }
+}
+
// Check that a time without a Zone still produces a (numeric) time zone
// when formatted with MST as a requested zone.
func TestMissingZone(t *testing.T) {
diff --git a/src/pkg/time/zoneinfo_windows.go b/src/pkg/time/zoneinfo_windows.go
index c357eec62..83afdfb02 100644
--- a/src/pkg/time/zoneinfo_windows.go
+++ b/src/pkg/time/zoneinfo_windows.go
@@ -14,7 +14,7 @@ import (
// this year's rules for daylight savings time apply to all previous
// and future years as well.
-// TODO(brainman): use GetDynamicTimeZoneInformation, whenever posible (Vista and up),
+// TODO(brainman): use GetDynamicTimeZoneInformation, whenever possible (Vista and up),
// to improve on situation described in the bug above.
type zone struct {
@@ -46,7 +46,7 @@ func (z *zone) populate(bias, biasdelta int32, d *syscall.Systemtime, name []uin
return
}
-// Pre-calculte cutoff time in seconds since the Unix epoch, if data is supplied in "absolute" format.
+// Pre-calculate cutoff time in seconds since the Unix epoch, if data is supplied in "absolute" format.
func (z *zone) preCalculateAbsSec() {
if z.year != 0 {
z.abssec = (&Time{z.year, int(z.month), int(z.day), int(z.hour), int(z.minute), int(z.second), 0, 0, ""}).Seconds()
@@ -55,7 +55,7 @@ func (z *zone) preCalculateAbsSec() {
}
}
-// Convert zone cutoff time to sec in number of seconds since the Unix epoch, given particualar year.
+// Convert zone cutoff time to sec in number of seconds since the Unix epoch, given particular year.
func (z *zone) cutoffSeconds(year int64) int64 {
// Windows specifies daylight savings information in "day in month" format:
// z.month is month number (1-12)
@@ -96,7 +96,7 @@ func (z *zone) isBeforeCutoff(t *Time) bool {
}
type zoneinfo struct {
- disabled bool // daylight saving time is not used localy
+ disabled bool // daylight saving time is not used locally
offsetIfDisabled int
januaryIsStd bool // is january 1 standard time?
std, dst zone