forked from araddon/dateparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseany.go
1363 lines (1295 loc) · 31.1 KB
/
parseany.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package dateparse parses date-strings without knowing the format
// in advance, using a fast lex based approach to eliminate shotgun
// attempts. It leans towards US style dates when there is a conflict.
package dateparse
import (
"fmt"
"strconv"
"time"
"unicode"
"unicode/utf8"
)
// func init() {
// gou.SetupLogging("debug")
// gou.SetColorOutput()
// }
type dateState uint8
type timeState uint8
const (
dateStart dateState = iota
dateDigit
dateDigitDash
dateDigitDashDash
dateDigitDashDashWs
dateDigitDashDashT
dateDigitDashDashAlpha
dateDigitDot
dateDigitDotDot
dateDigitSlash
dateDigitChineseYear
dateDigitChineseYearWs
dateDigitWs
dateDigitWsMoYear
dateDigitWsMolong
dateAlpha
dateAlphaWs
dateAlphaWsDigit
dateAlphaWsDigitComma
dateAlphaWsDigitCommaWs
dateAlphaWsDigitCommaWsYear
dateAlphaWsAlpha
dateAlphaWsAlphaYearmaybe
dateWeekdayComma
dateWeekdayAbbrevComma
//dateWeekdayAbbrevCommaDash
)
const (
// Time state
timeIgnore timeState = iota
timeStart
timeWs
timeWsAlpha
timeWsAlphaWs
timeWsAlphaZoneOffset
timeWsAlphaZoneOffsetWs
timeWsAlphaZoneOffsetWsYear
timeWsAlphaZoneOffsetWsExtra
timeWsAMPMMaybe
timeWsAMPM
timeWsOffset
timeWsOffsetWs
timeWsOffsetColonAlpha
timeWsOffsetColon
timeWsYear
timeOffset
timeOffsetColon
timeAlpha
timePeriod
timePeriodOffset
timePeriodOffsetColon
timePeriodOffsetColonWs
timePeriodWs
timePeriodWsAlpha
timePeriodWsOffset
timePeriodWsOffsetWs
timePeriodWsOffsetWsAlpha
timePeriodWsOffsetColon
timePeriodWsOffsetColonAlpha
timeZ
timeZDigit
)
var (
shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"}
)
// ParseAny parse an unknown date format, detect the layout, parse.
// Normal parse. Equivalent Timezone rules as time.Parse()
func ParseAny(datestr string) (time.Time, error) {
return parseTime(datestr, nil)
}
// ParseIn with Location, equivalent to time.ParseInLocation() timezone/offset
// rules. Using location arg, if timezone/offset info exists in the
// datestring, it uses the given location rules for any zone interpretation.
// That is, MST means one thing when using America/Denver and something else
// in other locations.
func ParseIn(datestr string, loc *time.Location) (time.Time, error) {
return parseTime(datestr, loc)
}
// ParseLocal Given an unknown date format, detect the layout,
// using time.Local, parse.
//
// Set Location to time.Local. Same as ParseIn Location but lazily uses
// the global time.Local variable for Location argument.
//
// denverLoc, _ := time.LoadLocation("America/Denver")
// time.Local = denverLoc
//
// t, err := dateparse.ParseLocal("3/1/2014")
//
// Equivalent to:
//
// t, err := dateparse.ParseIn("3/1/2014", denverLoc)
//
func ParseLocal(datestr string) (time.Time, error) {
return parseTime(datestr, time.Local)
}
// MustParse parse a date, and panic if it can't be parsed. Used for testing.
// Not recommended for most use-cases.
func MustParse(datestr string) time.Time {
t, err := parseTime(datestr, nil)
if err != nil {
panic(err.Error())
}
return t
}
func parse(layout, datestr string, loc *time.Location) (time.Time, error) {
if loc == nil {
return time.Parse(layout, datestr)
}
return time.ParseInLocation(layout, datestr, loc)
}
type parser struct {
loc *time.Location
preferMonthFirst bool
stateDate dateState
stateTime timeState
format []byte
datestr string
skip int
extra int
part1Len int
yeari int
yearlen int
moi int
molen int
dayi int
daylen int
houri int
hourlen int
mini int
minlen int
seci int
seclen int
msi int
mslen int
offseti int
offsetlen int
tzi int
tzlen int
}
func newParser(dateStr string, loc *time.Location) *parser {
p := parser{
stateDate: dateStart,
stateTime: timeIgnore,
datestr: dateStr,
loc: loc,
preferMonthFirst: true,
}
p.format = []byte(dateStr)
return &p
}
func (p *parser) set(start int, val string) {
if start < 0 {
return
}
if len(p.format) < start+len(val) {
return
}
for i, r := range val {
p.format[start+i] = byte(r)
}
}
func (p *parser) setMonth() {
if p.molen == 2 {
p.set(p.moi, "01")
} else if p.molen == 1 {
p.set(p.moi, "1")
}
}
func (p *parser) setDay() {
if p.daylen == 2 {
p.set(p.dayi, "02")
} else if p.daylen == 1 {
p.set(p.dayi, "2")
}
}
func (p *parser) setYear() {
if p.yearlen == 2 {
p.set(p.yeari, "06")
} else if p.yearlen == 4 {
p.set(p.yeari, "2006")
}
}
func (p *parser) coalesceDate(end int) {
if p.yeari > 0 {
if p.yearlen == 0 {
p.yearlen = end - p.yeari
}
p.setYear()
}
if p.moi > 0 && p.molen == 0 {
p.molen = end - p.moi
p.setMonth()
}
if p.dayi > 0 && p.daylen == 0 {
p.daylen = end - p.dayi
p.setDay()
}
}
func (p *parser) ts() string {
return fmt.Sprintf("h:(%d:%d) m:(%d:%d) s:(%d:%d)", p.houri, p.hourlen, p.mini, p.minlen, p.seci, p.seclen)
}
func (p *parser) ds() string {
return fmt.Sprintf("%s d:(%d:%d) m:(%d:%d) y:(%d:%d)", p.datestr, p.dayi, p.daylen, p.moi, p.molen, p.yeari, p.yearlen)
}
func (p *parser) coalesceTime(end int) {
// 03:04:05
// 15:04:05
// 3:04:05
// 3:4:5
// 15:04:05.00
if p.houri > 0 {
if p.hourlen == 2 {
p.set(p.houri, "15")
} else if p.hourlen == 1 {
p.set(p.houri, "3")
}
}
if p.mini > 0 {
if p.minlen == 0 {
p.minlen = end - p.mini
}
if p.minlen == 2 {
p.set(p.mini, "04")
} else {
p.set(p.mini, "4")
}
}
if p.seci > 0 {
if p.seclen == 0 {
p.seclen = end - p.seci
}
if p.seclen == 2 {
p.set(p.seci, "05")
} else {
p.set(p.seci, "5")
}
}
if p.msi > 0 {
for i := 0; i < p.mslen; i++ {
p.format[p.msi+i] = '0'
}
}
}
func (p *parser) trimExtra() {
if p.extra > 0 && len(p.format) > p.extra {
p.format = p.format[0:p.extra]
p.datestr = p.datestr[0:p.extra]
}
}
func (p *parser) parse() (time.Time, error) {
if p.skip > 0 && len(p.format) > p.skip {
p.format = p.format[p.skip:]
p.datestr = p.datestr[p.skip:]
}
//gou.Debugf("parse %q AS %s", p.datestr, string(p.format))
if p.loc == nil {
return time.Parse(string(p.format), p.datestr)
}
return time.ParseInLocation(string(p.format), p.datestr, p.loc)
}
func parseTime(datestr string, loc *time.Location) (time.Time, error) {
p := newParser(datestr, loc)
i := 0
// General strategy is to read rune by rune through the date looking for
// certain hints of what type of date we are dealing with.
// Hopefully we only need to read about 5 or 6 bytes before
// we figure it out and then attempt a parse
iterRunes:
for ; i < len(datestr); i++ {
//r := rune(datestr[i])
r, bytesConsumed := utf8.DecodeRuneInString(datestr[i:])
if bytesConsumed > 1 {
i += (bytesConsumed - 1)
}
switch p.stateDate {
case dateStart:
if unicode.IsDigit(r) {
p.stateDate = dateDigit
} else if unicode.IsLetter(r) {
p.stateDate = dateAlpha
}
case dateDigit:
switch r {
case '-', '\u2212':
// 2006-01-02
// 2006-01-02T15:04:05Z07:00
// 13-Feb-03
// 2013-Feb-03
p.stateDate = dateDigitDash
p.yeari = 0
p.yearlen = i
p.moi = i + 1
if i == 4 {
p.set(0, "2006")
}
case '/':
// 03/31/2005
// 2014/02/24
p.stateDate = dateDigitSlash
if i == 4 {
p.yearlen = i
p.moi = i + 1
p.setYear()
} else {
if p.preferMonthFirst {
if p.molen == 0 {
p.molen = i
p.setMonth()
p.dayi = i + 1
}
}
}
case '.':
// 3.31.2014
p.moi = 0
p.molen = i
p.setMonth()
p.dayi = i + 1
p.stateDate = dateDigitDot
case ' ':
// 18 January 2018
// 8 January 2018
// 8 jan 2018
// 02 Jan 2018 23:59
// 02 Jan 2018 23:59:34
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
p.stateDate = dateDigitWs
p.dayi = 0
p.daylen = i
case '年':
// Chinese Year
p.stateDate = dateDigitChineseYear
default:
//if unicode.IsDigit(r) {
continue
}
p.part1Len = i
case dateDigitDash:
// 2006-01
// 2006-01-02
// dateDigitDashDashT
// 2006-01-02T15:04:05Z07:00
// 2017-06-25T17:46:57.45706582-07:00
// 2006-01-02T15:04:05.999999999Z07:00
// 2006-01-02T15:04:05+0000
// dateDigitDashDashWs
// 2012-08-03 18:31:59.257000000
// 2014-04-26 17:24:37.3186369
// 2017-01-27 00:07:31.945167
// 2016-03-14 00:00:00.000
// 2014-05-11 08:20:13,787
// 2017-07-19 03:21:51+00:00
// 2013-04-01 22:43:22
// 2014-04-26 05:24:37 PM
// dateDigitDashDashAlpha
// 2013-Feb-03
// 13-Feb-03
switch r {
case '-':
p.molen = i - p.moi
p.dayi = i + 1
p.stateDate = dateDigitDashDash
p.setMonth()
default:
if unicode.IsDigit(r) {
//continue
} else if unicode.IsLetter(r) {
p.stateDate = dateDigitDashDashAlpha
}
}
case dateDigitDashDash:
// 2006-01-02
// dateDigitDashDashT
// 2006-01-02T15:04:05Z07:00
// 2017-06-25T17:46:57.45706582-07:00
// 2006-01-02T15:04:05.999999999Z07:00
// 2006-01-02T15:04:05+0000
// dateDigitDashDashWs
// 2012-08-03 18:31:59.257000000
// 2014-04-26 17:24:37.3186369
// 2017-01-27 00:07:31.945167
// 2016-03-14 00:00:00.000
// 2014-05-11 08:20:13,787
// 2017-07-19 03:21:51+00:00
// 2013-04-01 22:43:22
// 2014-04-26 05:24:37 PM
switch r {
case ' ':
p.daylen = i - p.dayi
p.stateDate = dateDigitDashDashWs
p.stateTime = timeStart
p.setDay()
break iterRunes
case 'T':
p.daylen = i - p.dayi
p.stateDate = dateDigitDashDashT
p.stateTime = timeStart
p.setDay()
break iterRunes
}
case dateDigitDashDashAlpha:
// 2013-Feb-03
// 13-Feb-03
switch r {
case '-':
p.molen = i - p.moi
p.set(p.moi, "Jan")
p.dayi = i + 1
}
case dateDigitSlash:
// 2014/07/10 06:55:38.156283
// 03/19/2012 10:11:59
// 04/2/2014 03:00:37
// 3/1/2012 10:11:59
// 4/8/2014 22:05
// 3/1/2014
// 10/13/2014
// 01/02/2006
// 1/2/06
switch r {
case ' ':
p.stateTime = timeStart
if p.yearlen == 0 {
p.yearlen = i - p.yeari
p.setYear()
} else if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
}
break iterRunes
case '/':
if p.yearlen > 0 {
// 2014/07/10 06:55:38.156283
if p.molen == 0 {
p.molen = i - p.moi
p.setMonth()
p.dayi = i + 1
}
} else if p.preferMonthFirst {
if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
p.yeari = i + 1
}
}
default:
// if unicode.IsDigit(r) || r == '/' {
// continue
// }
}
case dateDigitWs:
// 18 January 2018
// 8 January 2018
// 8 jan 2018
// 1 jan 18
// 02 Jan 2018 23:59
// 02 Jan 2018 23:59:34
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
switch r {
case ' ':
p.yeari = i + 1
//p.yearlen = 4
p.dayi = 0
p.daylen = p.part1Len
p.setDay()
p.stateTime = timeStart
if i <= len("12 Feb") {
p.moi = p.daylen + 1
p.molen = 3
p.set(p.moi, "Jan")
p.stateDate = dateDigitWsMoYear
} else {
p.stateDate = dateDigitWsMolong
}
}
case dateDigitWsMoYear:
// 8 jan 2018
// 02 Jan 2018 23:59
// 02 Jan 2018 23:59:34
// 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22
switch r {
case ',':
p.yearlen = i - p.yeari
p.setYear()
i++
break iterRunes
case ' ':
p.yearlen = i - p.yeari
p.setYear()
break iterRunes
}
case dateDigitWsMolong:
// 18 January 2018
// 8 January 2018
case dateDigitChineseYear:
// dateDigitChineseYear
// 2014年04月08日
// weekday %Y年%m月%e日 %A %I:%M %p
// 2013年07月18日 星期四 10:27 上午
if r == ' ' {
p.stateDate = dateDigitChineseYearWs
break
}
case dateDigitDot:
// 3.31.2014
if r == '.' {
p.daylen = i - p.dayi
p.yeari = i + 1
p.setDay()
p.stateDate = dateDigitDotDot
}
case dateDigitDotDot:
// iterate all the way through
case dateAlpha:
// dateAlphaWS
// Mon Jan _2 15:04:05 2006
// Mon Jan _2 15:04:05 MST 2006
// Mon Jan 02 15:04:05 -0700 2006
// Mon Aug 10 15:44:11 UTC+0100 2015
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
// dateAlphaWSDigit
// May 8, 2009 5:57:51 PM
// oct 1, 1970
//
// dateWeekdayComma
// Monday, 02 Jan 2006 15:04:05 MST
// Monday, 02-Jan-06 15:04:05 MST
// Monday, 02 Jan 2006 15:04:05 -0700
// Monday, 02 Jan 2006 15:04:05 +0100
// dateWeekdayAbbrevComma
// Mon, 02 Jan 2006 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 -0700
// Thu, 13 Jul 2017 08:58:40 +0100
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
// Mon, 02-Jan-06 15:04:05 MST
switch {
case r == ' ':
if i > 4 {
// September 17, 2012 at 5:00pm UTC-05
// This one doesn't follow standard parse methodologies. the "January"
// is difficult to use the format string replace method because of its variable-length (march, june)
// so we just use this format here. If we see more similar to this we will do something else.
return parse("January 02, 2006 at 3:04pm MST-07", datestr, loc)
}
p.stateDate = dateAlphaWs
case r == ',':
// p.moi = 0
// p.molen = i
if i == 3 {
p.stateDate = dateWeekdayAbbrevComma
p.set(0, "Mon")
} else {
p.stateDate = dateWeekdayComma
//p.set(0, "Monday")
p.skip = i + 2
i++
// TODO: lets just make this "skip" as we don't need
// the mon, monday, they are all superfelous and not needed
// just lay down the skip, no need to fill and then skip
}
}
case dateWeekdayComma:
// Monday, 02 Jan 2006 15:04:05 MST
// Monday, 02 Jan 2006 15:04:05 -0700
// Monday, 02 Jan 2006 15:04:05 +0100
// Monday, 02-Jan-06 15:04:05 MST
if p.dayi == 0 {
p.dayi = i
}
switch r {
case ' ', '-':
if p.moi == 0 {
p.moi = i + 1
p.daylen = i - p.dayi
p.setDay()
} else if p.yeari == 0 {
p.yeari = i + 1
p.molen = i - p.moi
p.set(p.moi, "Jan")
} else {
p.stateTime = timeStart
break iterRunes
}
}
case dateWeekdayAbbrevComma:
// Mon, 02 Jan 2006 15:04:05 MST
// Mon, 02 Jan 2006 15:04:05 -0700
// Thu, 13 Jul 2017 08:58:40 +0100
// Thu, 4 Jan 2018 17:53:36 +0000
// Tue, 11 Jul 2017 16:28:13 +0200 (CEST)
// Mon, 02-Jan-06 15:04:05 MST
switch r {
case ' ', '-':
if p.dayi == 0 {
p.dayi = i + 1
} else if p.moi == 0 {
p.daylen = i - p.dayi
p.setDay()
p.moi = i + 1
} else if p.yeari == 0 {
p.molen = i - p.moi
p.set(p.moi, "Jan")
p.yeari = i + 1
} else {
p.yearlen = i - p.yeari
p.setYear()
p.stateTime = timeStart
break iterRunes
}
}
case dateAlphaWs:
// dateAlphaWsAlpha
// Mon Jan _2 15:04:05 2006
// Mon Jan _2 15:04:05 MST 2006
// Mon Jan 02 15:04:05 -0700 2006
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
// Mon Aug 10 15:44:11 UTC+0100 2015
// dateAlphaWsDigit
// May 8, 2009 5:57:51 PM
// oct 1, 1970
// oct 7, '70
switch {
case unicode.IsLetter(r):
p.set(0, "Mon")
p.stateDate = dateAlphaWsAlpha
//p.moi = i
p.set(i, "Jan")
case unicode.IsDigit(r):
p.set(0, "Jan")
p.stateDate = dateAlphaWsDigit
p.dayi = i
}
case dateAlphaWsDigit:
// May 8, 2009 5:57:51 PM
// oct 1, 1970
// oct 7, '70
//gou.Debugf("%d %s dateAlphaWsDigit %s %s", i, string(r), p.ds(), p.ts())
if r == ',' {
p.daylen = i - p.dayi
p.setDay()
p.stateDate = dateAlphaWsDigitComma
}
case dateAlphaWsDigitComma:
// x
// May 8, 2009 5:57:51 PM
// oct 1, 1970
// oct 7, '70
if r == ' ' {
p.stateDate = dateAlphaWsDigitCommaWs
p.yeari = i + 1
}
case dateAlphaWsDigitCommaWs:
// x
// May 8, 2009 5:57:51 PM
// oct 1, 1970
// oct 7, '70
switch r {
case '\'':
p.yeari = i + 1
case ' ':
p.stateDate = dateAlphaWsDigitCommaWsYear
p.yearlen = i - p.yeari
p.setYear()
p.stateTime = timeStart
break iterRunes
}
case dateAlphaWsAlpha:
// Mon Jan _2 15:04:05 2006
// Mon Jan 02 15:04:05 -0700 2006
// Mon Jan _2 15:04:05 MST 2006
// Mon Aug 10 15:44:11 UTC+0100 2015
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
if r == ' ' {
if p.dayi > 0 {
p.daylen = i - p.dayi
p.setDay()
p.yeari = i + 1
p.stateDate = dateAlphaWsAlphaYearmaybe
p.stateTime = timeStart
}
} else if unicode.IsDigit(r) {
if p.dayi == 0 {
p.dayi = i
}
}
case dateAlphaWsAlphaYearmaybe:
// x
// Mon Jan _2 15:04:05 2006
// Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
if r == ':' {
i = i - 3
p.stateDate = dateAlphaWsAlpha
p.yeari = 0
break iterRunes
} else if r == ' ' {
// must be year format, not 15:04
p.yearlen = i - p.yeari
p.setYear()
break iterRunes
}
default:
break iterRunes
}
}
p.coalesceDate(i)
if p.stateTime == timeStart {
// increment first one, since the i++ occurs at end of loop
if i < len(p.datestr) {
i++
}
// ensure we skip any whitespace prefix
for ; i < len(datestr); i++ {
r := rune(datestr[i])
if r != ' ' {
break
}
}
iterTimeRunes:
for ; i < len(datestr); i++ {
r := rune(datestr[i])
//gou.Debugf("%d %s iterTimeRunes %s %s", i, string(r), p.ds(), p.ts())
switch p.stateTime {
case timeStart:
// 22:43:22
// 22:43
// timeComma
// 08:20:13,787
// timeWs
// 05:24:37 PM
// 06:20:00 UTC
// 00:12:00 +0000 UTC
// 15:04:05 -0700
// 15:04:05 -07:00
// 15:04:05 2008
// timeOffset
// 03:21:51+00:00
// 19:55:00+0100
// timePeriod
// 17:24:37.3186369
// 00:07:31.945167
// 18:31:59.257000000
// 00:00:00.000
// timePeriodOffset
// 19:55:00.799+0100
// timePeriodOffsetColon
// 15:04:05.999-07:00
// timePeriodWs
// timePeriodWsOffset
// 00:07:31.945167 +0000
// 00:00:00.000 +0000
// timePeriodWsOffsetAlpha
// 00:07:31.945167 +0000 UTC
// 00:00:00.000 +0000 UTC
// timePeriodWsAlpha
// 06:20:00.000 UTC
if p.houri == 0 {
p.houri = i
}
switch r {
case ',':
// hm, lets just swap out comma for period. for some reason go
// won't parse it.
// 2014-05-11 08:20:13,787
ds := []byte(p.datestr)
ds[i] = '.'
return parseTime(string(ds), loc)
case '-', '+':
// 03:21:51+00:00
p.stateTime = timeOffset
p.seclen = i - p.seci
p.offseti = i
case '.':
p.stateTime = timePeriod
p.seclen = i - p.seci
p.msi = i + 1
case 'Z':
p.stateTime = timeZ
if p.seci == 0 {
p.minlen = i - p.mini
} else {
p.seclen = i - p.seci
}
case ' ':
p.coalesceTime(i)
p.stateTime = timeWs
case ':':
if p.mini == 0 {
p.mini = i + 1
p.hourlen = i - p.houri
} else if p.seci == 0 {
p.seci = i + 1
p.minlen = i - p.mini
}
}
case timeOffset:
// 19:55:00+0100
// timeOffsetColon
// 15:04:05+07:00
// 15:04:05-07:00
if r == ':' {
p.stateTime = timeOffsetColon
}
case timeWs:
// timeWsAlpha
// 06:20:00 UTC
// 15:44:11 UTC+0100 2015
// 18:04:07 GMT+0100 (GMT Daylight Time)
// 17:57:51 MST 2009
// timeWsAMPMMaybe
// 05:24:37 PM
// timeWsOffset
// 15:04:05 -0700
// 00:12:00 +0000 UTC
// timeWsOffsetColon
// 15:04:05 -07:00
// 17:57:51 -0700 2009
// timeWsOffsetColonAlpha
// 00:12:00 +00:00 UTC
// timeWsYear
// 00:12:00 2008
// timeZ
// 15:04:05.99Z
switch r {
case 'A', 'P':
// Could be AM/PM or could be PST or similar
p.tzi = i
p.stateTime = timeWsAMPMMaybe
case '+', '-':
p.offseti = i
p.stateTime = timeWsOffset
default:
if unicode.IsLetter(r) {
// 06:20:00 UTC
// 15:44:11 UTC+0100 2015
// 17:57:51 MST 2009
p.tzi = i
p.stateTime = timeWsAlpha
//break iterTimeRunes
} else if unicode.IsDigit(r) {
// 00:12:00 2008
p.stateTime = timeWsYear
p.yeari = i
}
}
case timeWsAlpha:
// 06:20:00 UTC
// timeWsAlphaWs
// 17:57:51 MST 2009
// timeWsAlphaZoneOffset
// timeWsAlphaZoneOffsetWs
// timeWsAlphaZoneOffsetWsExtra
// 18:04:07 GMT+0100 (GMT Daylight Time)
// timeWsAlphaZoneOffsetWsYear
// 15:44:11 UTC+0100 2015
switch r {
case '+', '-':
p.tzlen = i - p.tzi
if p.tzlen == 4 {
p.set(p.tzi, " MST")
} else if p.tzlen == 3 {
p.set(p.tzi, "MST")
}
p.stateTime = timeWsAlphaZoneOffset
p.offseti = i
case ' ':
// 17:57:51 MST 2009
p.stateTime = timeWsAlphaWs
p.yeari = i + 1
}
case timeWsAlphaWs:
// 17:57:51 MST 2009
case timeWsAlphaZoneOffset:
// timeWsAlphaZoneOffset
// timeWsAlphaZoneOffsetWs
// timeWsAlphaZoneOffsetWsExtra
// 18:04:07 GMT+0100 (GMT Daylight Time)
// timeWsAlphaZoneOffsetWsYear
// 15:44:11 UTC+0100 2015
switch r {
case ' ':
p.set(p.offseti, "-0700")
p.yeari = i + 1
p.stateTime = timeWsAlphaZoneOffsetWs
}
case timeWsAlphaZoneOffsetWs:
// timeWsAlphaZoneOffsetWs
// timeWsAlphaZoneOffsetWsExtra
// 18:04:07 GMT+0100 (GMT Daylight Time)
// timeWsAlphaZoneOffsetWsYear
// 15:44:11 UTC+0100 2015
if unicode.IsDigit(r) {
p.stateTime = timeWsAlphaZoneOffsetWsYear
} else {
p.extra = i - 1
p.stateTime = timeWsAlphaZoneOffsetWsExtra
}
case timeWsAlphaZoneOffsetWsYear:
// 15:44:11 UTC+0100 2015
if unicode.IsDigit(r) {
p.yearlen = i - p.yeari + 1
if p.yearlen == 4 {
p.setYear()
}
}
case timeWsAMPMMaybe:
// timeWsAMPMMaybe
// timeWsAMPM
// 05:24:37 PM
// timeWsAlpha
// 00:12:00 PST
// 15:44:11 UTC+0100 2015
if r == 'M' {
//return parse("2006-01-02 03:04:05 PM", datestr, loc)
p.stateTime = timeWsAMPM
p.set(i-1, "PM")
if p.hourlen == 2 {
p.set(p.houri, "03")
} else if p.hourlen == 1 {
p.set(p.houri, "3")
}
} else {
p.stateTime = timeWsAlpha
}
case timeWsOffset:
// timeWsOffset
// 15:04:05 -0700
// timeWsOffsetWs
// 17:57:51 -0700 2009
// 00:12:00 +0000 UTC
// timeWsOffsetColon
// 15:04:05 -07:00
// timeWsOffsetColonAlpha
// 00:12:00 +00:00 UTC
switch r {
case ':':
p.stateTime = timeWsOffsetColon