Skip to content

Commit

Permalink
fix midnight on 12-hour clocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwilkes committed Jul 13, 2021
1 parent 09329cc commit 553d56c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 4 additions & 2 deletions appenders.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ func (v hourPadded) Append(b []byte, t time.Time) []byte {
if v.twelveHour && h > 12 {
h = h - 12
}
if v.twelveHour && h == 0 {
h = 12
}

if h < 10 {
b = append(b, v.pad)
Expand Down Expand Up @@ -322,7 +325,7 @@ func (v hmsWAMPM) Append(b []byte, t time.Time) []byte {
case h == 12:
// no op
case h > 12:
h = h -12
h = h - 12
default:
am = true
}
Expand All @@ -343,4 +346,3 @@ func (v hmsWAMPM) Append(b []byte, t time.Time) []byte {

return b
}

18 changes: 16 additions & 2 deletions strftime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,12 @@ func TestGHIssue18(t *testing.T) {
for i := 0; i < 24; i++ {
testTime := time.Date(2020, 1, 1, i, 1, 1, 1, time.UTC)
var correctString string
if twelveHour && i > 12 {
switch {
case twelveHour && i == 0:
correctString = fmt.Sprintf("%02d", 12)
case twelveHour && i > 12:
correctString = fmt.Sprintf("%02d", i-12)
} else {
default:
correctString = fmt.Sprintf("%02d", i)
}

Expand Down Expand Up @@ -323,3 +326,14 @@ func TestGHIssue18(t *testing.T) {
t.Run("24 hour zero pad %H", testIH(false))
t.Run("12 hour zero pad %r", testR)
}

func TestFormat12AM(t *testing.T) {
s, err := strftime.Format(`%H %I %l`, time.Time{})
if !assert.NoError(t, err, `strftime.Format succeeds`) {
return
}

if !assert.Equal(t, "00 12 12", s, "correctly format the hour") {
return
}
}

0 comments on commit 553d56c

Please sign in to comment.