Skip to content

Commit

Permalink
Add support for comma format like "04/2/2014, 03:00:37"
Browse files Browse the repository at this point in the history
  • Loading branch information
iTanken committed Apr 1, 2024
1 parent 00672cf commit bfdab07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
12 changes: 9 additions & 3 deletions parseany.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ iterRunes:
case dateDigitSlash:
// 03/19/2012 10:11:59
// 04/2/2014 03:00:37
// 04/2/2014, 03:00:37
// 3/1/2012 10:11:59
// 4/8/2014 22:05
// 3/1/2014
Expand Down Expand Up @@ -682,6 +683,14 @@ iterRunes:
p.setYear()
}
break iterRunes
case ',':
p.stateTime = timeStart
if p.yearlen == 0 {
p.yearlen = i - p.yeari
i++
p.setYear()
}
break iterRunes
}

case dateDigitColon:
Expand Down Expand Up @@ -807,7 +816,6 @@ iterRunes:
// 2013年07月18日 星期四 10:27 上午
if r == ' ' {
p.stateDate = dateDigitChineseYearWs
break
}
case dateDigitDot:
// This is the 2nd period
Expand Down Expand Up @@ -1717,7 +1725,6 @@ iterRunes:
if r == '=' && datestr[i-1] == 'm' {
p.extra = i - 2
p.trimExtra()
break
}

case timePeriodWsOffsetColon:
Expand Down Expand Up @@ -2124,7 +2131,6 @@ type parser struct {
msi int
mslen int
offseti int
offsetlen int
tzi int
tzlen int
t *time.Time
Expand Down
42 changes: 24 additions & 18 deletions parseany_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ func TestParse(t *testing.T) {
{in: "8/8/71", out: "1971-08-08 00:00:00 +0000 UTC"},
// mm/dd/yy hh:mm:ss
{in: "04/02/2014 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04/02/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "4/2/2014 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "4/2/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04/02/2014 4:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04/02/2014 4:8:9", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04/02/2014 04:08", out: "2014-04-02 04:08:00 +0000 UTC"},
Expand All @@ -209,7 +211,9 @@ func TestParse(t *testing.T) {
{in: "04/02/2014 04:08:09 AM", out: "2014-04-02 04:08:09 +0000 UTC"},
{in: "04/02/2014 04:08:09 PM", out: "2014-04-02 16:08:09 +0000 UTC"},
{in: "04/02/2014 04:08 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
{in: "04/02/2014, 04:08 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
{in: "04/02/2014 04:08 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
{in: "04/02/2014, 04:08 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
{in: "04/02/2014 4:8 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
{in: "04/02/2014 4:8 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
{in: "04/02/2014 04:08:09.123 AM", out: "2014-04-02 04:08:09.123 +0000 UTC"},
Expand Down Expand Up @@ -555,27 +559,29 @@ func TestPStruct(t *testing.T) {
assert.True(t, len(p.ts()) > 0)
}

var testParseErrors = []dateTest{
{in: "3", err: true},
{in: `{"hello"}`, err: true},
{in: "2009-15-12T22:15Z", err: true},
{in: "5,000-9,999", err: true},
{in: "xyzq-baad"},
{in: "oct.-7-1970", err: true},
{in: "septe. 7, 1970", err: true},
{in: "SeptemberRR 7th, 1970", err: true},
{in: "29-06-2016", err: true},
// this is just testing the empty space up front
{in: " 2018-01-02 17:08:09 -07:00", err: true},
}

func TestParseErrors(t *testing.T) {
var testParseErrors = []dateTest{
{in: "3", err: true},
{in: `{"hello"}`, err: true},
{in: "2009-15-12T22:15Z", err: true},
{in: "5,000-9,999", err: true},
{in: "xyzq-baad"},
{in: "oct.-7-1970", err: true},
{in: "septe. 7, 1970", err: true},
{in: "SeptemberRR 7th, 1970", err: true},
//{in: "29-06-2016", err: true},
// this is just testing the empty space up front
{in: " 2018-01-02 17:08:09 -07:00", err: true},
}

for _, th := range testParseErrors {
v, err := ParseAny(th.in)
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)
t.Run(th.in, func(t *testing.T) {
v, err := ParseAny(th.in)
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)

v, err = ParseAny(th.in, RetryAmbiguousDateWithSwap(true))
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)
v, err = ParseAny(th.in, RetryAmbiguousDateWithSwap(true))
assert.NotEqual(t, nil, err, "%v for %v", v, th.in)
})
}
}

Expand Down

0 comments on commit bfdab07

Please sign in to comment.