-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
333 lines (292 loc) · 7.92 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
package timex
import (
"errors"
"fmt"
)
const (
tokenYearTwoDigit = iota + 1
tokenYearFourDigit
tokenMonth
tokenMonthTwoDigit
tokenMonthShortName
tokenMonthLongName
tokenDayOfMonth
tokenDayOfMonthTwoDigit
)
const (
RFC3339 = "YYYY-MM-DD"
)
var monthShortNames = []string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}
var monthLongNames = []string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
// ParseError describes a problem parsing a date string.
type ParseError struct {
Layout string
Value string
LayoutElem string
ValueElem string
}
// Error returns the string representation of a ParseError.
func (e *ParseError) Error() string {
if len(e.LayoutElem) == 0 && len(e.ValueElem) == 0 {
return fmt.Sprintf("parsing date %q as %q", e.Value, e.Layout)
}
return fmt.Sprintf("parsing date %q as %q: cannot parse %q as %q", e.Value, e.Layout, e.ValueElem, e.LayoutElem)
}
func nextToken(layout string) (prefix string, token int, suffix string) {
for i := 0; i < len(layout); i++ {
switch layout[i] {
case 'Y': // YY, YYYY
if len(layout) >= i+4 && layout[i:i+4] == "YYYY" {
return layout[:i], tokenYearFourDigit, layout[i+4:]
}
if len(layout) >= i+2 && layout[i:i+2] == "YY" {
return layout[:i], tokenYearTwoDigit, layout[i+2:]
}
case 'M': // M, MM, MMM, MMMM
if len(layout) >= i+4 && layout[i:i+4] == "MMMM" {
return layout[:i], tokenMonthLongName, layout[i+4:]
}
if len(layout) >= i+3 && layout[i:i+3] == "MMM" {
return layout[:i], tokenMonthShortName, layout[i+3:]
}
if len(layout) >= i+2 && layout[i:i+2] == "MM" {
return layout[:i], tokenMonthTwoDigit, layout[i+2:]
}
if len(layout) >= i+1 && layout[i:i+1] == "M" {
return layout[:i], tokenMonth, layout[i+1:]
}
case 'D': // D, DD
if len(layout) >= i+2 && layout[i:i+2] == "DD" {
return layout[:i], tokenDayOfMonthTwoDigit, layout[i+2:]
}
if len(layout) >= i+1 && layout[i:i+1] == "D" {
return layout[:i], tokenDayOfMonth, layout[i+1:]
}
}
}
return layout, 0, ""
}
func parseStrictRFC3339(b []byte) (Date, error) {
if len(b) < len(RFC3339) {
return Date{}, &ParseError{Layout: RFC3339, Value: string(b)}
}
ok := true
parseUint := func(s []byte) (n int) {
for _, c := range s {
if !isDigit(c) {
ok = false
return 0
}
n = n*10 + int(c-'0')
}
return n
}
year := parseUint(b[0:4])
month := parseUint(b[5:7])
day := parseUint(b[8:10])
if !ok || b[4] != '-' || b[7] != '-' {
return Date{}, &ParseError{Layout: RFC3339, Value: string(b)}
}
return NewDate(year, month, day)
}
// ParseDate parses a formatted string and returns the date it represents.
//
// YY 01 Two-digit year
// YYYY 2001 Four-digit year
// M 1-12 Month, beginning at 1
// MM 01-12 Month, 2-digits
// MMM Jan-Dec The abbreviated month name
// MMMM January-December The full month name
// D 1-31 Day of month
// DD 01-31 Day of month, 2-digits
func ParseDate(layout, value string) (Date, error) {
var year, month, day int
originLayout, originValue := layout, value
var layoutElem, valueElem string
for {
prefix, token, suffix := nextToken(layout)
if token == 0 {
break
}
layoutElem = layout[len(prefix) : len(layout)-len(suffix)]
layout = suffix
if len(value) < len(prefix) {
return Date{}, &ParseError{Layout: originLayout, Value: originValue, LayoutElem: layoutElem, ValueElem: valueElem}
}
if value[:len(prefix)] != prefix {
return Date{}, &ParseError{Layout: originLayout, Value: originValue, LayoutElem: prefix, ValueElem: value}
}
value = value[len(prefix):]
valueElem = value
var ok bool
switch token {
case tokenYearTwoDigit:
year, value, ok = atoi(value, 2, 2)
if year >= 69 {
year += 1900
} else {
year += 2000
}
case tokenYearFourDigit:
year, value, ok = atoi(value, 4, 4)
case tokenMonth:
month, value, ok = atoi(value, 1, 2)
case tokenMonthTwoDigit:
month, value, ok = atoi(value, 2, 2)
case tokenMonthShortName:
month, value, ok = searchName(monthShortNames, value)
month++
case tokenMonthLongName:
month, value, ok = searchName(monthLongNames, value)
month++
case tokenDayOfMonth:
day, value, ok = atoi(value, 1, 2)
case tokenDayOfMonthTwoDigit:
day, value, ok = atoi(value, 2, 2)
}
if !ok {
return Date{}, &ParseError{Layout: originLayout, Value: originValue, LayoutElem: layoutElem, ValueElem: valueElem}
}
}
return NewDate(year, month, day)
}
func (d Date) appendRFC3339(b []byte) []byte {
year, month, day := ordinalToCalendar(d.ordinal)
b = appendInt(b, year, 4)
b = append(b, '-')
b = appendInt(b, month, 2)
b = append(b, '-')
b = appendInt(b, day, 2)
return b
}
func (d Date) appendStrictRFC3339(b []byte) ([]byte, error) {
year, month, day := ordinalToCalendar(d.ordinal)
if year < 0 || year > 9999 {
return nil, errors.New("year is out of range [0,9999]")
}
b = appendInt(b, year, 4)
b = append(b, '-')
b = appendInt(b, month, 2)
b = append(b, '-')
b = appendInt(b, day, 2)
return b, nil
}
func (d Date) format(layout string) string {
year, month, day := ordinalToCalendar(d.ordinal)
bytes := make([]byte, 0, len(layout)+10)
for {
prefix, token, suffix := nextToken(layout)
bytes = append(bytes, prefix...)
if token == 0 {
break
}
layout = suffix
switch token {
case tokenYearTwoDigit:
bytes = appendInt(bytes, year%100, 2)
case tokenYearFourDigit:
bytes = appendInt(bytes, year, 4)
case tokenMonth:
bytes = appendInt(bytes, month, 0)
case tokenMonthTwoDigit:
bytes = appendInt(bytes, month, 2)
case tokenMonthShortName:
bytes = append(bytes, monthShortNames[month-1]...)
case tokenMonthLongName:
bytes = append(bytes, monthLongNames[month-1]...)
case tokenDayOfMonth:
bytes = appendInt(bytes, day, 0)
case tokenDayOfMonthTwoDigit:
bytes = appendInt(bytes, day, 2)
}
}
return string(bytes)
}
// Format returns a textual representation of the date.
//
// YY 01 Two-digit year
// YYYY 2001 Four-digit year
// M 1-12 Month, beginning at 1
// MM 01-12 Month, 2-digits
// MMM Jan-Dec The abbreviated month name
// MMMM January-December The full month name
// D 1-31 Day of month
// DD 01-31 Day of month, 2-digits
func (d Date) Format(layout string) string {
switch layout {
case RFC3339:
b := make([]byte, 0, len(RFC3339))
b = d.appendRFC3339(b)
return string(b)
default:
return d.format(layout)
}
}
// String returns the textual representation of the date.
func (d Date) String() string {
return d.Format(RFC3339)
}
// GoString returns the Go syntax of the date.
func (d Date) GoString() string {
year, month, day := ordinalToCalendar(d.ordinal)
bytes := make([]byte, 0, 32)
bytes = append(bytes, "timex.MustNewDate("...)
bytes = appendInt(bytes, year, 0)
bytes = append(bytes, ", "...)
bytes = appendInt(bytes, month, 0)
bytes = append(bytes, ", "...)
bytes = appendInt(bytes, day, 0)
bytes = append(bytes, ')')
return string(bytes)
}
// MarshalJSON implements the json.Marshaler interface.
// The date is a quoted string in RFC 3339 format.
func (d Date) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(RFC3339)+2)
b = append(b, '"')
b, err := d.appendStrictRFC3339(b)
if err != nil {
return nil, err
}
b = append(b, '"')
return b, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// The date is expected to be a quoted string in RFC 3339 format.
func (d *Date) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
return errors.New("Date.UnmarshalJSON: input is not a JSON string")
}
var err error
*d, err = parseStrictRFC3339(data[1 : len(data)-1])
return err
}