From 7a3c9238201b1b13b41f0aca76861acc6bb93d82 Mon Sep 17 00:00:00 2001 From: Klondike Dragon Date: Mon, 18 Dec 2023 23:14:08 -0700 Subject: [PATCH] Fix mm.dd.yyyy (time) format --- parseany.go | 18 ++++++++++++++++-- parseany_test.go | 5 +++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/parseany.go b/parseany.go index 8e6b5b7..f1501bd 100644 --- a/parseany.go +++ b/parseany.go @@ -1143,12 +1143,26 @@ iterRunes: return p, p.unknownErr(datestr) } case ' ': + if p.daylen == 0 && p.molen > 0 && p.yearlen > 0 { p.daylen = i - p.dayi - p.stateDate = dateDigitDotDotWs - p.stateTime = timeStart if !p.setDay() { return p, p.unknownErr(datestr) } + } else if p.molen == 0 && p.daylen > 0 && p.yearlen > 0 { + p.molen = i - p.moi + if !p.setMonth() { + return p, p.unknownErr(datestr) + } + } else if p.yearlen == 0 && p.daylen > 0 && p.molen > 0 { + p.yearlen = i - p.yeari + if !p.setYear() { + return p, p.unknownErr(datestr) + } + } else { + return p, p.unknownErr(datestr) + } + p.stateDate = dateDigitDotDotWs + p.stateTime = timeStart break iterRunes case 'T': p.daylen = i - p.dayi diff --git a/parseany_test.go b/parseany_test.go index fa9b8d2..6284607 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -676,6 +676,7 @@ var testInputs = []dateTest{ {in: "3.31.2014", out: "2014-03-31 00:00:00 +0000 UTC"}, {in: "3.3.2014", out: "2014-03-03 00:00:00 +0000 UTC"}, {in: "03.31.2014", out: "2014-03-31 00:00:00 +0000 UTC"}, + {in: "03.31.2014 10:11:59 MST", out: "2014-03-31 10:11:59 +0000 UTC", zname: "MST"}, // mm.dd.yy {in: "08.21.71", out: "1971-08-21 00:00:00 +0000 UTC"}, // 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) @@ -1198,6 +1199,6 @@ func TestRetryAmbiguousDateWithSwap(t *testing.T) { // Convenience function for debugging a particular broken test case func TestDebug(t *testing.T) { - ts := MustParse("03:08:2012 18:31:59+00:00", PreferMonthFirst(false)) - assert.Equal(t, "2012-08-03 18:31:59 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts := MustParse("03.31.2014 10:11:59 MST-0700", PreferMonthFirst(true)) + assert.Equal(t, "2014-03-31 17:11:59 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) }