@@ -17,6 +17,8 @@ func TestOne(t *testing.T) {
1717type dateTest struct {
1818 in , out , loc , zname string
1919 err bool
20+ preferDayFirst bool
21+ retryAmbiguous bool
2022}
2123
2224var 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