-
Notifications
You must be signed in to change notification settings - Fork 1
/
layout.go
452 lines (400 loc) · 10.4 KB
/
layout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package datefmt
import (
"strings"
"time"
"unsafe"
)
type Layout struct {
max int
flag formatFlag
args []*formatArg
layout string
}
func (l *Layout) String() string {
return l.layout
}
func (l *Layout) Format(t time.Time) string {
var (
year int
month time.Month
day int
hour int
minute int
second int
zoneName string
zoneOffset int
p []byte
)
p = make([]byte, 0, l.max)
if l.flag.Has(formatFlagNeedDate) {
year, month, day = t.Date()
}
if l.flag.Has(formatFlagNeedClock) {
hour, minute, second = t.Clock()
}
if l.flag.Has(formatFlagNeedZone) {
zoneName, zoneOffset = t.Zone()
}
for _, arg := range l.args {
switch arg.ph.flag {
case formatFlagNone:
p = arg.ph.format(p, 0, arg.w)
case formatFlagYear:
p = arg.ph.format(p, year, arg.w)
case formatFlagMonth:
p = arg.ph.format(p, int(month), arg.w)
case formatFlagDay:
p = arg.ph.format(p, day, arg.w)
case formatFlagHour:
p = arg.ph.format(p, hour, arg.w)
case formatFlagMinute:
p = arg.ph.format(p, minute, arg.w)
case formatFlagSecond:
p = arg.ph.format(p, second, arg.w)
case formatFlagWeekDay:
p = arg.ph.format(p, int(t.Weekday()), arg.w)
case formatFlagNanosecond:
p = arg.ph.format(p, t.Nanosecond(), arg.w)
case formatFlagZoneName:
p = formatZoneName(p, zoneName, zoneOffset)
case formatFlagZoneOffset:
p = arg.ph.format(p, zoneOffset, arg.w)
case formatFlagYearDay:
p = arg.ph.format(p, t.YearDay(), arg.w)
case formatFlagWeekYear:
iosYear, _ := t.ISOWeek()
p = arg.ph.format(p, iosYear, arg.w)
case formatFlagWeekInYear:
_, isoWeek := t.ISOWeek()
p = arg.ph.format(p, isoWeek, arg.w)
case formatFlagWeekInMonth:
firstDayOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, t.Location())
firstWeekYear, firstWeek := firstDayOfMonth.ISOWeek()
isoYear, isoWeek := t.ISOWeek()
if firstWeekYear == isoYear {
p = arg.ph.format(p, isoWeek-firstWeek+1, arg.w)
} else if month == time.January {
// Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1
p = arg.ph.format(p, isoWeek+1, arg.w)
} else {
// Dec 29 to Dec 31 might belong to week 1 of year n+1
_, lastWeek := time.Date(year, time.December, 28, 0, 0, 0, 0, t.Location()).ISOWeek()
p = arg.ph.format(p, lastWeek-firstWeek+2, arg.w)
}
}
}
// fmt.Println("len =", fb.Len(), ", cap =", fb.Cap(), ", max =", l.max)
return readOnlyBytes2String(p)
}
func NewLayout(generalLayout string) *Layout {
var (
l = Layout{layout: generalLayout}
gl = []byte(generalLayout)
n = len(gl)
sb = strings.Builder{}
tmax = 8 // text max length
)
flushBuffer := func() {
l.args = append(l.args, newBytesFormatArg(string2ReadOnlyBytes(sb.String())))
l.max += sb.Len()
sb.Reset()
sb.Grow(tmax)
}
sb.Grow(tmax)
for i := 0; i < n; i++ {
if _, ok := placeholders[gl[i]]; !ok && gl[i] != '\'' {
sb.WriteByte(gl[i])
continue
}
// quote
if gl[i] == '\'' {
for i++; i < n; i++ {
if gl[i] == '\'' {
if gl[i-1] == '\'' {
// real quote
sb.WriteByte('\'')
break
} else if i < n-1 && gl[i+1] == '\'' {
// real quote
sb.WriteByte('\'')
i++
continue
} else {
// end of text
break
}
}
// text delimiter
sb.WriteByte(gl[i])
}
continue
}
// flush buffer
if sb.Len() > 0 {
// fmt.Println("text =", sb.String())
flushBuffer()
}
// find consecutive tokens
s, e, token := i, i, gl[i]
for i++; i < n; i++ {
if gl[i] != token {
i--
break
}
e = i
}
// fmt.Println("ph =", string(gl[s:e+1]))
arg := newPlaceholderFormatArg(gl[s : e+1])
l.args = append(l.args, arg)
l.max += arg.max
l.flag.Add(arg.ph.flag)
}
// flush buffer
if sb.Len() > 0 {
// fmt.Println("text =", sb.String())
flushBuffer()
}
return &l
}
type formatArg struct {
s string
w int
max int
ph placeholder
}
func newBytesFormatArg(p []byte) *formatArg {
return &formatArg{
s: readOnlyBytes2String(p),
w: len(p),
max: len(p),
ph: placeholder{
flag: formatFlagNone,
format: func(dst []byte, _, _ int) []byte { return append(dst, p...) },
},
}
}
func newPlaceholderFormatArg(p []byte) *formatArg {
ph := placeholders[p[0]]
return &formatArg{
s: readOnlyBytes2String(p),
w: len(p),
max: ph.max(len(p)),
ph: *ph,
}
}
type formatFlag int
const (
formatFlagNone formatFlag = iota
formatFlagNanosecond
formatFlagWeekYear
formatFlagWeekInYear
formatFlagYearDay
formatFlagWeekDay
formatFlagYear formatFlag = iota + formatFlagNeedDate
formatFlagMonth
formatFlagDay
formatFlagWeekInMonth
formatFlagHour formatFlag = iota + formatFlagNeedClock
formatFlagMinute
formatFlagSecond
formatFlagZoneName formatFlag = iota + formatFlagNeedZone
formatFlagZoneOffset
formatFlagNeedDate formatFlag = 1 << 0 << 7
formatFlagNeedClock formatFlag = 1 << 1 << 7
formatFlagNeedZone formatFlag = 1 << 2 << 7
)
func (f *formatFlag) Add(flag formatFlag) {
*f |= flag
}
func (f formatFlag) Has(flag formatFlag) bool {
return f&flag != 0
}
type placeholder struct {
max func(int) int
flag formatFlag
format func(p []byte, v, w int) []byte
}
var (
placeholders = map[byte]*placeholder{
'G': {max: fixedMax(2), flag: formatFlagYear, format: formatEra},
'y': {max: yearMax, flag: formatFlagYear, format: formatYear},
'Y': {max: yearMax, flag: formatFlagWeekYear, format: formatYear},
'M': {max: monthMax, flag: formatFlagMonth, format: formatMonth},
'w': {max: numberMax(2), flag: formatFlagWeekInYear, format: formatNumProbably2Digits},
'W': {max: numberMax(2), flag: formatFlagWeekInMonth, format: formatNumProbably2Digits},
'D': {max: numberMax(3), flag: formatFlagYearDay, format: formatNumProbably3Digits},
'd': {max: numberMax(2), flag: formatFlagDay, format: formatNumProbably2Digits},
'F': {max: numberMax(1), flag: formatFlagDay, format: func(p []byte, v, w int) []byte { return formatNumProbably2Digits(p, dayOfWeekInMonth(v), w) }},
'E': {max: textMax(3, 9), flag: formatFlagWeekDay, format: formatWeek},
'u': {max: numberMax(1), flag: formatFlagWeekDay, format: func(p []byte, v, w int) []byte { return formatNumProbably2Digits(p, dayNumOfWeek(v), w) }},
'a': {max: fixedMax(2), flag: formatFlagHour, format: formatPM},
'H': {max: numberMax(2), flag: formatFlagHour, format: formatNumProbably2Digits},
'k': {max: numberMax(2), flag: formatFlagHour, format: func(p []byte, v, w int) []byte { return formatNumProbably2Digits(p, hour24(v), w) }},
'K': {max: numberMax(2), flag: formatFlagHour, format: func(p []byte, v, w int) []byte { return formatNumProbably2Digits(p, v%12, w) }},
'h': {max: numberMax(2), flag: formatFlagHour, format: func(p []byte, v, w int) []byte { return formatNumProbably2Digits(p, hour12(v), w) }},
'm': {max: numberMax(2), flag: formatFlagMinute, format: formatNumProbably2Digits},
's': {max: numberMax(2), flag: formatFlagSecond, format: formatNumProbably2Digits},
'S': {max: nanosecondMax, flag: formatFlagNanosecond, format: formatNanosecond},
'z': {max: fixedMax(5), flag: formatFlagZoneName},
'Z': {max: fixedMax(5), flag: formatFlagZoneOffset, format: formatZoneOffsetRFC822},
'X': {max: fixedMax(6), flag: formatFlagZoneOffset, format: formatZoneOffsetISO8601},
}
)
// y Y Year
func yearMax(w int) int {
if w == 2 {
return 2
}
return 4
}
func formatYear(p []byte, year, w int) []byte {
if w == 2 {
return formatNumProbably2Digits(p, year%100, 2)
}
return formatNumProbably4Digits(p, year, w)
}
// M Month
func monthMax(w int) int {
if w < 3 {
return 2
}
if w == 3 {
return 3
}
return 9
}
func formatMonth(p []byte, month, w int) []byte {
if w < 3 {
return formatNumProbably2Digits(p, month, w)
}
if w == 3 {
return formatString(p, time.Month(month).String()[:3])
}
return formatString(p, time.Month(month).String())
}
// E Week
func formatWeek(p []byte, week, w int) []byte {
if w <= 3 {
return formatString(p, time.Weekday(week).String()[:3])
}
return formatString(p, time.Weekday(week).String())
}
// G Era
func formatEra(p []byte, year, w int) []byte {
if year < 0 {
return append(p, "BC"...)
}
return append(p, "AD"...)
}
// a PM
func formatPM(p []byte, v, w int) []byte {
if v >= 12 {
return append(p, "PM"...)
}
return append(p, "AM"...)
}
// S Nanosecond
func nanosecondMax(w int) int {
return w
}
func formatNanosecond(p []byte, v, w int) []byte {
// truncate nanosecond number
div := 1000000000
i := w
for i > 0 {
div /= 10
i--
}
return formatNum(p, v/div, w)
}
// z Zone name
func formatZoneName(p []byte, zoneName string, zoneOffset int) []byte {
if len(zoneName) > 0 {
return append(p, zoneName...)
}
// No time zone known for this time, but we must print one.
// Use the RFC822 format.
return formatZoneOffsetRFC822(p, zoneOffset, 0)
}
// Z Zone RFC822
func formatZoneOffsetRFC822(p []byte, zoneOffset, w int) []byte {
sign, hour, minute := getZoneOffsetParts(zoneOffset)
return formatNum(formatNum(append(p, sign), hour, 2), minute, 2)
}
// X Zone ISO8601
func formatZoneOffsetISO8601(p []byte, zoneOffset, w int) []byte {
if zoneOffset == 0 {
return append(p, 'Z')
}
sign, hour, minute := getZoneOffsetParts(zoneOffset)
switch w {
case 1:
return formatNum(append(p, sign), hour, 2)
case 2:
return formatNum(formatNum(append(p, sign), hour, 2), minute, 2)
default:
return formatNum(append(formatNum(append(p, sign), hour, 2), ':'), minute, 2)
}
}
// helper functions
func hour12(hour int) int {
hour %= 12
if hour == 0 {
return 12
}
return hour
}
func hour24(hour int) int {
if hour == 0 {
return 24
}
return hour
}
func dayNumOfWeek(week int) int {
if time.Weekday(week) == time.Sunday {
return 7
}
return week
}
func dayOfWeekInMonth(day int) int {
return (day-1)/7 + 1
}
func getZoneOffsetParts(zoneOffset int) (sign byte, hour, minute int) {
zone := zoneOffset / 60 // convert to minutes
if zone < 0 {
sign = '-'
zone = -zone
} else {
sign = '+'
}
hour = zone / 60
minute = zone % 60
return
}
func fixedMax(max int) func(int) int {
return func(int) int {
return max
}
}
func textMax(short, long int) func(int) int {
return func(w int) int {
if w >= 4 {
return long
}
return short
}
}
func numberMax(minWidth int) func(int) int {
return func(w int) int {
if w > minWidth {
return w
}
return minWidth
}
}
func readOnlyBytes2String(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func string2ReadOnlyBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(&s))
}