Skip to content

Commit 3ebc8bc

Browse files
Incorporate fix for dd.mm.yyyy format
Incorporates PR araddon#133 from https://github.com/mehanizm to fix araddon#129 Adds test cases to verify the following are already fixed: * araddon#105
1 parent 1b1e0b3 commit 3ebc8bc

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

parseany.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,24 @@ iterRunes:
396396
}
397397
} else {
398398
p.ambiguousMD = true
399-
p.moi = 0
400-
p.molen = i
401-
if !p.setMonth() {
402-
return p, unknownErr(datestr)
399+
if p.preferMonthFirst {
400+
if p.molen == 0 {
401+
// 03.31.2005
402+
p.molen = i
403+
if !p.setMonth() {
404+
return p, unknownErr(datestr)
405+
}
406+
p.dayi = i + 1
407+
}
408+
} else {
409+
if p.daylen == 0 {
410+
p.daylen = i
411+
if !p.setDay() {
412+
return p, unknownErr(datestr)
413+
}
414+
p.moi = i + 1
415+
}
403416
}
404-
p.dayi = i + 1
405417
}
406418

407419
case ' ':
@@ -799,9 +811,17 @@ iterRunes:
799811
return p, unknownErr(datestr)
800812
}
801813
p.stateDate = dateDigitDotDot
814+
} else if p.dayi == 0 && p.yearlen == 0 {
815+
// 23.07.2002
816+
p.molen = i - p.moi
817+
p.yeari = i + 1
818+
if !p.setMonth() {
819+
return p, unknownErr(datestr)
820+
}
821+
p.stateDate = dateDigitDotDot
802822
} else {
803823
// 2018.09.30
804-
//p.molen = 2
824+
// p.molen = 2
805825
p.molen = i - p.moi
806826
p.dayi = i + 1
807827
if !p.setMonth() {
@@ -2267,20 +2287,20 @@ func (p *parser) coalesceTime(end int) {
22672287
}
22682288
}
22692289
func (p *parser) setFullMonth(month string) {
2270-
oldLen := len(p.format)
2271-
const fullMonth = "January"
2290+
oldLen := len(p.format)
2291+
const fullMonth = "January"
22722292
p.format = []byte(fmt.Sprintf("%s%s%s", p.format[0:p.moi], fullMonth, p.format[p.moi+len(month):]))
2273-
newLen := len(p.format)
2293+
newLen := len(p.format)
22742294
if newLen > oldLen && p.formatSetLen >= p.moi {
2275-
p.formatSetLen += newLen - oldLen
2295+
p.formatSetLen += newLen - oldLen
22762296
} else if newLen < oldLen && p.formatSetLen >= p.moi {
22772297
p.formatSetLen -= oldLen - newLen
2278-
}
2298+
}
22792299

2280-
if p.formatSetLen > len(p.format) {
2281-
p.formatSetLen = len(p.format)
2282-
} else if p.formatSetLen < len(fullMonth) {
2283-
p.formatSetLen = len(fullMonth)
2300+
if p.formatSetLen > len(p.format) {
2301+
p.formatSetLen = len(p.format)
2302+
} else if p.formatSetLen < len(fullMonth) {
2303+
p.formatSetLen = len(fullMonth)
22842304
} else if p.formatSetLen < 0 {
22852305
p.formatSetLen = 0
22862306
}

parseany_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func TestOne(t *testing.T) {
1717
type dateTest struct {
1818
in, out, loc, zname string
1919
err bool
20+
preferDayFirst bool
21+
retryAmbiguous bool
2022
}
2123

2224
var testInputs = []dateTest{
@@ -525,6 +527,32 @@ var testInputs = []dateTest{
525527
{in: "03.31.2014", out: "2014-03-31 00:00:00 +0000 UTC"},
526528
// mm.dd.yy
527529
{in: "08.21.71", out: "1971-08-21 00:00:00 +0000 UTC"},
530+
// dd.mm.yyyy (see https://github.com/araddon/dateparse/issues/129 and https://github.com/araddon/dateparse/issues/28 and https://github.com/araddon/dateparse/pull/133)
531+
{in: "23.07.1938", out: "1938-07-23 00:00:00 +0000 UTC", retryAmbiguous: true},
532+
{in: "23.07.1938", out: "1938-07-23 00:00:00 +0000 UTC", preferDayFirst: true},
533+
{in: "23/07/1938", out: "1938-07-23 00:00:00 +0000 UTC", retryAmbiguous: true},
534+
{in: "23/07/1938", out: "1938-07-23 00:00:00 +0000 UTC", preferDayFirst: true},
535+
{in: "31/3/2014", out: "2014-03-31 00:00:00 +0000 UTC", retryAmbiguous: true},
536+
{in: "31/3/2014", out: "2014-03-31 00:00:00 +0000 UTC", preferDayFirst: true},
537+
{in: "31/03/2014", out: "2014-03-31 00:00:00 +0000 UTC", retryAmbiguous: true},
538+
{in: "31/03/2014", out: "2014-03-31 00:00:00 +0000 UTC", preferDayFirst: true},
539+
{in: "21/08/71", out: "1971-08-21 00:00:00 +0000 UTC", retryAmbiguous: true},
540+
{in: "21/08/71", out: "1971-08-21 00:00:00 +0000 UTC", preferDayFirst: true},
541+
{in: "1/8/71", out: "1971-01-08 00:00:00 +0000 UTC", preferDayFirst: false},
542+
{in: "1/8/71", out: "1971-08-01 00:00:00 +0000 UTC", preferDayFirst: true},
543+
{in: "8/4/2014 22:05", out: "2014-08-04 22:05:00 +0000 UTC", preferDayFirst: false},
544+
{in: "8/4/2014 22:05", out: "2014-04-08 22:05:00 +0000 UTC", preferDayFirst: true},
545+
{in: "08/04/2014 22:05", out: "2014-08-04 22:05:00 +0000 UTC", preferDayFirst: false},
546+
{in: "08/04/2014 22:05", out: "2014-04-08 22:05:00 +0000 UTC", preferDayFirst: true},
547+
{in: "2/04/2014 03:00:51", out: "2014-02-04 03:00:51 +0000 UTC", preferDayFirst: false},
548+
{in: "2/04/2014 03:00:51", out: "2014-04-02 03:00:51 +0000 UTC", preferDayFirst: true},
549+
{in: "19/03/2012 10:11:59", out: "2012-03-19 10:11:59 +0000 UTC", retryAmbiguous: true},
550+
{in: "19/03/2012 10:11:59", out: "2012-03-19 10:11:59 +0000 UTC", preferDayFirst: true},
551+
{in: "19/03/2012 10:11:59.3186369", out: "2012-03-19 10:11:59.3186369 +0000 UTC", retryAmbiguous: true},
552+
{in: "19/03/2012 10:11:59.3186369", out: "2012-03-19 10:11:59.3186369 +0000 UTC", preferDayFirst: true},
553+
// https://github.com/araddon/dateparse/issues/105
554+
{in: "20/5/2006 19:51:45", out: "2006-05-20 19:51:45 +0000 UTC", retryAmbiguous: true},
555+
{in: "20/5/2006 19:51:45", out: "2006-05-20 19:51:45 +0000 UTC", preferDayFirst: true},
528556
// yyyymmdd and similar
529557
{in: "2014", out: "2014-01-01 00:00:00 +0000 UTC"},
530558
{in: "20140601", out: "2014-06-01 00:00:00 +0000 UTC"},
@@ -573,12 +601,13 @@ func TestParse(t *testing.T) {
573601
t.Fatalf("error: %s", r)
574602
}
575603
}()
604+
parserOptions := []ParserOption{PreferMonthFirst(!th.preferDayFirst), RetryAmbiguousDateWithSwap(th.retryAmbiguous)}
576605
if len(th.loc) > 0 {
577606
loc, err := time.LoadLocation(th.loc)
578607
if err != nil {
579608
t.Fatalf("Expected to load location %q but got %v", th.loc, err)
580609
}
581-
ts, err = ParseIn(th.in, loc)
610+
ts, err = ParseIn(th.in, loc, parserOptions...)
582611
if err != nil {
583612
t.Fatalf("expected to parse %q but got %v", th.in, err)
584613
}
@@ -592,7 +621,7 @@ func TestParse(t *testing.T) {
592621
assert.Equal(t, th.zname, gotZone, "Expected zname %q but got %q from %q", th.zname, gotZone, th.in)
593622
}
594623
} else {
595-
ts = MustParse(th.in)
624+
ts = MustParse(th.in, parserOptions...)
596625
got := fmt.Sprintf("%v", ts.In(time.UTC))
597626
assert.Equal(t, th.out, got, "Expected %q but got %q from %q", th.out, got, th.in)
598627
if th.out != got {

0 commit comments

Comments
 (0)