-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.go
More file actions
367 lines (359 loc) · 9.58 KB
/
core.go
File metadata and controls
367 lines (359 loc) · 9.58 KB
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
// Package strftime implements C-like strftime functionality with locale support.
package strftime
import (
"bytes"
"strings"
"time"
)
// appendStrftime formats a time according to the specified format string and locale information,
// appending the result to a byte slice.
//
// This is the core formatting function that processes the format string and applies the appropriate
// formatting based on the conversion specifiers (% directives).
//
// Parameters:
// - l: Pointer to locale information for language-specific formatting
// - b: Byte slice to append formatted output to
// - f: Format string as bytes with strftime-style directives
// - t: Time value to format
//
// Returns: The extended byte slice with formatted time appended
//
// Supported format directives include:
// - %a - Abbreviated weekday name
// - %A - Full weekday name
// - %b - Abbreviated month name
// - %B - Full month name
// - %c - Preferred date and time representation
// - %C - Century (year/100)
// - %d - Day of month as decimal (01-31)
// - %D - Equivalent to %m/%d/%y
// - %e - Day of month as decimal with leading space (1-31)
// - %f - Microseconds
// - %F - Equivalent to %Y-%m-%d (ISO 8601)
// - %g - Last two digits of ISO week-based year
// - %G - ISO week-based year
// - %h - Same as %b
// - %H - Hour (00-23)
// - %I - Hour (01-12)
// - %j - Day of year (001-366)
// - %k - Hour with leading space (0-23)
// - %l - Hour with leading space (1-12)
// - %m - Month as decimal (01-12)
// - %M - Minute (00-59)
// - %n - Newline character
// - %p - AM or PM
// - %P - am or pm
// - %r - Time in 12-hour format with AM/PM
// - %R - Time in 24-hour format (%H:%M)
// - %s - Seconds since Unix epoch
// - %S - Second (00-60)
// - %t - Tab character
// - %T - Time in 24-hour format (%H:%M:%S)
// - %u - Weekday as decimal (1-7, Monday=1)
// - %U - Week number (00-53, Sunday as first day)
// - %v - Date in form of %e-%b-%Y
// - %V - ISO week number (01-53)
// - %w - Weekday as decimal (0-6, Sunday=0)
// - %W - Week number (00-53, Monday as first day)
// - %x - Preferred date representation
// - %X - Preferred time representation
// - %y - Year without century (00-99)
// - %Y - Year with century
// - %z - Time zone offset
// - %Z - Time zone name
// - %% - Percent sign
//
// Extended modifiers supported (before specifier):
// - %E - Alternative format (for date/time) - depends on locale, mainly used for era-based dates
// - %O - Alternative numeral format - depends on locale, mainly used for non-latin numerals
// - %- - No zero padding
func appendStrftime(l *strftimeLocaleInfo, b []byte, f []byte, t time.Time) []byte {
var skip, i int
// Preallocate additional space in the buffer if needed
// This helps reduce the number of allocations during append operations
if cap(b)-len(b) < len(f) {
newBuf := make([]byte, len(b), len(b)+len(f))
copy(newBuf, b)
b = newBuf
}
for len(f) > 0 {
i = bytes.IndexByte(f, '%')
if i > 0 {
b = append(b, f[:i]...)
f = f[i:]
} else if i == -1 {
// end of string
return append(b, f...)
}
// at this point, f always starts with a % symbol
if len(f) < 2 {
// can't have anything anymore
return append(b, f...)
}
skip = 2 // number of bytes to skip
switch f[1] {
case 'E':
if len(f) < 3 {
// not enough data to process
skip = 0
break
}
skip = 3
// Era modifier
switch f[2] {
case 'c':
if l.DTfmtEra != "" {
b = appendStrftime(l, b, []byte(l.DTfmtEra), t)
} else {
b = appendStrftime(l, b, []byte(l.DTfmt), t)
}
case 'C':
if l.Eyear != nil {
b = append(b, []byte(l.Eyear(t, 'C'))...)
} else {
b = appendInt(b, t.Year()/100, 1)
}
case 'x':
if l.DfmtEra != "" {
b = appendStrftime(l, b, []byte(l.DfmtEra), t)
} else {
b = appendStrftime(l, b, []byte(l.Dfmt), t)
}
case 'X':
if l.TfmtEra != "" {
b = appendStrftime(l, b, []byte(l.TfmtEra), t)
} else {
b = appendStrftime(l, b, []byte(l.Tfmt), t)
}
case 'y':
if l.Eyear != nil {
b = append(b, []byte(l.Eyear(t, 'y'))...)
} else {
b = appendInt(b, t.Year()%100, 2)
}
case 'Y':
if l.Eyear != nil {
b = append(b, []byte(l.Eyear(t, 'Y'))...)
} else {
b = appendInt(b, t.Year(), 1)
}
default:
skip = 0
}
case 'O':
if len(f) < 3 {
// not enough data to process
skip = 0
break
}
skip = 3
// alternative digits output (japanese, etc)
var v uint8
switch f[2] {
case 'd', 'e': // day (two decimals)
v = uint8(t.Day())
case 'H':
v = uint8(t.Hour())
case 'I':
// Noon is 12PM, midnight is 12AM.
v = uint8(t.Hour() % 12)
if v == 0 {
v = 12
}
case 'm':
v = uint8(t.Month())
case 'M':
v = uint8(t.Minute())
case 'S':
v = uint8(t.Second())
case 'U':
v = uint8(((t.YearDay() - 1) - int(t.Weekday()) + 7) / 7)
case 'V':
_, w := t.ISOWeek()
v = uint8(w)
case 'w':
v = uint8(t.Weekday())
case 'W': // same as %U, but with monday
wday := int(t.Weekday()+6) % 7 // weekday but Monday = 0
v = uint8(((t.YearDay() - 1) - wday + 7) / 7)
case 'y':
v = uint8(t.Year() % 100)
default:
skip = 0
}
if skip != 0 {
if l.Oprint != nil {
b = l.Oprint(b, int(v))
} else {
switch f[2] {
case 'e':
b = appendUint8Sp(b, v, 2)
case 'w', 'W':
b = appendUint8(b, v, 1)
default:
b = appendUint8(b, v, 2)
}
}
}
case '-':
if len(f) < 3 {
// not enough data to process
skip = 0
break
}
skip = 3
// no zero padding modified
switch f[2] {
case 'd': // day (two decimals)
b = appendUint8(b, uint8(t.Day()), 1)
case 'H':
b = appendUint8(b, uint8(t.Hour()), 1)
case 'I':
// Noon is 12PM, midnight is 12AM.
h := t.Hour() % 12
if h == 0 {
h = 12
}
b = appendUint8(b, uint8(h), 1)
case 'j':
b = appendInt(b, t.YearDay(), 1)
case 'm':
b = appendUint8(b, uint8(t.Month()), 1)
case 'M':
b = appendUint8(b, uint8(t.Minute()), 1)
case 'S':
b = appendUint8(b, uint8(t.Second()), 1)
default:
skip = 0
}
case 'a': // day (abbreviated)
b = append(b, []byte(l.AbDay[t.Weekday()])...)
case 'A': // day
b = append(b, []byte(l.Day[t.Weekday()])...)
case 'b', 'h': // month (abbreviated)
b = append(b, []byte(l.AbMonth[int(t.Month())-1])...)
case 'B': // month
b = append(b, []byte(l.Month[int(t.Month())-1])...)
case 'c': // date & time format
b = appendStrftime(l, b, []byte(l.DTfmt), t)
case 'C': // century part of year
b = appendInt(b, t.Year()/100, 1)
case 'd': // day (two decimals)
b = appendUint8(b, uint8(t.Day()), 2)
case 'D': // date (month/day/year format)
b = appendStrftime(l, b, []byte("%m/%d/%y"), t)
case 'e': // day
b = appendUint8Sp(b, uint8(t.Day()), 2)
case 'f':
b = appendInt(b, t.Nanosecond()/1000, 6)
case 'F':
b = appendStrftime(l, b, []byte("%Y-%m-%d"), t)
case 'g':
y, _ := t.ISOWeek()
b = appendInt(b, y%100, 2)
case 'G':
y, _ := t.ISOWeek()
b = appendInt(b, y, 1)
case 'H':
b = appendUint8(b, uint8(t.Hour()), 2)
case 'I':
// Noon is 12PM, midnight is 12AM.
h := t.Hour() % 12
if h == 0 {
h = 12
}
b = appendUint8(b, uint8(h), 2)
case 'j':
b = appendInt(b, t.YearDay(), 3)
case 'k':
b = appendUint8Sp(b, uint8(t.Hour()), 2)
case 'l':
// Noon is 12PM, midnight is 12AM.
h := t.Hour() % 12
if h == 0 {
h = 12
}
b = appendUint8Sp(b, uint8(h), 2)
case 'm':
b = appendUint8(b, uint8(t.Month()), 2)
case 'M':
b = appendUint8(b, uint8(t.Minute()), 2)
case 'n':
b = append(b, '\n')
case 'p':
if t.Hour() >= 12 {
b = append(b, []byte(strings.ToUpper(l.AmPm[1]))...)
} else {
b = append(b, []byte(strings.ToUpper(l.AmPm[0]))...)
}
case 'P':
if t.Hour() >= 12 {
b = append(b, []byte(strings.ToLower(l.AmPm[1]))...)
} else {
b = append(b, []byte(strings.ToLower(l.AmPm[0]))...)
}
case 'r':
b = appendStrftime(l, b, []byte("%I:%M:%S %p"), t)
case 'R':
b = appendStrftime(l, b, []byte("%H:%M"), t)
case 's':
b = appendInt64(b, t.Unix(), 1)
case 'S':
b = appendUint8(b, uint8(t.Second()), 2)
case 't':
b = append(b, '\t')
case 'T':
b = appendStrftime(l, b, []byte("%H:%M:%S"), t)
case 'u':
wday := (int(t.Weekday()+6) % 7) + 1 // weekday but Monday = 1
b = appendUint8(b, uint8(wday), 1)
case 'U':
b = appendUint8(b, uint8(((t.YearDay()-1)-int(t.Weekday())+7)/7), 2)
case 'v': // non-standard extension found in https://github.com/lestrrat-go/strftime
b = appendStrftime(l, b, []byte("%e-%b-%Y"), t)
case 'V':
_, w := t.ISOWeek()
b = appendUint8(b, uint8(w), 2)
case 'w':
b = appendUint8(b, uint8(t.Weekday()), 1)
case 'W': // same as %U, but with monday
wday := int(t.Weekday()+6) % 7 // weekday but Monday = 0
b = appendUint8(b, uint8(((t.YearDay()-1)-wday+7)/7), 2)
case 'x':
b = appendStrftime(l, b, []byte(l.Dfmt), t)
case 'X':
b = appendStrftime(l, b, []byte(l.Tfmt), t)
case 'y':
b = appendInt(b, t.Year()%100, 2)
case 'Y':
b = appendInt(b, t.Year(), 1)
case 'z':
_, z := t.Zone()
z = z / 60 // convert seconds → minutes
if z < 0 {
b = append(b, '-')
z = -z
} else {
b = append(b, '+')
}
b = appendUint8(b, uint8(z/60), 2)
b = appendUint8(b, uint8(z%60), 2)
case 'Z':
n, _ := t.Zone()
b = append(b, []byte(n)...)
case '%':
b = append(b, '%')
default:
skip = 0
}
// move f pointer
if skip == 0 {
b = append(b, f[0])
f = f[1:]
} else {
f = f[skip:]
}
}
return b
}