case stdHour:
p = zeroPad(t.Hour)
case stdHour12:
- // Noon is 12PM.
- if t.Hour == 12 {
- p = "12"
- } else {
- 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:
- if t.Hour == 12 {
- p = "12"
- } else {
- 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:
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.
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:
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:
}
if pmSet && t.Hour < 12 {
t.Hour += 12
+ } else if amSet && t.Hour == 12 {
+ t.Hour = 0
}
return &t, nil
}
}
}
+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 {
}
}
+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) {