-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.go
executable file
·251 lines (230 loc) · 5.92 KB
/
parsers.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
package dateparse
import (
"fmt"
"time"
)
// parser defines a function that processes a string and returns the
// remaining characters unconsumed by the given parser.
//
// The data parsed from the consumed characters should be
// written to the `datetime` struct.
type parser func(result *datetime, chars string) (rest string, err error)
func trimPrefix(count int, str string) string {
if len(str) > count {
return str[count:]
}
return ""
}
func literalParser(literal byte) parser {
return func(dt *datetime, chars string) (rest string, _ error) {
if len(chars) < 1 && literal != ' ' {
return "", fmt.Errorf("expected literal \"%c\", found empty string", literal)
}
chars = takeAllSpaces(chars)
if literal == ' ' {
return chars, nil
}
if chars[0] != literal {
return "", fmt.Errorf("expected literal \"%c\", got \"%c\"", literal, chars[0])
}
return trimPrefix(1, chars), nil
}
}
func parseAmPm(result *datetime, chars string) (rest string, _ error) {
if len(chars) < 2 {
return "", fmt.Errorf("expected > 2 chars, found %d", len(chars))
}
switch chars[:2] {
case "am":
result.am = boolPtr(true)
case "pm":
result.am = boolPtr(false)
default:
return "", fmt.Errorf("expected AM or PM, got \"%s\"", chars[:2])
}
return trimPrefix(2, chars), nil
}
func parseWeedayAbbreviation(result *datetime, chars string) (rest string, _ error) {
if len(chars) < 3 {
return "", fmt.Errorf("expected at least 3 chars, got %d", len(chars))
}
weekday, ok := weekdayAbbrev(chars[:3])
if !ok {
return "", fmt.Errorf("invalid week abbreviation \"%s\"", chars[:3])
}
result.weekday = &weekday
return trimPrefix(3, chars), nil
}
func parseMonthAbbreviation(result *datetime, chars string) (rest string, _ error) {
if len(chars) < 3 {
return "", fmt.Errorf("expected at least 3 chars, got %d", len(chars))
}
month, ok := monthAbbrev(chars[:3])
if !ok {
return "", fmt.Errorf("invalid month abbreviation \"%s\"", chars[:3])
}
result.month = &month
return trimPrefix(3, chars), nil
}
func parseMonthNumeric(result *datetime, chars string) (rest string, _ error) {
num, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
month := time.Month(num)
result.month = &month
return rest, nil
}
func parseDayOfMonthNumeric(result *datetime, chars string) (rest string, _ error) {
num, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.day = &num
return rest, nil
}
func parseMicrosecondsNumeric(result *datetime, chars string) (rest string, _ error) {
num, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.microseconds = &num
return rest, nil
}
func parse24HourNumeric(result *datetime, chars string) (rest string, _ error) {
hour, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.hours = &hour
return rest, nil
}
func parse12HourNumeric(result *datetime, chars string) (rest string, _ error) {
num, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.hours = &num
return rest, nil
}
func parseMinuteNumeric(result *datetime, chars string) (rest string, _ error) {
min, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.minutes = &min
return rest, nil
}
func parseMonthName(result *datetime, chars string) (rest string, _ error) {
month, charCount, ok := monthName(chars)
if !ok {
return "", fmt.Errorf("unknown month name, got \"%s\"", chars)
}
result.month = &month
return trimPrefix(charCount, chars), nil
}
func parse12HourTimestamp(result *datetime, chars string) (rest string, _ error) {
hour, rest, err := takeNumberAtMostNChars(2, chars)
if err != nil {
return "", err
}
rest, err = literalParser(':')(result, rest)
if err != nil {
return "", err
}
min, rest, err := takeNumberAtMostNChars(2, rest)
if err != nil {
return "", err
}
rest, err = literalParser(':')(result, rest)
if err != nil {
return "", err
}
sec, rest, err := takeNumberAtMostNChars(2, rest)
if err != nil {
return "", err
}
rest = takeAllSpaces(rest)
rest, err = parseAmPm(result, rest)
if err != nil {
return "", err
}
result.seconds = &sec
result.minutes = &min
result.hours = &hour
return rest, nil
}
func parseSecondsNumeric(result *datetime, chars string) (rest string, _ error) {
sec, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.seconds = &sec
return rest, nil
}
func parse24HourTimestamp(result *datetime, chars string) (rest string, _ error) {
hour, rest, err := takeNumberAtMostNChars(2, chars)
if err != nil {
return "", err
}
rest, err = literalParser(':')(result, rest)
if err != nil {
return "", err
}
minute, rest, err := takeNumberAtMostNChars(2, rest)
if err != nil {
return "", err
}
rest, err = literalParser(':')(result, rest)
if err != nil {
return "", err
}
seconds, rest, err := takeNumberAtMostNChars(2, rest)
if err != nil {
return "", err
}
result.hours = &hour
result.minutes = &minute
result.seconds = &seconds
return rest, err
}
func parseYear2DigitNumeric(result *datetime, chars string) (rest string, _ error) {
year, rest, err := takeNumberAtMostNChars(2, chars)
if err != nil {
return "", err
}
if year >= 70 {
year += 1900
} else {
year += 2000
}
result.year = &year
return rest, nil
}
func parseYear4DigitNumeric(result *datetime, chars string) (rest string, _ error) {
if len(chars) < 4 {
return "", fmt.Errorf("expected at least 4 chars, got %d", len(chars))
}
year, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.year = &year
return rest, nil
}
func parseDayNumericWithEnglishSuffix(result *datetime, chars string) (rest string, _ error) {
num, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.day = &num
return trimPrefix(2, rest), nil
}
func parseDayOfYearNumeric(result *datetime, chars string) (rest string, _ error) {
num, rest, err := takeNumber(chars)
if err != nil {
return "", err
}
result.dayOfYear = &num
return rest, nil
}