]> Cypherpunks repositories - gostls13.git/commitdiff
time: midnight is 12AM.
authorRob Pike <r@golang.org>
Fri, 27 May 2011 21:06:53 +0000 (07:06 +1000)
committerRob Pike <r@golang.org>
Fri, 27 May 2011 21:06:53 +0000 (07:06 +1000)
This is the other half of the problem fixed at noon by the previous change.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/4515150

src/pkg/time/format.go
src/pkg/time/time_test.go

index 4b427f9d75faddf6c14c8816527addd3cc226d50..47d73634258dd6328befc49c6462921d756075c6 100644 (file)
@@ -272,18 +272,19 @@ func (t *Time) Format(layout string) string {
                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:
@@ -438,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.
@@ -567,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:
@@ -578,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:
@@ -622,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
 }
index 9c70ad1cc19cb7cd4af011baeb575e7d58efd904..eb676bf64a8ad81295a4c45a5a234af346530b2b 100644 (file)
@@ -315,6 +315,19 @@ func TestNoonIs12PM(t *testing.T) {
        }
 }
 
+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 {
@@ -332,6 +345,23 @@ func Test12PMIsNoon(t *testing.T) {
        }
 }
 
+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) {