Skip to content

Commit a6e0f63

Browse files
committed
New date format 02.01.2006
Ref: araddon/pull/133
1 parent 82197ca commit a6e0f63

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

parseany.go

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

381391
case ' ':
@@ -730,9 +740,15 @@ iterRunes:
730740
p.yeari = i + 1
731741
p.setDay()
732742
p.stateDate = dateDigitDotDot
743+
} else if p.dayi == 0 && p.yearlen == 0 {
744+
// 23.07.2002
745+
p.molen = i - p.moi
746+
p.yeari = i + 1
747+
p.setMonth()
748+
p.stateDate = dateDigitDotDot
733749
} else {
734750
// 2018.09.30
735-
//p.molen = 2
751+
// p.molen = 2
736752
p.molen = i - p.moi
737753
p.dayi = i + 1
738754
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"},
@@ -458,7 +461,7 @@ func TestParse(t *testing.T) {
458461
panic("whoops")
459462
}
460463
} else {
461-
ts = MustParse(th.in)
464+
ts = MustParse(th.in, RetryAmbiguousDateWithSwap(true))
462465
got := fmt.Sprintf("%v", ts.In(time.UTC))
463466
assert.Equal(t, th.out, got, "Expected %q but got %q from %q", th.out, got, th.in)
464467
if th.out != got {

0 commit comments

Comments
 (0)