Skip to content

Commit 5b4d7c9

Browse files
mehanizmdron22
authored andcommitted
fix problem with date format 02.01.2006
This is a commit from https://github.com/mehanizm/dateparse/tree/fix_02.01.2006 which attempts to fix the limitation with day first date formats. It was not yet merged into the original repo (araddon#133)
1 parent 6b43995 commit 5b4d7c9

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

parseany.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,20 @@ iterRunes:
374374
p.setYear()
375375
} else {
376376
p.ambiguousMD = true
377-
p.moi = 0
378-
p.molen = i
379-
p.setMonth()
380-
p.dayi = i + 1
377+
if p.preferMonthFirst {
378+
if p.molen == 0 {
379+
// 03.31.2005
380+
p.molen = i
381+
p.setMonth()
382+
p.dayi = i + 1
383+
}
384+
} else {
385+
if p.daylen == 0 {
386+
p.daylen = i
387+
p.setDay()
388+
p.moi = i + 1
389+
}
390+
}
381391
}
382392

383393
case ' ':
@@ -726,9 +736,15 @@ iterRunes:
726736
p.yeari = i + 1
727737
p.setDay()
728738
p.stateDate = dateDigitDotDot
739+
} else if p.dayi == 0 && p.yearlen == 0 {
740+
// 23.07.2002
741+
p.molen = i - p.moi
742+
p.yeari = i + 1
743+
p.setMonth()
744+
p.stateDate = dateDigitDotDot
729745
} else {
730746
// 2018.09.30
731-
//p.molen = 2
747+
// p.molen = 2
732748
p.molen = i - p.moi
733749
p.dayi = i + 1
734750
p.setMonth()

parseany_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ var testInputs = []dateTest{
402402
{in: "03.31.2014", out: "2014-03-31 00:00:00 +0000 UTC"},
403403
// mm.dd.yy
404404
{in: "08.21.71", out: "1971-08-21 00:00:00 +0000 UTC"},
405+
// dd.mm.yyyy
406+
{in: "23.07.1938", out: "1938-07-23 00:00:00 +0000 UTC"},
407+
{in: "23/07/1938", out: "1938-07-23 00:00:00 +0000 UTC"},
405408
// yyyymmdd and similar
406409
{in: "2014", out: "2014-01-01 00:00:00 +0000 UTC"},
407410
{in: "20140601", out: "2014-06-01 00:00:00 +0000 UTC"},
@@ -450,7 +453,7 @@ func TestParse(t *testing.T) {
450453
panic("whoops")
451454
}
452455
} else {
453-
ts = MustParse(th.in)
456+
ts = MustParse(th.in, RetryAmbiguousDateWithSwap(true))
454457
got := fmt.Sprintf("%v", ts.In(time.UTC))
455458
assert.Equal(t, th.out, got, "Expected %q but got %q from %q", th.out, got, th.in)
456459
if th.out != got {

0 commit comments

Comments
 (0)