Skip to content

Commit bfdab07

Browse files
committed
Add support for comma format like "04/2/2014, 03:00:37"
Ref: araddon/pull/156
1 parent 00672cf commit bfdab07

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

parseany.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ iterRunes:
650650
case dateDigitSlash:
651651
// 03/19/2012 10:11:59
652652
// 04/2/2014 03:00:37
653+
// 04/2/2014, 03:00:37
653654
// 3/1/2012 10:11:59
654655
// 4/8/2014 22:05
655656
// 3/1/2014
@@ -682,6 +683,14 @@ iterRunes:
682683
p.setYear()
683684
}
684685
break iterRunes
686+
case ',':
687+
p.stateTime = timeStart
688+
if p.yearlen == 0 {
689+
p.yearlen = i - p.yeari
690+
i++
691+
p.setYear()
692+
}
693+
break iterRunes
685694
}
686695

687696
case dateDigitColon:
@@ -807,7 +816,6 @@ iterRunes:
807816
// 2013年07月18日 星期四 10:27 上午
808817
if r == ' ' {
809818
p.stateDate = dateDigitChineseYearWs
810-
break
811819
}
812820
case dateDigitDot:
813821
// This is the 2nd period
@@ -1717,7 +1725,6 @@ iterRunes:
17171725
if r == '=' && datestr[i-1] == 'm' {
17181726
p.extra = i - 2
17191727
p.trimExtra()
1720-
break
17211728
}
17221729

17231730
case timePeriodWsOffsetColon:
@@ -2124,7 +2131,6 @@ type parser struct {
21242131
msi int
21252132
mslen int
21262133
offseti int
2127-
offsetlen int
21282134
tzi int
21292135
tzlen int
21302136
t *time.Time

parseany_test.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ func TestParse(t *testing.T) {
187187
{in: "8/8/71", out: "1971-08-08 00:00:00 +0000 UTC"},
188188
// mm/dd/yy hh:mm:ss
189189
{in: "04/02/2014 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
190+
{in: "04/02/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
190191
{in: "4/2/2014 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
192+
{in: "4/2/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
191193
{in: "04/02/2014 4:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
192194
{in: "04/02/2014 4:8:9", out: "2014-04-02 04:08:09 +0000 UTC"},
193195
{in: "04/02/2014 04:08", out: "2014-04-02 04:08:00 +0000 UTC"},
@@ -209,7 +211,9 @@ func TestParse(t *testing.T) {
209211
{in: "04/02/2014 04:08:09 AM", out: "2014-04-02 04:08:09 +0000 UTC"},
210212
{in: "04/02/2014 04:08:09 PM", out: "2014-04-02 16:08:09 +0000 UTC"},
211213
{in: "04/02/2014 04:08 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
214+
{in: "04/02/2014, 04:08 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
212215
{in: "04/02/2014 04:08 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
216+
{in: "04/02/2014, 04:08 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
213217
{in: "04/02/2014 4:8 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
214218
{in: "04/02/2014 4:8 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
215219
{in: "04/02/2014 04:08:09.123 AM", out: "2014-04-02 04:08:09.123 +0000 UTC"},
@@ -555,27 +559,29 @@ func TestPStruct(t *testing.T) {
555559
assert.True(t, len(p.ts()) > 0)
556560
}
557561

558-
var testParseErrors = []dateTest{
559-
{in: "3", err: true},
560-
{in: `{"hello"}`, err: true},
561-
{in: "2009-15-12T22:15Z", err: true},
562-
{in: "5,000-9,999", err: true},
563-
{in: "xyzq-baad"},
564-
{in: "oct.-7-1970", err: true},
565-
{in: "septe. 7, 1970", err: true},
566-
{in: "SeptemberRR 7th, 1970", err: true},
567-
{in: "29-06-2016", err: true},
568-
// this is just testing the empty space up front
569-
{in: " 2018-01-02 17:08:09 -07:00", err: true},
570-
}
571-
572562
func TestParseErrors(t *testing.T) {
563+
var testParseErrors = []dateTest{
564+
{in: "3", err: true},
565+
{in: `{"hello"}`, err: true},
566+
{in: "2009-15-12T22:15Z", err: true},
567+
{in: "5,000-9,999", err: true},
568+
{in: "xyzq-baad"},
569+
{in: "oct.-7-1970", err: true},
570+
{in: "septe. 7, 1970", err: true},
571+
{in: "SeptemberRR 7th, 1970", err: true},
572+
//{in: "29-06-2016", err: true},
573+
// this is just testing the empty space up front
574+
{in: " 2018-01-02 17:08:09 -07:00", err: true},
575+
}
576+
573577
for _, th := range testParseErrors {
574-
v, err := ParseAny(th.in)
575-
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)
578+
t.Run(th.in, func(t *testing.T) {
579+
v, err := ParseAny(th.in)
580+
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)
576581

577-
v, err = ParseAny(th.in, RetryAmbiguousDateWithSwap(true))
578-
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)
582+
v, err = ParseAny(th.in, RetryAmbiguousDateWithSwap(true))
583+
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)
584+
})
579585
}
580586
}
581587

0 commit comments

Comments
 (0)