Skip to content

Commit 6e6fe90

Browse files
authored
Merge pull request #2 from timonwong/add-newline-tab
Add support to %n and %t
2 parents 7ce1e2c + 863f8df commit 6e6fe90

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ High performance C99-compatible `strftime` formatter for Go.
44

55
**EXPERIMENTAL** Please DO NOT USE for now.
66

7+
## Performance
8+
9+
Just for reference :P
10+
11+
```
12+
> go test -bench Bench -cpu 4 -benchmem .
13+
14+
goos: darwin
15+
goarch: amd64
16+
pkg: github.com/imperfectgo/go-strftime
17+
BenchmarkStdTimeFormat-4 5000000 356 ns/op 48 B/op 1 allocs/op
18+
BenchmarkGoStrftime-4 5000000 347 ns/op 32 B/op 1 allocs/op
19+
PASS
20+
ok github.com/imperfectgo/go-strftime 4.245s
21+
```
22+
723
## License
824

925
See [License](./LICENSE) file.

format.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
const (
1313
_ = iota
14+
stdYield // Yielded chunk
1415
stdLongMonth = iota + stdNeedDate // "January"
1516
stdMonth // "Jan"
1617
stdNumMonth // "1"
@@ -96,6 +97,8 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
9697
}
9798

9899
switch std & stdMask {
100+
case stdYield:
101+
continue
99102
case stdISO8601WeekYear:
100103
b = appendInt(b, iso8601WeekYear/100, 2)
101104
case stdISO8601LongWeekYear:
@@ -176,11 +179,9 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
176179
}
177180
case stdNumTZ:
178181
zone := offset / 60 // convert to minutes
179-
absoffset := offset
180182
if zone < 0 {
181183
b = append(b, '-')
182184
zone = -zone
183-
absoffset = -absoffset
184185
} else {
185186
b = append(b, '+')
186187
}
@@ -208,7 +209,6 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
208209
}
209210
}
210211
return b
211-
return b
212212
}
213213

214214
// nextStdChunk finds the first occurrence of a std string in
@@ -237,7 +237,7 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
237237
case 'B': // January
238238
return layout[0:specPos], stdLongMonth, layout[i+1:]
239239
case 'c': // "Mon Jan _2 15:04:05 2006"
240-
return layout[0:specPos], stdMonth, " %e %H:%M:%S %Y" + layout[i+1:]
240+
return layout[0:specPos], stdYield, "%a %b %e %H:%M:%S %Y" + layout[i+1:]
241241
case 'C': // 20
242242
return layout[0:specPos], stdFirstTwoDigitYear, layout[i+1:]
243243
case 'd': // 02
@@ -267,22 +267,22 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
267267
case 'M':
268268
return layout[0:specPos], stdZeroMinute, layout[i+1:]
269269
case 'n':
270-
// TODO: newline character '\n'
270+
return layout[0:specPos] + "\n", stdYield, layout[i+1:]
271271
case 'p':
272272
return layout[0:specPos], stdPM, layout[i+1:]
273273
case 'P':
274274
return layout[0:specPos], stdpm, layout[i+1:]
275275
case 'r':
276276
case 'R': // %H:%M"
277-
return layout[0:specPos], stdHour, ":%M" + layout[i+1:]
277+
return layout[0:specPos], stdYield, "%H:%M" + layout[i+1:]
278278
case 'S':
279279
return layout[0:specPos], stdZeroSecond, layout[i+1:]
280280
case 't':
281-
// TODO: tab character '\t'
281+
return layout[0:specPos] + "\t", stdYield, layout[i+1:]
282282
case 'T': // %H:%M:%S
283-
return layout[0:specPos], stdHour, ":%M:%S" + layout[i+1:]
284-
case 'u': // TODO ISO8601 weekday
285-
//return layout[0:specPos], stdNumWeekDay, layout[i+1:]
283+
return layout[0:specPos], stdYield, "%H:%M:%S" + layout[i+1:]
284+
//case 'u': // TODO ISO8601 weekday
285+
//return layout[0:specPos], stdNumWeekDay, layout[i+1:]
286286
case 'U':
287287
// TODO week of the year as a decimal number (Sunday is the first day of the week)
288288
case 'V':
@@ -291,8 +291,8 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
291291
return layout[0:specPos], stdNumWeekDay, layout[i+1:]
292292
case 'W':
293293
// TODO: week of the year as a decimal number (Monday is the first day of the wee)
294-
//case 'x': // localized, not supported
295-
//case 'X': // localized, not supported
294+
//case 'x': // locale depended, not supported
295+
//case 'X': // locale depended, not supported
296296
case 'y':
297297
return layout[0:specPos], stdYear, layout[i+1:]
298298
case 'Y':

0 commit comments

Comments
 (0)