Skip to content

Commit d920120

Browse files
committed
Add support to %j (year day) specifier
1 parent 6e6fe90 commit d920120

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

format.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const (
3232
stdLongYear = iota + stdNeedDate // "2006"
3333
stdYear // "06"
3434
stdFirstTwoDigitYear // "20"
35-
stdISO8601WeekYear = iota + stdNeedISOISO8601Week // last two digitsof ISO 8601 week-based year
35+
stdYearDay // day of the year as a decimal number (range [001,366])
36+
stdISO8601WeekYear = iota + stdNeedISOISO8601Week // last two digits of ISO 8601 week-based year
3637
stdISO8601LongWeekYear // ISO 8601 week-based year
3738
stdISO8601Week // ISO 8601 week
3839
stdPM = iota + stdNeedClock // "PM"
@@ -60,13 +61,14 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
6061
var (
6162
name, offset, abs = locabs(&t)
6263

63-
year int = -1
64+
year = -1
6465
month time.Month
6566
day int
66-
hour int = -1
67+
yday int
68+
hour = -1
6769
min int
6870
sec int
69-
iso8601WeekYear int = -1
71+
iso8601WeekYear = -1
7072
iso8601Week int
7173
)
7274

@@ -83,7 +85,7 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
8385

8486
// Compute year, month, day if needed.
8587
if year < 0 && std&stdNeedDate != 0 {
86-
year, month, day, _ = absDate(abs, true)
88+
year, month, day, yday = absDate(abs, true)
8789
}
8890

8991
// Compute hour, minute, second if needed.
@@ -115,6 +117,8 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
115117
b = appendInt(b, year, 4)
116118
case stdFirstTwoDigitYear:
117119
b = appendInt(b, year/100, 2)
120+
case stdYearDay:
121+
b = appendInt(b, yday, 3)
118122
case stdMonth:
119123
b = append(b, month.String()[:3]...)
120124
case stdLongMonth:
@@ -214,9 +218,7 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
214218
// nextStdChunk finds the first occurrence of a std string in
215219
// layout and returns the text before, the std string, and the text after.
216220
func nextStdChunk(layout string) (prefix string, std int, suffix string) {
217-
var (
218-
specPos int = -1
219-
)
221+
specPos := -1
220222

221223
for i := 0; i < len(layout); i++ {
222224
c := int(layout[i])
@@ -243,15 +245,15 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
243245
case 'd': // 02
244246
return layout[0:specPos], stdZeroDay, layout[i+1:]
245247
case 'D': // %m/%d/%y
246-
return layout[0:specPos], stdZeroMonth, "/%d/%y" + layout[i+1:]
248+
return layout[0:specPos], stdYield, "%m/%d/%y" + layout[i+1:]
247249
case 'e': // _2
248250
return layout[0:specPos], stdUnderDay, layout[i+1:]
249251
case 'f': // fraction seconds in microseconds (Python)
250252
std = stdFracSecond0
251253
std |= 6 << stdArgShift // microseconds precision
252254
return layout[0:specPos], std, layout[i+1:]
253255
case 'F': // %Y-%m-%d
254-
return layout[0:specPos], stdLongYear, "-%m-%d" + layout[i+1:]
256+
return layout[0:specPos], stdYield, "%Y-%m-%d" + layout[i+1:]
255257
case 'g':
256258
return layout[0:specPos], stdISO8601WeekYear, layout[i+1:]
257259
case 'G':
@@ -261,7 +263,7 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
261263
case 'I':
262264
return layout[0:specPos], stdZeroHour12, layout[i+1:]
263265
case 'j':
264-
// TODO: day of the year as a decimal number (range [001,366])
266+
return layout[0:specPos], stdYearDay, layout[i+1:]
265267
case 'm':
266268
return layout[0:specPos], stdZeroMonth, layout[i+1:]
267269
case 'M':
@@ -290,7 +292,7 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
290292
case 'w':
291293
return layout[0:specPos], stdNumWeekDay, layout[i+1:]
292294
case 'W':
293-
// TODO: week of the year as a decimal number (Monday is the first day of the wee)
295+
// TODO: week of the year as a decimal number (Monday is the first day of the week)
294296
//case 'x': // locale depended, not supported
295297
//case 'X': // locale depended, not supported
296298
case 'y':

0 commit comments

Comments
 (0)