Skip to content

Commit c6b8cb4

Browse files
committed
Add partial support to "%x" and "%X" specifier, add "%u" specifier
1 parent 9de6554 commit c6b8cb4

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
sudo: false
2+
23
language: go
4+
35
go:
46
- '1.7'
57
- '1.8'
68
- '1.9'
79
- '1.10'
810
- master
911

12+
matrix:
13+
# It's ok if our code fails on unstable development versions of Go.
14+
allow_failures:
15+
- go: master
16+
# Don't wait for tip tests to finish. Mark the test run green if the
17+
# tests pass on the stable versions of Go.
18+
fast_finish: true
19+
1020
go_import_path: github.com/imperfectgo/go-strftime
1121

1222
notifications:

format.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const (
1717
stdNumMonth // "1"
1818
stdZeroMonth // "01"
1919
stdLongWeekDay // "Monday"
20-
stdNumWeekDay // numerical week representation (0 - Sunday ~ 6 - Saturday)
20+
stdZeroBasedNumWeekDay // numerical week representation (0 - Sunday ~ 6 - Saturday)
21+
stdNumWeekDay // numerical week representation (1 - Monday ~ 7- Sunday)
2122
stdWeekDay // "Mon"
2223
stdDay // "2"
2324
stdUnderDay // "_2"
@@ -137,8 +138,14 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
137138
case stdLongWeekDay:
138139
s := absWeekday(abs).String()
139140
b = append(b, s...)
141+
case stdZeroBasedNumWeekDay:
142+
w := int(absWeekday(abs))
143+
b = appendInt(b, w, 0)
140144
case stdNumWeekDay:
141145
w := int(absWeekday(abs))
146+
if w == 0 {
147+
w = 7
148+
}
142149
b = appendInt(b, w, 0)
143150
case stdDay:
144151
b = appendInt(b, day, 0)
@@ -242,7 +249,7 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
242249
return layout[0:specPos], stdMonth, layout[i+1:]
243250
case 'B': // January
244251
return layout[0:specPos], stdLongMonth, layout[i+1:]
245-
case 'c': // "Mon Jan _2 15:04:05 2006"
252+
case 'c': // "Mon Jan _2 15:04:05 2006" (assumes "C" locale)
246253
return layout[0:specPos], stdYield, "%a %b %e %H:%M:%S %Y" + layout[i+1:]
247254
case 'C': // 20
248255
return layout[0:specPos], stdFirstTwoDigitYear, layout[i+1:]
@@ -287,18 +294,20 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
287294
return layout[0:specPos] + "\t", stdYield, layout[i+1:]
288295
case 'T': // %H:%M:%S
289296
return layout[0:specPos], stdYield, "%H:%M:%S" + layout[i+1:]
290-
//case 'u': // TODO ISO8601 weekday
291-
//return layout[0:specPos], stdNumWeekDay, layout[i+1:]
297+
case 'u': // weekday as a decimal number, where Monday is 1
298+
return layout[0:specPos], stdNumWeekDay, layout[i+1:]
292299
case 'U':
293300
// TODO week of the year as a decimal number (Sunday is the first day of the week)
294301
case 'V':
295302
return layout[0:specPos], stdISO8601Week, layout[i+1:]
296303
case 'w':
297-
return layout[0:specPos], stdNumWeekDay, layout[i+1:]
304+
return layout[0:specPos], stdZeroBasedNumWeekDay, layout[i+1:]
298305
case 'W':
299306
// TODO: week of the year as a decimal number (Monday is the first day of the week)
300-
//case 'x': // locale depended, not supported
301-
//case 'X': // locale depended, not supported
307+
case 'x': // locale depended date representation (assumes "C" locale)
308+
return layout[0:specPos], stdYield, "%m/%d/%y" + layout[i+1:]
309+
case 'X': // locale depended time representation (assumes "C" locale)
310+
return layout[0:specPos], stdYield, "%H:%M:%S" + layout[i+1:]
302311
case 'y':
303312
return layout[0:specPos], stdYear, layout[i+1:]
304313
case 'Y':

format_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 Timon Wong. All rights reserved.
2+
// Copyright 2009 The Go Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package strftime
7+
8+
import (
9+
"testing"
10+
"time"
11+
)
12+
13+
const layout = "%Y-%m-%dT%H:%M:%S.%f%z"
14+
15+
func TestFormat(t *testing.T) {
16+
now := time.Now()
17+
t.Log(now.Format(time.RFC3339Nano))
18+
t.Logf("Format %s -> %s", layout, Format(now, layout))
19+
t.Logf("Format %s -> %s", "%f", Format(now, "%f"))
20+
t.Logf("Format %s -> %s", "%c", Format(now, "%c"))
21+
t.Logf("Format %s -> %s", "%R", Format(now, "%R"))
22+
t.Logf("Format %s -> %s", "%T", Format(now, "%T"))
23+
t.Logf("Format %s -> %s", "%j", Format(now, "%j"))
24+
}

0 commit comments

Comments
 (0)