-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathparse.go
298 lines (261 loc) · 8.55 KB
/
parse.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package date
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"unicode"
)
// MustAutoParse is as per [AutoParse] except that it panics if the string cannot be parsed.
// This is intended for setup code; don't use it for user inputs.
func MustAutoParse(value string) Date {
d, err := AutoParse(value)
if err != nil {
panic(err)
}
return d
}
// MustAutoParseUS is as per [AutoParseUS] except that it panics if the string cannot be parsed.
// This is intended for setup code; don't use it for user inputs.
func MustAutoParseUS(value string) Date {
d, err := AutoParseUS(value)
if err != nil {
panic(err)
}
return d
}
// AutoParse is like [ParseISO], except that it automatically adapts to a variety of date formats
// provided that they can be detected unambiguously. Specifically, this includes the widely-used
// "European" and "British" date formats but not the common US format. Surrounding whitespace is
// ignored.
//
// The supported formats are:
//
// - all formats supported by [ParseISO]
// - yyyy/mm/dd | yyyy.mm.dd (or any similar pattern)
// - dd/mm/yyyy | dd.mm.yyyy (or any similar pattern)
// - d/m/yyyy | d.m.yyyy (or any similar pattern)
// - surrounding whitespace is ignored
func AutoParse(value string) (Date, error) {
return autoParse(value, func(yyyy, f1, f2 string) string { return fmt.Sprintf("%s-%s-%s", yyyy, f1, f2) })
}
// AutoParseUS is like [ParseISO], except that it automatically adapts to a variety of date formats
// provided that they can be detected unambiguously. Specifically, this includes the widely-used
// "European" and "US" date formats but not the common "British" format. Surrounding whitespace is
// ignored.
//
// The supported formats are:
//
// - all formats supported by ParseISO
// - yyyy/mm/dd | yyyy.mm.dd (or any similar pattern)
// - mm/dd/yyyy | mm.dd.yyyy (or any similar pattern)
// - m/d/yyyy | m.d.yyyy (or any similar pattern)
// - surrounding whitespace is ignored
func AutoParseUS(value string) (Date, error) {
return autoParse(value, func(yyyy, f1, f2 string) string { return fmt.Sprintf("%s-%s-%s", yyyy, f2, f1) })
}
func autoParse(value string, compose func(yyyy, f1, f2 string) string) (Date, error) {
abs := strings.TrimSpace(value)
if len(abs) == 0 {
return 0, errors.New("Date.AutoParse: cannot parse a blank string")
}
sign := ""
if abs[0] == '+' || abs[0] == '-' {
sign = abs[:1]
abs = abs[1:]
}
if len(abs) >= 8 {
i1 := -1
i2 := -1
for i, r := range abs {
if unicode.IsPunct(r) {
if i1 < 0 {
i1 = i
} else {
i2 = i
}
}
}
if i1 >= 4 && i2 > i1 && abs[i1] == abs[i2] {
// just normalise the punctuation
a := []byte(abs)
a[i1] = '-'
a[i2] = '-'
abs = string(a)
} else if i1 >= 1 && i2 > i1 && abs[i1] == abs[i2] {
// harder case - need to swap the field order
f1 := abs[0:i1] // day or month
f2 := abs[i1+1 : i2] // month or day
if len(f1) == 1 {
f1 = "0" + f1
}
if len(f2) == 1 {
f2 = "0" + f2
}
yyyy := abs[i2+1:]
abs = compose(yyyy, f2, f1)
}
}
return parseISO(value, sign+abs)
}
// MustParseISO is as per [ParseISO] except that it panics if the string cannot be parsed.
// This is intended for setup code; don't use it for user inputs.
func MustParseISO(value string) Date {
d, err := ParseISO(value)
if err != nil {
panic(err)
}
return d
}
// ParseISO parses an ISO 8601 formatted string and returns the date value it represents.
// It accepts the following formats:
//
// - the common formats ±YYYY-MM-DD and ±YYYYMMDD (e.g. 2006-01-02 and 20060102)
// - the ordinal date representation ±YYYY-OOO (e.g. 2006-217)
//
// # Common date formats
//
// For common formats, ParseISO will accept dates with more year digits than the four-digit
// minimum. A leading plus '+' sign is allowed and ignored. Basic format (without '-'
// separators) is allowed.
//
// If a time field is present, it is ignored. For example, "2018-02-03T00:00:00Z" is parsed as
// 3rd February 2018.
//
// # Ordinal dates
//
// For ordinal dates, the extended format (including '-') is supported, but the basic format
// (without '-') is not supported because it could not be distinguished from the YYYYMMDD format.
//
// See also [Parse], which can be used to parse date strings in other formats; however, it
// only accepts years represented with exactly four digits.
//
// # Background
//
// - https://en.wikipedia.org/wiki/ISO_8601#Dates
// - https://www.iso.org/obp/ui#iso:std:iso:8601:-1:ed-1:v1:en:term:3.1.3.1
func ParseISO(value string) (Date, error) {
return parseISO(value, value)
}
func parseISO(input, value string) (Date, error) {
abs := value
sign := 1
if len(value) > 0 {
switch value[0] {
case '+':
abs = value[1:]
case '-':
abs = value[1:]
sign = -1
}
}
tee := strings.IndexByte(abs, 'T')
if tee == 8 || tee == 10 {
if !timeRegex1.MatchString(abs[tee:]) && !timeRegex2.MatchString(abs[tee:]) {
return 0, fmt.Errorf("date.ParseISO: date-time %q: not a time", value)
}
abs = abs[:tee]
}
dash1 := strings.IndexByte(abs, '-')
dash2 := strings.LastIndexByte(abs, '-')
if dash1 < 0 {
// parse YYYYMMDD (more Y digits are allowed)
ln := len(abs)
fm := ln - 4
fd := ln - 2
if fm < 0 || fd < 0 {
return 0, fmt.Errorf("date.ParseISO: cannot parse %q: too short", input)
}
return parseYYYYMMDD(input, abs[:fm], abs[fm:fd], abs[fd:], sign)
}
if dash2 > dash1 {
// parse YYYY-MM-DD (more Y digits are allowed)
fy1 := dash1
fm1 := dash1 + 1
fm2 := dash2
fd1 := dash2 + 1
if abs[fm2] != '-' {
return 0, fmt.Errorf("date.ParseISO: cannot parse %q: incorrect syntax for date yyyy-mm-dd", input)
}
return parseYYYYMMDD(input, abs[:fy1], abs[fm1:fm2], abs[fd1:], sign)
}
// parse YYYY-OOO (more Y digits are allowed)
fy1 := dash1
fo1 := dash1 + 1
if len(abs) != fo1+3 {
return 0, fmt.Errorf("date.ParseISO: cannot parse %q: incorrect length for ordinal date yyyy-ooo", input)
}
return parseYYYYOOO(input, abs[:fy1], abs[fo1:], sign)
}
func parseYYYYMMDD(input, yyyy, mm, dd string, sign int) (Date, error) {
year, e1 := parseField(yyyy, "year", 4, -1)
month, e2 := parseField(mm, "month", -1, 2)
day, e3 := parseField(dd, "day", -1, 2)
err := errors.Join(e1, e2, e3)
if err != nil {
return 0, fmt.Errorf("date.ParseISO: cannot parse %q: %w", input, err)
}
t := time.Date(sign*year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
return encode(t), nil
}
func parseYYYYOOO(input, yyyy, ooo string, sign int) (Date, error) {
year, e1 := parseField(yyyy, "year", 4, -1)
ordinal, e2 := parseField(ooo, "ordinal", -1, 3)
err := errors.Join(e1, e2)
if err != nil {
return 0, fmt.Errorf("date.ParseISO: cannot parse ordinal date %q: %w", input, err)
}
t := time.Date(sign*year, time.January, ordinal, 0, 0, 0, 0, time.UTC)
return encode(t), nil
}
var (
timeRegex1 = regexp.MustCompile("^T[0-9][0-9].[0-9][0-9].[0-9][0-9]")
timeRegex2 = regexp.MustCompile("^T[0-9]{2,6}")
)
func parseField(field, name string, minLength, requiredLength int) (int, error) {
if (minLength > 0 && len(field) < minLength) || (requiredLength > 0 && len(field) != requiredLength) {
return 0, fmt.Errorf("%s has wrong length", name)
}
number, err := strconv.Atoi(field)
if err != nil {
return 0, fmt.Errorf("invalid %s", name)
}
return number, nil
}
// MustParse is as per Parse except that it panics if the string cannot be parsed.
// This is intended for setup code; don't use it for user inputs.
func MustParse(layout, value string) Date {
d, err := Parse(layout, value)
if err != nil {
panic(err)
}
return d
}
// Parse parses a formatted string of a known layout and returns the [Date] value it represents.
// The layout defines the format by showing how the reference date, defined
// to be
//
// Monday, Jan 2, 2006
//
// would be interpreted if it were the value; it serves as an example of the
// input format. The same interpretation will then be made to the input string.
//
// This function actually uses [time.Parse] to parse the input and can use any
// layout accepted by [time.Parse], but returns only the date part of the
// parsed [time.Time] value.
//
// This function cannot currently parse ISO 8601 strings that use the expanded
// year format; you should use [ParseISO] to parse those strings correctly.
// That is, it only accepts years represented with exactly four digits.
func Parse(layout, value string) (Date, error) {
t, err := time.Parse(layout, value)
if err != nil {
return 0, err
}
return encode(t), nil
}