-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
370 lines (353 loc) · 10.1 KB
/
format.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
package date
import (
"strings"
)
const (
// layoutTokenEnd indicates the layout is empty.
layoutTokenEnd int = iota
// layoutTokenNone is not a token.
layoutTokenNone
// layoutTokenYearLong is the year.
layoutTokenYearLong
// layoutTokenYear is the two-digits year.
layoutTokenYear
// layoutTokenMonth is the month beginning at 1.
layoutTokenMonth
// layoutTokenMonthLong is the two-digits month.
layoutTokenMonthLong
// layoutTokenMonthAbbr is the abbreviated month name.
layoutTokenMonthAbbr
// layoutTokenMonthFull is the full month name.
layoutTokenMonthFull
// layoutTokenDay is the day beginning at 1.
layoutTokenDay
// layoutTokenDayLong is the two-digits day.
layoutTokenDayLong
// layoutTokenDayOfWeek is the day of week that beginning at 0 (Sunday).
layoutTokenDayOfWeek
// layoutTokenDayOfWeek is the abbreviated name of the day of week.
layoutTokenDayOfWeekAbbr
// layoutTokenDayOfWeekFull is the name of the day of week.
layoutTokenDayOfWeekFull
// layoutTokenHour is the 24-hour clock hour that beginning at 1.
layoutTokenHour
// layoutTokenHourLong is the two-digits, 24-hour clock hour.
layoutTokenHourLong
// layoutTokenHour12 is the 12-hour clock hour that beginning at 1.
layoutTokenHour12
// layoutTokenHour12Long is the two-digits, 12-hour clock hour.
layoutTokenHour12Long
// layoutTokenMinute is the minute beginning at 1.
layoutTokenMinute
// layoutTokenMinuteLong is the two-digits minute.
layoutTokenMinuteLong
// layoutTokenSecond is the second beginning at 1.
layoutTokenSecond
// layoutTokenSecondLong is the two-digits second.
layoutTokenSecondLong
// layoutTokenMillisecondHundred is the one-digit millisecond (hundreds of milliseconds).
layoutTokenMillisecondHundred
// layoutTokenMillisecondTen is the two-digits millisecond (tens of milliseconds).
layoutTokenMillisecondTen
// layoutTokenMillisecondThree is the three-digits millisecond.
layoutTokenMillisecond
// layoutTokenPMUpper is post or ante meridiem in upper-case.
layoutTokenPMUpper
// layoutTokenPMLower is post or ante meridiem in upper-case.
layoutTokenPMLower
// layoutTokenTZ is the timezone offset from UTC.
layoutTokenTZ
// layoutTokenTZColon is the timezone offset from UTC that separate by colon.
layoutTokenTZColon
)
var abbrMonthNames = []string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}
var fullMonthNames = []string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
var abbrWeekdayNames = []string{
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
}
var fullWeekdayNames = []string{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
}
// nextLayoutToken gets the next token in the layout, and return
func nextLayoutToken(layout string) (int, string, string) {
if len(layout) == 0 {
return layoutTokenEnd, "", ""
}
switch layout[0] {
case 'Y':
if strings.HasPrefix(layout, "YYYY") {
return layoutTokenYearLong, layout[0:4], layout[4:]
} else if strings.HasPrefix(layout, "YY") {
return layoutTokenYear, layout[0:2], layout[2:]
}
case 'M':
if strings.HasPrefix(layout, "Monday") {
return layoutTokenDayOfWeekFull, layout[0:6], layout[6:]
} else if strings.HasPrefix(layout, "Mon") {
return layoutTokenDayOfWeekAbbr, layout[0:3], layout[3:]
}
if strings.HasPrefix(layout, "MMMM") {
return layoutTokenMonthFull, layout[0:4], layout[4:]
} else if strings.HasPrefix(layout, "MMM") {
return layoutTokenMonthAbbr, layout[0:3], layout[3:]
} else if strings.HasPrefix(layout, "MM") {
return layoutTokenMonthLong, layout[0:2], layout[2:]
} else {
return layoutTokenMonth, layout[0:1], layout[1:]
}
case 'J':
if strings.HasPrefix(layout, "Jan") {
if strings.HasPrefix(layout, "January") {
return layoutTokenMonthFull, layout[0:7], layout[7:]
} else {
return layoutTokenMonthAbbr, layout[0:3], layout[3:]
}
}
case 'D':
if strings.HasPrefix(layout, "DD") {
return layoutTokenDayLong, layout[0:2], layout[2:]
} else {
return layoutTokenDay, layout[0:1], layout[1:]
}
case 'd':
if strings.HasPrefix(layout, "dddd") {
return layoutTokenDayOfWeekFull, layout[0:4], layout[4:]
} else if strings.HasPrefix(layout, "ddd") {
return layoutTokenDayOfWeekAbbr, layout[0:3], layout[3:]
} else {
return layoutTokenDayOfWeek, layout[0:1], layout[1:]
}
case 'H':
if strings.HasPrefix(layout, "HH") {
return layoutTokenHourLong, layout[0:2], layout[2:]
} else {
return layoutTokenHour, layout[0:1], layout[1:]
}
case 'h':
if strings.HasPrefix(layout, "hh") {
return layoutTokenHour12Long, layout[0:2], layout[2:]
} else {
return layoutTokenHour12, layout[0:1], layout[1:]
}
case 'm':
if strings.HasPrefix(layout, "mm") {
return layoutTokenMinuteLong, layout[0:2], layout[2:]
} else {
return layoutTokenMinute, layout[0:1], layout[1:]
}
case 's':
if strings.HasPrefix(layout, "ss") {
return layoutTokenSecondLong, layout[0:2], layout[2:]
} else {
return layoutTokenSecond, layout[0:1], layout[1:]
}
case 'S':
if strings.HasPrefix(layout, "SSS") {
return layoutTokenMillisecond, layout[0:3], layout[3:]
} else if strings.HasPrefix(layout, "SS") {
return layoutTokenMillisecondTen, layout[0:2], layout[2:]
} else {
return layoutTokenMillisecondHundred, layout[0:1], layout[1:]
}
case 'A':
return layoutTokenPMUpper, layout[0:1], layout[1:]
case 'a':
return layoutTokenPMLower, layout[0:1], layout[1:]
case 'P':
if strings.HasPrefix(layout, "PM") {
return layoutTokenPMUpper, layout[0:2], layout[2:]
}
case 'Z':
if strings.HasPrefix(layout, "ZZ") {
return layoutTokenTZ, layout[0:2], layout[2:]
} else {
return layoutTokenTZColon, layout[0:1], layout[1:]
}
case '\\': // Escape next character
if len(layout) >= 2 {
return layoutTokenNone, layout[1:2], layout[2:]
}
case '0':
if len(layout) < 2 {
break
}
token := layoutTokenNone
switch layout[1] {
case '1':
token = layoutTokenMonthLong
case '2':
token = layoutTokenDayLong
case '3':
token = layoutTokenHour12Long
case '4':
token = layoutTokenMinuteLong
case '5':
token = layoutTokenSecondLong
case '6':
token = layoutTokenYear
}
if token != layoutTokenNone {
return token, layout[0:2], layout[2:]
}
case '1':
if len(layout) >= 2 && layout[1] == '5' {
return layoutTokenHourLong, layout[0:2], layout[2:]
}
return layoutTokenMonth, layout[0:1], layout[1:]
case '2':
if strings.HasPrefix(layout, "2006") {
return layoutTokenYearLong, layout[0:4], layout[4:]
}
return layoutTokenDay, layout[0:1], layout[1:]
case '3':
return layoutTokenHour12, layout[0:1], layout[1:]
case '4':
return layoutTokenMinute, layout[0:1], layout[1:]
case '5':
return layoutTokenSecond, layout[0:1], layout[1:]
}
return layoutTokenNone, layout[0:1], layout[1:]
}
// AppendFormat is like Format but appends the textual representation to b and returns the extended
// buffer.
func (t Time) AppendFormat(b []byte, layout string) []byte {
buf := t.formatByLayout(layout, b)
return buf
}
// Format returns a string of the time formatted by the layout from the parameter.
func (t Time) Format(layout string) string {
buf := make([]byte, 0, 64)
buf = t.formatByLayout(layout, buf)
return string(buf)
}
// formatByLayout appends the string of the time formatted by the layout into the buffer, and
// returns the reference of the buffer.
func (t Time) formatByLayout(layout string, buf []byte) []byte {
for {
token, str, suffix := nextLayoutToken(layout)
layout = suffix
if token == layoutTokenEnd {
break
} else if token == layoutTokenNone {
buf = append(buf, str...)
continue
}
switch token {
case layoutTokenYearLong:
buf = appendIntToBuffer(buf, t.Year(), 4)
case layoutTokenYear:
buf = appendIntToBuffer(buf, t.Year()%100, 2)
case layoutTokenMonth:
buf = appendIntToBuffer(buf, int(t.Month()), 0)
case layoutTokenMonthLong:
buf = appendIntToBuffer(buf, int(t.Month()), 2)
case layoutTokenMonthAbbr:
abbr := abbrMonthNames[t.Month()-1]
buf = append(buf, abbr...)
case layoutTokenMonthFull:
name := fullMonthNames[t.Month()-1]
buf = append(buf, name...)
case layoutTokenDay:
buf = appendIntToBuffer(buf, t.Day(), 1)
case layoutTokenDayLong:
buf = appendIntToBuffer(buf, t.Day(), 2)
case layoutTokenDayOfWeek:
buf = appendIntToBuffer(buf, int(t.Weekday()), 1)
case layoutTokenDayOfWeekAbbr:
abbr := abbrWeekdayNames[t.Weekday()]
buf = append(buf, abbr...)
case layoutTokenDayOfWeekFull:
name := fullWeekdayNames[t.Weekday()]
buf = append(buf, name...)
case layoutTokenHour:
buf = appendIntToBuffer(buf, t.Hour(), 1)
case layoutTokenHourLong:
buf = appendIntToBuffer(buf, t.Hour(), 2)
case layoutTokenHour12:
buf = appendIntToBuffer(buf, t.Hour12(), 1)
case layoutTokenHour12Long:
buf = appendIntToBuffer(buf, t.Hour12(), 2)
case layoutTokenMinute:
buf = appendIntToBuffer(buf, t.Minute(), 1)
case layoutTokenMinuteLong:
buf = appendIntToBuffer(buf, t.Minute(), 2)
case layoutTokenSecond:
buf = appendIntToBuffer(buf, t.Second(), 1)
case layoutTokenSecondLong:
buf = appendIntToBuffer(buf, t.Second(), 2)
case layoutTokenMillisecondHundred:
buf = appendIntToBuffer(buf, t.Millisecond()/100, 1)
case layoutTokenMillisecondTen:
buf = appendIntToBuffer(buf, t.Millisecond()/10, 2)
case layoutTokenMillisecond:
buf = appendIntToBuffer(buf, t.Millisecond(), 3)
case layoutTokenPMUpper:
hour := t.Hour()
if hour >= 12 {
buf = append(buf, "PM"...)
} else {
buf = append(buf, "AM"...)
}
case layoutTokenPMLower:
hour := t.Hour()
if hour >= 12 {
buf = append(buf, "pm"...)
} else {
buf = append(buf, "am"...)
}
case layoutTokenTZ, layoutTokenTZColon:
_, offset := t.Zone()
zone := offset / 60
if zone < 0 {
buf = append(buf, '-')
zone = -zone
} else {
buf = append(buf, '+')
}
buf = appendIntToBuffer(buf, zone/60, 2)
if token == layoutTokenTZColon {
buf = append(buf, ':')
}
buf = appendIntToBuffer(buf, zone%60, 2)
}
}
return buf
}