mirror of
https://github.com/golang/go
synced 2024-11-22 03:34:40 -07:00
time: fix Format bug: noon is 12PM, not 0PM.
Fixes #1882. R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/4556062
This commit is contained in:
parent
a825e8a69f
commit
406d73876d
@ -272,9 +272,18 @@ func (t *Time) Format(layout string) string {
|
|||||||
case stdHour:
|
case stdHour:
|
||||||
p = zeroPad(t.Hour)
|
p = zeroPad(t.Hour)
|
||||||
case stdHour12:
|
case stdHour12:
|
||||||
p = strconv.Itoa(t.Hour % 12)
|
// Noon is 12PM.
|
||||||
|
if t.Hour == 12 {
|
||||||
|
p = "12"
|
||||||
|
} else {
|
||||||
|
p = strconv.Itoa(t.Hour % 12)
|
||||||
|
}
|
||||||
case stdZeroHour12:
|
case stdZeroHour12:
|
||||||
p = zeroPad(t.Hour % 12)
|
if t.Hour == 12 {
|
||||||
|
p = "12"
|
||||||
|
} else {
|
||||||
|
p = zeroPad(t.Hour % 12)
|
||||||
|
}
|
||||||
case stdMinute:
|
case stdMinute:
|
||||||
p = strconv.Itoa(t.Minute)
|
p = strconv.Itoa(t.Minute)
|
||||||
case stdZeroMinute:
|
case stdZeroMinute:
|
||||||
|
@ -302,6 +302,36 @@ 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 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check that a time without a Zone still produces a (numeric) time zone
|
// Check that a time without a Zone still produces a (numeric) time zone
|
||||||
// when formatted with MST as a requested zone.
|
// when formatted with MST as a requested zone.
|
||||||
func TestMissingZone(t *testing.T) {
|
func TestMissingZone(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user