forked from araddon/dateparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseany.go
2199 lines (2083 loc) · 51.2 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"
"strings"
"time"
"unicode"
"unicode/utf8"
)
// func init() {
// gou.SetupLogging("debug")
// gou.SetColorOutput()
// }
var days = []string{
"mon",
"tue",
"wed",
"thu",
"fri",
"sat",
"sun",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
}
var months = []string{
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december",
}
type dateState uint8
type timeState uint8
const (
dateStart dateState = iota // 0
dateDigit
dateDigitSt
dateYearDash
dateYearDashAlphaDash
dateYearDashDash
dateYearDashDashWs // 5
dateYearDashDashT
dateYearDashDashOffset
dateDigitDash
dateDigitDashAlpha
dateDigitDashAlphaDash // 10
dateDigitDot
dateDigitDotDot
dateDigitSlash
dateDigitYearSlash
dateDigitSlashAlpha // 15
dateDigitColon
dateDigitChineseYear
dateDigitChineseYearWs
dateDigitWs
dateDigitWsMoYear // 20
dateDigitWsMolong
dateAlpha
dateAlphaWs
dateAlphaWsDigit
dateAlphaWsDigitMore // 25
dateAlphaWsDigitMoreWs
dateAlphaWsDigitMoreWsYear
dateAlphaWsMonth
dateAlphaWsDigitYearmaybe
dateAlphaWsMonthMore
dateAlphaWsMonthSuffix
dateAlphaWsMore
dateAlphaWsAtTime
dateAlphaWsAlpha
dateAlphaWsAlphaYearmaybe // 35
dateAlphaPeriodWsDigit
dateWeekdayComma
dateWeekdayAbbrevComma
)
const (
// Time state
timeIgnore timeState = iota // 0
timeStart
timeWs
timeWsAlpha
timeWsAlphaWs
timeWsAlphaZoneOffset // 5
timeWsAlphaZoneOffsetWs
timeWsAlphaZoneOffsetWsYear
timeWsAlphaZoneOffsetWsExtra
timeWsAMPMMaybe
timeWsAMPM // 10
timeWsOffset
timeWsOffsetWs // 12
timeWsOffsetColonAlpha
timeWsOffsetColon
timeWsYear // 15
timeOffset
timeOffsetColon
timeAlpha
timePeriod
timePeriodOffset // 20
timePeriodOffsetColon
timePeriodOffsetColonWs
timePeriodWs
timePeriodWsAlpha
timePeriodWsOffset // 25
timePeriodWsOffsetWs
timePeriodWsOffsetWsAlpha
timePeriodWsOffsetColon
timePeriodWsOffsetColonAlpha
timeZ
timeZDigit
)
var (
// ErrAmbiguousMMDD for date formats such as 04/02/2014 the mm/dd vs dd/mm are
// ambiguous, so it is an error for strict parse rules.
ErrAmbiguousMMDD = fmt.Errorf("this date has ambiguous mm/dd vs dd/mm type format")
)
func unknownErr(datestr string) error {
return fmt.Errorf("could not find format for %q", datestr)
}
// ParseAny parse an unknown date format, detect the layout.
// Normal parse. Equivalent Timezone rules as time.Parse().
// NOTE: please see readme on mmdd vs ddmm ambiguous dates.
func ParseAny(datestr string, opts ...ParserOption) (time.Time, error) {
p, err := parseTime(datestr, nil, opts...)
if err != nil {
return time.Time{}, err
}
return p.parse()
}
// 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, opts ...ParserOption) (time.Time, error) {
p, err := parseTime(datestr, loc, opts...)
if err != nil {
return time.Time{}, err
}
return p.parse()
}
// 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, opts ...ParserOption) (time.Time, error) {
p, err := parseTime(datestr, time.Local, opts...)
if err != nil {
return time.Time{}, err
}
return p.parse()
}
// 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, opts ...ParserOption) time.Time {
p, err := parseTime(datestr, nil, opts...)
if err != nil {
panic(err.Error())
}
t, err := p.parse()
if err != nil {
panic(err.Error())
}
return t
}
// ParseFormat parse's an unknown date-time string and returns a layout
// string that can parse this (and exact same format) other date-time strings.
//
// layout, err := dateparse.ParseFormat("2013-02-01 00:00:00")
// // layout = "2006-01-02 15:04:05"
//
func ParseFormat(datestr string, opts ...ParserOption) (string, error) {
p, err := parseTime(datestr, nil, opts...)
if err != nil {
return "", err
}
_, err = p.parse()
if err != nil {
return "", err
}
return string(p.format), nil
}
// ParseStrict parse an unknown date format. IF the date is ambigous
// mm/dd vs dd/mm then return an error. These return errors: 3.3.2014 , 8/8/71 etc
func ParseStrict(datestr string, opts ...ParserOption) (time.Time, error) {
p, err := parseTime(datestr, nil, opts...)
if err != nil {
return time.Time{}, err
}
if p.ambiguousMD {
return time.Time{}, ErrAmbiguousMMDD
}
return p.parse()
}
func parseTime(datestr string, loc *time.Location, opts ...ParserOption) (p *parser, err error) {
p = newParser(datestr, loc, opts...)
if p.retryAmbiguousDateWithSwap {
// month out of range signifies that a day/month swap is the correct solution to an ambiguous date
// this is because it means that a day is being interpreted as a month and overflowing the valid value for that
// by retrying in this case, we can fix a common situation with no assumptions
defer func() {
if p != nil && p.ambiguousMD {
// if it errors out with the following error, swap before we
// get out of this function to reduce scope it needs to be applied on
_, err := p.parse()
if err != nil && strings.Contains(err.Error(), "month out of range") {
// create the option to reverse the preference
preferMonthFirst := PreferMonthFirst(!p.preferMonthFirst)
// turn off the retry to avoid endless recursion
retryAmbiguousDateWithSwap := RetryAmbiguousDateWithSwap(false)
modifiedOpts := append(opts, preferMonthFirst, retryAmbiguousDateWithSwap)
p, err = parseTime(datestr, time.Local, modifiedOpts...)
}
}
}()
}
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)
}
// gou.Debugf("i=%d r=%s state=%d %s", i, string(r), p.stateDate, datestr)
switch p.stateDate {
case dateStart:
if unicode.IsDigit(r) {
p.stateDate = dateDigit
} else if unicode.IsLetter(r) {
p.stateDate = dateAlpha
} else {
return nil, unknownErr(datestr)
}
case dateDigit:
switch r {
case '-', '\u2212':
// 2006-01-02
// 2013-Feb-03
// 13-Feb-03
// 29-Jun-2016
if i == 4 {
p.stateDate = dateYearDash
p.yeari = 0
p.yearlen = i
p.moi = i + 1
p.set(0, "2006")
} else {
p.stateDate = dateDigitDash
}
case '/':
// 08/May/2005
// 03/31/2005
// 2014/02/24
p.stateDate = dateDigitSlash
if i == 4 {
// 2014/02/24 - Year first /
p.yearlen = i // since it was start of datestr, i=len
p.moi = i + 1
p.setYear()
p.stateDate = dateDigitYearSlash
} else {
// Either Ambiguous dd/mm vs mm/dd OR dd/month/yy
// 08/May/2005
// 03/31/2005
// 31/03/2005
if i+2 < len(p.datestr) && unicode.IsLetter(rune(datestr[i+1])) {
// 08/May/2005
p.stateDate = dateDigitSlashAlpha
p.moi = i + 1
p.daylen = 2
p.dayi = 0
p.setDay()
continue
}
// Ambiguous dd/mm vs mm/dd the bane of date-parsing
// 03/31/2005
// 31/03/2005
p.ambiguousMD = true
if p.preferMonthFirst {
if p.molen == 0 {
// 03/31/2005
p.molen = i
p.setMonth()
p.dayi = i + 1
}
} else {
if p.daylen == 0 {
p.daylen = i
p.setDay()
p.moi = i + 1
}
}
}
case ':':
// 03/31/2005
// 2014/02/24
p.stateDate = dateDigitColon
if i == 4 {
p.yearlen = i
p.moi = i + 1
p.setYear()
} else {
p.ambiguousMD = true
if p.preferMonthFirst {
if p.molen == 0 {
p.molen = i
p.setMonth()
p.dayi = i + 1
}
} else {
if p.daylen == 0 {
p.daylen = i
p.setDay()
p.moi = i + 1
}
}
}
case '.':
// 3.31.2014
// 08.21.71
// 2014.05
p.stateDate = dateDigitDot
if i == 4 {
p.yearlen = i
p.moi = i + 1
p.setYear()
} else {
p.ambiguousMD = true
p.moi = 0
p.molen = i
p.setMonth()
p.dayi = i + 1
}
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
if i == 6 {
p.stateDate = dateDigitSt
} else {
p.stateDate = dateDigitWs
p.dayi = 0
p.daylen = i
}
case '年':
// Chinese Year
p.stateDate = dateDigitChineseYear
case ',':
return nil, unknownErr(datestr)
default:
continue
}
p.part1Len = i
case dateDigitSt:
p.set(0, "060102")
i = i - 1
p.stateTime = timeStart
break iterRunes
case dateYearDash:
// dateYearDashDashT
// 2006-01-02T15:04:05Z07:00
// 2020-08-17T17:00:00:000+0100
// dateYearDashDashWs
// 2013-04-01 22:43:22
// dateYearDashAlphaDash
// 2013-Feb-03
switch r {
case '-':
p.molen = i - p.moi
p.dayi = i + 1
p.stateDate = dateYearDashDash
p.setMonth()
default:
if unicode.IsLetter(r) {
p.stateDate = dateYearDashAlphaDash
}
}
case dateYearDashDash:
// dateYearDashDashT
// 2006-01-02T15:04:05Z07:00
// dateYearDashDashWs
// 2013-04-01 22:43:22
// dateYearDashDashOffset
// 2020-07-20+00:00
switch r {
case '+', '-':
p.offseti = i
p.daylen = i - p.dayi
p.stateDate = dateYearDashDashOffset
p.setDay()
case ' ':
p.daylen = i - p.dayi
p.stateDate = dateYearDashDashWs
p.stateTime = timeStart
p.setDay()
break iterRunes
case 'T':
p.daylen = i - p.dayi
p.stateDate = dateYearDashDashT
p.stateTime = timeStart
p.setDay()
break iterRunes
}
case dateYearDashDashT:
// dateYearDashDashT
// 2006-01-02T15:04:05Z07:00
// 2020-08-17T17:00:00:000+0100
case dateYearDashDashOffset:
// 2020-07-20+00:00
switch r {
case ':':
p.set(p.offseti, "-07:00")
// case ' ':
// return nil, unknownErr(datestr)
}
case dateYearDashAlphaDash:
// 2013-Feb-03
switch r {
case '-':
p.molen = i - p.moi
p.set(p.moi, "Jan")
p.dayi = i + 1
}
case dateDigitDash:
// 13-Feb-03
// 29-Jun-2016
if unicode.IsLetter(r) {
p.stateDate = dateDigitDashAlpha
p.moi = i
} else {
return nil, unknownErr(datestr)
}
case dateDigitDashAlpha:
// 13-Feb-03
// 28-Feb-03
// 29-Jun-2016
switch r {
case '-':
p.molen = i - p.moi
p.set(p.moi, "Jan")
p.yeari = i + 1
p.stateDate = dateDigitDashAlphaDash
}
case dateDigitDashAlphaDash:
// 13-Feb-03 ambiguous
// 28-Feb-03 ambiguous
// 29-Jun-2016 dd-month(alpha)-yyyy
switch r {
case ' ':
// we need to find if this was 4 digits, aka year
// or 2 digits which makes it ambiguous year/day
length := i - (p.moi + p.molen + 1)
if length == 4 {
p.yearlen = 4
p.set(p.yeari, "2006")
// We now also know that part1 was the day
p.dayi = 0
p.daylen = p.part1Len
p.setDay()
} else if length == 2 {
// We have no idea if this is
// yy-mon-dd OR dd-mon-yy
//
// We are going to ASSUME (bad, bad) that it is dd-mon-yy which is a horible assumption
p.ambiguousMD = true
p.yearlen = 2
p.set(p.yeari, "06")
// We now also know that part1 was the day
p.dayi = 0
p.daylen = p.part1Len
p.setDay()
}
p.stateTime = timeStart
break iterRunes
}
case dateDigitYearSlash:
// 2014/07/10 06:55:38.156283
// I honestly don't know if this format ever shows up as yyyy/
switch r {
case ' ', ':':
p.stateTime = timeStart
if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
}
break iterRunes
case '/':
if p.molen == 0 {
p.molen = i - p.moi
p.setMonth()
p.dayi = i + 1
}
}
case dateDigitSlashAlpha:
// 06/May/2008
switch r {
case '/':
// |
// 06/May/2008
if p.molen == 0 {
p.set(p.moi, "Jan")
p.yeari = i + 1
}
// We aren't breaking because we are going to re-use this case
// to find where the date starts, and possible time begins
case ' ', ':':
p.stateTime = timeStart
if p.yearlen == 0 {
p.yearlen = i - p.yeari
p.setYear()
}
break iterRunes
}
case dateDigitSlash:
// 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 '/':
// This is the 2nd / so now we should know start pts of all of the dd, mm, yy
if p.preferMonthFirst {
if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
p.yeari = i + 1
}
} else {
if p.molen == 0 {
p.molen = i - p.moi
p.setMonth()
p.yeari = i + 1
}
}
// Note no break, we are going to pass by and re-enter this dateDigitSlash
// and look for ending (space) or not (just date)
case ' ':
p.stateTime = timeStart
if p.yearlen == 0 {
p.yearlen = i - p.yeari
p.setYear()
}
break iterRunes
}
case dateDigitColon:
// 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
}
} else {
if p.molen == 0 {
p.molen = i - p.moi
p.setMonth()
p.yeari = i + 1
}
}
}
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 > p.daylen+len(" Sep") { // November etc
// If len greather than space + 3 it must be full month
p.stateDate = dateDigitWsMolong
} else {
// If len=3, the might be Feb or May? Ie ambigous abbreviated but
// we can parse may with either. BUT, that means the
// format may not be correct?
// mo := strings.ToLower(datestr[p.daylen+1 : i])
p.moi = p.daylen + 1
p.molen = i - p.moi
p.set(p.moi, "Jan")
p.stateDate = dateDigitWsMoYear
}
}
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
}
case dateDigitDot:
// This is the 2nd period
// 3.31.2014
// 08.21.71
// 2014.05
// 2018.09.30
if r == '.' {
if p.moi == 0 {
// 3.31.2014
p.daylen = i - p.dayi
p.yeari = i + 1
p.setDay()
p.stateDate = dateDigitDotDot
} else {
// 2018.09.30
//p.molen = 2
p.molen = i - p.moi
p.dayi = i + 1
p.setMonth()
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
// dateAlphaWsMonth
// April 8, 2009
// dateAlphaWsMore
// dateAlphaWsAtTime
// January 02, 2006 at 3:04pm MST-07
//
// dateAlphaPeriodWsDigit
// 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 == ' ':
// X
// April 8, 2009
if i > 3 {
// Check to see if the alpha is name of month? or Day?
month := strings.ToLower(datestr[0:i])
if isMonthFull(month) {
p.fullMonth = month
// len(" 31, 2018") = 9
if len(datestr[i:]) < 10 {
// April 8, 2009
p.stateDate = dateAlphaWsMonth
} else {
p.stateDate = dateAlphaWsMore
}
p.dayi = i + 1
break
}
} else {
// This is possibly ambiguous? May will parse as either though.
// So, it could return in-correct format.
// dateAlphaWs
// May 05, 2005, 05:05:05
// May 05 2005, 05:05:05
// Jul 05, 2005, 05:05:05
// May 8 17:57:51 2009
// May 8 17:57:51 2009
// skip & return to dateStart
// Tue 05 May 2020, 05:05:05
// Mon Jan 2 15:04:05 2006
maybeDay := strings.ToLower(datestr[0:i])
if isDay(maybeDay) {
// using skip throws off indices used by other code; saner to restart
return parseTime(datestr[i+1:], loc)
}
p.stateDate = dateAlphaWs
}
case r == ',':
// Mon, 02 Jan 2006
if i == 3 {
p.stateDate = dateWeekdayAbbrevComma
p.set(0, "Mon")
} else {
p.stateDate = dateWeekdayComma
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 r == '.':
// sept. 28, 2017
// jan. 28, 2017
p.stateDate = dateAlphaPeriodWsDigit
if i == 3 {
p.molen = i
p.set(0, "Jan")
} else if i == 4 {
// gross
datestr = datestr[0:i-1] + datestr[i:]
return parseTime(datestr, loc, opts...)
} else {
return nil, unknownErr(datestr)
}
}
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
// May 8 2009 5:57:51 PM
// May 8 17:57:51 2009
// May 8 17:57:51 2009
// May 08 17:57:51 2009
// oct 1, 1970
// oct 7, '70
switch {
case unicode.IsLetter(r):
p.set(0, "Mon")
p.stateDate = dateAlphaWsAlpha
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
// May 8 2009 5:57:51 PM
// oct 1, 1970
// oct 7, '70
// oct. 7, 1970
// May 8 17:57:51 2009
// May 8 17:57:51 2009
// May 08 17:57:51 2009
if r == ',' {
p.daylen = i - p.dayi
p.setDay()
p.stateDate = dateAlphaWsDigitMore
} else if r == ' ' {
p.daylen = i - p.dayi
p.setDay()
p.yeari = i + 1
p.stateDate = dateAlphaWsDigitYearmaybe
p.stateTime = timeStart
} else if unicode.IsLetter(r) {
p.stateDate = dateAlphaWsMonthSuffix
i--
}
case dateAlphaWsDigitYearmaybe:
// x
// May 8 2009 5:57:51 PM
// May 8 17:57:51 2009
// May 8 17:57:51 2009
// May 08 17:57:51 2009
// Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)
if r == ':' {
// Guessed wrong; was not a year
i = i - 3
p.stateDate = dateAlphaWsDigit
p.yeari = 0
break iterRunes
} else if r == ' ' {
// must be year format, not 15:04
p.yearlen = i - p.yeari
p.setYear()
break iterRunes
}
case dateAlphaWsDigitMore:
// x
// May 8, 2009 5:57:51 PM
// May 05, 2005, 05:05:05
// May 05 2005, 05:05:05
// oct 1, 1970
// oct 7, '70
if r == ' ' {
p.yeari = i + 1
p.stateDate = dateAlphaWsDigitMoreWs
}
case dateAlphaWsDigitMoreWs:
// x
// May 8, 2009 5:57:51 PM
// May 05, 2005, 05:05:05
// oct 1, 1970
// oct 7, '70
switch r {
case '\'':
p.yeari = i + 1
case ' ', ',':
// x
// May 8, 2009 5:57:51 PM
// x
// May 8, 2009, 5:57:51 PM
p.stateDate = dateAlphaWsDigitMoreWsYear
p.yearlen = i - p.yeari
p.setYear()
p.stateTime = timeStart
break iterRunes
}
case dateAlphaWsMonth:
// April 8, 2009
// April 8 2009
switch r {
case ' ', ',':
// x
// June 8, 2009
// x
// June 8 2009
if p.daylen == 0 {
p.daylen = i - p.dayi
p.setDay()
}
case 's', 'S', 'r', 'R', 't', 'T', 'n', 'N':
// st, rd, nd, st
i--
p.stateDate = dateAlphaWsMonthSuffix
default:
if p.daylen > 0 && p.yeari == 0 {
p.yeari = i
}
}
case dateAlphaWsMonthMore:
// X
// January 02, 2006, 15:04:05
// January 02 2006, 15:04:05
// January 02, 2006 15:04:05
// January 02 2006 15:04:05
switch r {
case ',':
p.yearlen = i - p.yeari
p.setYear()
p.stateTime = timeStart
i++
break iterRunes
case ' ':
p.yearlen = i - p.yeari
p.setYear()
p.stateTime = timeStart
break iterRunes
}
case dateAlphaWsMonthSuffix:
// x
// April 8th, 2009
// April 8th 2009
switch r {
case 't', 'T':
if p.nextIs(i, 'h') || p.nextIs(i, 'H') {
if len(datestr) > i+2 {