/
date_time_parser.v
299 lines (285 loc) · 8.05 KB
/
date_time_parser.v
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
module time
struct DateTimeParser {
datetime string
format string
mut:
current_pos_datetime int
}
fn new_date_time_parser(datetime string, format string) DateTimeParser {
return DateTimeParser{
datetime: datetime
format: format
}
}
fn (mut p DateTimeParser) next(length int) !string {
if p.current_pos_datetime + length > p.datetime.len {
return error('end of string')
}
val := p.datetime[p.current_pos_datetime..p.current_pos_datetime + length]
p.current_pos_datetime += length
return val
}
fn (mut p DateTimeParser) peek(length int) !string {
if p.current_pos_datetime + length > p.datetime.len {
return error('end of string')
}
return p.datetime[p.current_pos_datetime..p.current_pos_datetime + length]
}
fn (mut p DateTimeParser) must_be_int(length int) !int {
val := p.next(length)!
if !val.contains_only('0123456789') {
return error('expected int, found: ${val}')
}
return val.int()
}
fn (mut p DateTimeParser) must_be_int_with_minimum_length(min int, max int, allow_leading_zero bool) !int {
mut length := max + 1 - min
mut val := ''
for _ in 0 .. length {
tok := p.peek(1) or { break }
if !tok.contains_only('0123456789') {
break
}
p.next(1)!
val += tok
}
if val.len < min {
return error('expected int with a minimum length of ${min}, found: ${val.len}')
}
if !allow_leading_zero && val.starts_with('0') {
return error('0 is not allowed for this format')
}
return val.int()
}
fn (mut p DateTimeParser) must_be_single_int_with_optional_leading_zero() !int {
mut val := p.next(1)!
if val == '0' {
next := p.next(1) or { '' }
if !next.contains_only('0123456789') {
return error('expected int, found: ${next}')
}
val += next
}
return val.int()
}
fn (mut p DateTimeParser) must_be_string(must string) ! {
val := p.next(must.len)!
if val != must {
return error('invalid string: "${val}"!="${must}" at: ${p.current_pos_datetime}')
}
}
fn (mut p DateTimeParser) must_be_string_one_of(oneof []string) !string {
for must in oneof {
val := p.peek(must.len) or { continue }
if val == must {
return must
}
}
return error('invalid string: must be one of ${oneof}, at ${p.current_pos_datetime}')
}
fn (mut p DateTimeParser) must_be_valid_month() !int {
for v in long_months {
if p.current_pos_datetime + v.len < p.datetime.len {
month_name := p.datetime[p.current_pos_datetime..p.current_pos_datetime + v.len]
if v == month_name {
p.current_pos_datetime += v.len
return long_months.index(month_name) + 1
}
}
}
return error_invalid_time(0, 'invalid month name')
}
fn (mut p DateTimeParser) must_be_valid_week_day(letters int) !string {
val := p.next(letters)!
for v in long_days {
if v[0..letters] == val {
return v
}
}
return error_invalid_time(0, 'invalid month name')
}
fn extract_tokens(s string) ![]string {
mut tokens := []string{}
mut current := ''
for r in s {
if current.contains_only(r.ascii_str()) || current == '' {
current += r.ascii_str()
} else {
tokens << current
current = r.ascii_str()
}
}
if current != '' {
tokens << current
}
return tokens
}
// parse_format parses the string `s`, as a custom `format`, containing the following specifiers:
// YYYY - 4 digit year, 0000..9999
// YY - 2 digit year, 00..99
// M - month, 1..12
// MM - month, 2 digits, 01..12
// MMMM - name of month
// D - day of the month, 1..31
// DD - day of the month, 01..31
// H - hour, 0..23
// HH - hour, 00..23
// h - hour, 0..23
// hh - hour, 0..23
// k - hour, 0..23
// kk - hour, 0..23
// m - minute, 0..59
// mm - minute, 0..59
// s - second, 0..59
// ss - second, 0..59
fn (mut p DateTimeParser) parse() !Time {
mut year_ := 0
mut month_ := 0
mut day_in_month := 0
mut hour_ := 0
mut minute_ := 0
mut second_ := 0
tokens := extract_tokens(p.format) or {
return error_invalid_time(0, 'malformed format string: ${err}')
}
for token in tokens {
match token {
'YYYY' {
year_ = p.must_be_int(4) or {
return error_invalid_time(0, 'end of string reached before the full year was specified')
}
}
'YY' {
year_ = now().year / 100 * 100 + p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before the full year was specified')
}
}
'M' {
month_ = p.must_be_int_with_minimum_length(1, 2, false) or {
return error_invalid_time(0, 'end of string reached before the month was specified')
}
if month_ < 1 || month_ > 12 {
return error_invalid_time(0, 'month must be between 1 and 12')
}
}
'MM' {
month_ = p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before the month was specified')
}
if month_ < 1 || month_ > 12 {
return error_invalid_time(0, 'month must be between 01 and 12')
}
}
'MMMM' {
month_ = p.must_be_valid_month() or { return err }
}
'D' {
day_in_month = p.must_be_int_with_minimum_length(1, 2, false) or {
return error_invalid_time(0, 'end of string reached before the day was specified')
}
if day_in_month < 1 || day_in_month > 31 {
return error_invalid_time(0, 'day must be between 1 and 31')
}
}
'DD' {
day_in_month = p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before the month was specified')
}
if day_in_month < 1 || day_in_month > 31 {
return error_invalid_time(0, 'day must be between 01 and 31')
}
}
'H' {
hour_ = p.must_be_int_with_minimum_length(1, 2, false) or {
return error_invalid_time(0, 'end of string reached before hours where specified')
}
if hour_ < 0 || hour_ > 23 {
return error_invalid_time(0, 'hour must be between 0 and 23')
}
}
'HH' {
hour_ = p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before hours where specified')
}
if hour_ < 0 || hour_ > 23 {
return error_invalid_time(0, 'hour must be between 00 and 23')
}
}
'h' {
hour_ = p.must_be_int_with_minimum_length(1, 2, false) or {
return error_invalid_time(0, 'end of string reached before hours where specified')
}
if hour_ < 0 || hour_ > 23 {
return error_invalid_time(0, 'hour must be between 0 and 23')
}
}
'hh' {
hour_ = p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before hours where specified')
}
if hour_ < 0 || hour_ > 23 {
return error_invalid_time(0, 'hour must be between 00 and 23')
}
}
'k' {
hour_ = p.must_be_int(1) or {
return error_invalid_time(0, 'end of string reached before hours where specified')
}
if hour_ < 0 || hour_ > 23 {
return error_invalid_time(0, 'hour must be between 0 and 23')
}
}
'kk' {
hour_ = p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before hours where specified')
}
if hour_ < 0 || hour_ > 23 {
return error_invalid_time(0, 'hour must be between 00 and 23')
}
}
'm' {
minute_ = p.must_be_int(1) or {
return error_invalid_time(0, 'end of string reached before minutes where specified')
}
if minute_ < 0 || minute_ > 59 {
return error_invalid_time(0, 'minute must be between 0 and 59')
}
}
'mm' {
minute_ = p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before minutes where specified')
}
if minute_ < 0 || minute_ > 59 {
return error_invalid_time(0, 'minute must be between 00 and 59')
}
}
's' {
second_ = p.must_be_int(1) or {
return error_invalid_time(0, 'end of string reached before seconds where specified')
}
if second_ < 0 || second_ > 59 {
return error_invalid_time(0, 'second must be between 0 and 59')
}
}
'ss' {
second_ = p.must_be_int(2) or {
return error_invalid_time(0, 'end of string reached before seconds where specified')
}
if second_ < 0 || second_ > 59 {
return error_invalid_time(0, 'second must be between 00 and 59')
}
}
else {
p.must_be_string(token) or { return error_invalid_time(0, '${err}') }
}
}
}
return new_time(
year: year_
month: month_
day: day_in_month
hour: hour_
minute: minute_
second: second_
)
}