forked from go-chrono/chrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
365 lines (316 loc) Β· 7.46 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
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
//go:build parse
package chrono
import (
"strconv"
"strings"
"unicode"
)
type ParseConfig struct {
DayFirst bool
}
type Chronological interface {
String() string
Parse(layout, value string) error
get() (dv, tv, ov *int64)
set(dv, tv, ov int64)
}
// func Parse(value string, conf ParseConfig) (Chronological, error) {
// // pick type
// return nil, nil
// }
// ParseToLayout attempts to parse the input string as C and returns
// the applicable layout string that would parse that string, or format C to that string.
// Any valid and non-ambiguous ISO 8601 string will be parsed correctly,
// and any other string will be parsed with a best effort attempt, although the resulting
// layout string may not be valid.
func ParseToLayout(value string, conf ParseConfig, c Chronological) (string, error) {
var (
typ, prevTyp rune // a = alpha, n = numeric, s = separator, w = whitespace, o = other
sign int // -1 = negative, 0 = no sign, 1 = positive
buf []rune
state state
layout []string
parts parts
ambiguous []chunk
)
var date, time, offset *int64
if c != nil {
date, time, offset = c.get()
}
var err error
if date != nil {
if parts.year, parts.month, parts.day, err = fromDate(*date); err != nil {
return "", err
}
if parts.isoYear, parts.isoWeek, err = getISOWeek(*date); err != nil {
return "", err
}
}
if time != nil {
parts.hour, parts.min, parts.sec, parts.nsec = fromTime(*time)
_, parts.isAfternoon = convert24To12HourClock(parts.hour)
}
if offset != nil {
parts.offset = *offset
}
_ = sign // TODO
for i, c := range []rune(value) {
switch {
case (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'):
typ = 'a'
case c >= '0' && c <= '9':
typ = 'n'
case (c == '+' || c == '-') && (prevTyp == 0 || prevTyp != 'n'):
typ = 'n'
case c == '/' || c == '-' || c == '.' || c == ',' || c == ':':
typ = 's'
case unicode.IsSpace(c):
typ = 'w'
default:
typ = 'o'
}
if typ == prevTyp {
buf = append(buf, c)
if i != len(value)-1 {
continue
}
}
evalParseRules(prevTyp, buf, conf, &state, &layout, &parts, &ambiguous)
prevTyp = typ
typ = 0
sign = 0
buf = []rune{c}
}
//fmt.Println("=>", layout)
evalAmbiguous(ambiguous, conf, layout, &parts)
layoutStr := strings.Join(layout, "")
if c != nil {
if err := applyParts(parts, date, time, offset); err != nil {
return layoutStr, err
}
c.set(*date, *time, *offset)
}
return layoutStr, nil
}
func evalParseRules(prevTyp rune, buf []rune, conf ParseConfig, state *state, layout *[]string, parts *parts, ambiguous *[]chunk) {
switch prevTyp {
case 'w', 'o':
*layout = append(*layout, string(buf))
case 's':
switch buf[0] {
case '-':
state.component = componentDate
case '/':
state.component = componentDate
// TODO conf
case ':':
state.component = componentTime
}
*layout = append(*layout, string(buf))
case 'n':
var sign int
switch buf[0] {
case '+':
sign = 1
case '-':
sign = -1
}
v, err := strconv.Atoi(string(buf))
if err != nil {
panic(err)
}
if str, ok := eval(v, sign, uint(len(buf)), conf, state, parts, false); ok {
*layout = append(*layout, str)
if len(*ambiguous) != 0 {
(*ambiguous)[len(*ambiguous)-1].state = *state
}
return
}
*layout = append(*layout, string(buf))
*ambiguous = append(*ambiguous, chunk{
pos: uint(len(*layout) - 1),
v: v,
sign: sign,
digits: uint(len(buf)),
})
}
}
func evalAmbiguous(chunks []chunk, conf ParseConfig, layout []string, parts *parts) {
for i := len(chunks) - 1; i >= 0; i-- {
if str, ok := eval(chunks[i].v, chunks[i].sign, chunks[i].digits, conf, &chunks[i].state, parts, true); ok {
layout[chunks[i].pos] = str
}
}
}
type state struct {
component
datePart
timePart
}
func (s state) String() string {
return string(s.component) + string(s.datePart) + string(s.timePart)
}
func (s state) d(rev bool) datePart {
if rev && s.datePart == datePartYear {
return datePartDay
} else if rev && s.datePart == datePartDay {
return datePartYear
}
return s.datePart
}
func (s state) t(rev bool) timePart {
return s.timePart // TODO
}
type component rune
const (
componentNone = 0
componentDate component = 'd'
componentTime component = 't'
)
type datePart rune
const (
datePartNone = 0
datePartYear datePart = 'y'
datePartMonth datePart = 'm'
datePartDay datePart = 'd'
)
type timePart rune
const (
timePartHour timePart = 'h'
timePartMinute timePart = 'm'
timePartSecond timePart = 's'
)
type chunk struct {
pos uint
v int
sign int
digits uint
state state
}
func eval(v int, sign int, digits uint, conf ParseConfig, state *state, parts *parts, rev bool) (str string, ok bool) {
// fmt.Println("========= eval")
// fmt.Println("v:", v)
// fmt.Println("sign:", sign)
// fmt.Println("digits:", digits)
// fmt.Println("conf:", conf)
// fmt.Println("state:", *state)
// fmt.Println("datePart:", string(state.d(rev)))
// fmt.Println("parts:", *parts)
// fmt.Println("rev:", rev)
// defer func() {
// fmt.Println("out:", ok, str)
// }()
switch state.component {
case componentNone:
switch digits {
case 4: // [2006](-01-02) => %Y
if sign == 0 {
sign = 1
}
state.component = componentDate
state.datePart = datePartYear
parts.year = v * sign
return "%Y", true
}
case componentDate:
switch d := state.d(rev); d {
case datePartNone:
switch digits {
case 2:
switch conf.DayFirst {
case false:
switch sign {
case 0: // 01-[02] => %d
state.component = componentDate
state.datePart = datePartDay
parts.day = v
return "%d", true
}
case true:
switch sign {
case 0: // 02-[01](-2006) => %m
state.component = componentDate
state.datePart = datePartMonth
parts.month = v
return "%m", true
}
}
case 4: // 02-[2006] => %Y
if sign == 0 {
sign = 1
}
state.component = componentDate
state.datePart = datePartYear
parts.year = v * sign
return "%Y", true
}
case datePartYear:
switch digits {
case 2:
switch conf.DayFirst {
case false:
switch sign {
case 0: // 2006-[01](-02) => %m
state.component = componentDate
state.datePart = datePartMonth
parts.month = v
return "%m", true
}
case true:
switch sign {
case 0: // 2006-[02](-01) => %d
state.component = componentDate
state.datePart = datePartDay
parts.day = v
return "%d", true
}
}
}
case datePartMonth:
switch digits {
case 2:
switch conf.DayFirst {
case false:
switch sign {
case 0: // (2006)-01-[02] => %d
state.component = componentDate
state.datePart = datePartDay
parts.day = v
return "%d", true
}
case true:
switch sign {
case 0: // // [02]-01(-2006) => %m
state.component = componentDate
state.datePart = datePartDay
parts.day = v
return "%d", true
}
}
}
case datePartDay:
switch digits {
case 2:
switch conf.DayFirst {
case true:
switch sign {
case 0: // 2006-02-[01] => %m
state.component = componentDate
state.datePart = datePartMonth
parts.month = v
return "%m", true
}
case false:
switch sign {
case 0: // [01]-2006 => %m
state.component = componentDate
state.datePart = datePartMonth
parts.month = v
return "%m", true
}
}
}
}
}
return "", false
}