Skip to content

Commit 23869f3

Browse files
Add support for mm/dd/yyyy, hh:mm:ss
Incorporate PR araddon#156 from https://github.com/BrianLeishman and adapt to also validate the format
1 parent 14fb939 commit 23869f3

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

parseany.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,14 @@ func parseTime(datestr string, loc *time.Location, opts ...ParserOption) (p *par
254254
if p != nil && p.ambiguousMD {
255255
// if it errors out with the following error, swap before we
256256
// get out of this function to reduce scope it needs to be applied on
257-
_, err := p.parse()
257+
_, err = p.parse()
258258
if err != nil && strings.Contains(err.Error(), "month out of range") {
259259
// create the option to reverse the preference
260260
preferMonthFirst := PreferMonthFirst(!p.preferMonthFirst)
261261
// turn off the retry to avoid endless recursion
262262
retryAmbiguousDateWithSwap := RetryAmbiguousDateWithSwap(false)
263263
modifiedOpts := append(opts, preferMonthFirst, retryAmbiguousDateWithSwap)
264-
p, _ = parseTime(datestr, time.Local, modifiedOpts...)
264+
p, err = parseTime(datestr, time.Local, modifiedOpts...)
265265
}
266266
}
267267

@@ -684,6 +684,7 @@ iterRunes:
684684
case dateDigitSlash:
685685
// 03/19/2012 10:11:59
686686
// 04/2/2014 03:00:37
687+
// 04/2/2014, 03:00:37
687688
// 3/1/2012 10:11:59
688689
// 4/8/2014 22:05
689690
// 3/1/2014
@@ -713,10 +714,14 @@ iterRunes:
713714
}
714715
// Note no break, we are going to pass by and re-enter this dateDigitSlash
715716
// and look for ending (space) or not (just date)
716-
case ' ':
717+
case ' ', ',':
717718
p.stateTime = timeStart
718719
if p.yearlen == 0 {
719720
p.yearlen = i - p.yeari
721+
if r == ',' {
722+
// skip the comma
723+
i++
724+
}
720725
if !p.setYear() {
721726
return p, unknownErr(datestr)
722727
}

parseany_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
)
1010

1111
func TestOne(t *testing.T) {
12-
time.Local = time.UTC
13-
var ts = MustParse("2020-07-20+08:00")
12+
ts := MustParse("2020-07-20+08:00")
1413
assert.Equal(t, "2020-07-19 16:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
1514
}
1615

@@ -431,6 +430,11 @@ var testInputs = []dateTest{
431430
{in: "Jul 9, 2012 at 5:02am (EST)", out: "2012-07-09 05:02:00 +0000 UTC", zname: "EST"},
432431
{in: "Jul 9, 2012 at 5:02am (EST)", out: "2012-07-09 05:02:00 +0000 UTC", loc: "US/Pacific", zname: "EST"},
433432
{in: "Jul 9, 2012 at 5:02am (EST)", out: "2012-07-09 10:02:00 +0000 UTC", loc: "America/New_York", zname: "EDT"},
433+
// https://github.com/araddon/dateparse/pull/156
434+
{in: "04/02/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
435+
{in: "4/2/2014, 04:08:09", out: "2014-04-02 04:08:09 +0000 UTC"},
436+
{in: "04/02/2014, 04:08 AM", out: "2014-04-02 04:08:00 +0000 UTC"},
437+
{in: "04/02/2014, 04:08 PM", out: "2014-04-02 16:08:00 +0000 UTC"},
434438
// yyyy-mm-dd hh:mm:ss,000
435439
{in: "2014-05-11 08:20:13,787", out: "2014-05-11 08:20:13.787 +0000 UTC"},
436440
// yyyy-mm-dd hh:mm:ss +0000

0 commit comments

Comments
 (0)