From ebfdb9cd62fbac9617c1b376d75734ea7eb3a5f4 Mon Sep 17 00:00:00 2001 From: Daniel Ferstay Date: Tue, 9 Nov 2021 20:58:30 -0800 Subject: [PATCH] Add support for dates of the form "yyyy mmm dd" where mmm is an alpha literal Fixes #141 Signed-off-by: Daniel Ferstay --- parseany.go | 36 +++++++++++++++++++++++++++++++++++- parseany_test.go | 2 ++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/parseany.go b/parseany.go index b9668b2..568328e 100644 --- a/parseany.go +++ b/parseany.go @@ -93,6 +93,8 @@ const ( dateAlphaPeriodWsDigit dateWeekdayComma dateWeekdayAbbrevComma + dateYearWs + dateYearWsMonthWs ) const ( // Time state @@ -388,7 +390,13 @@ iterRunes: // 02 Jan 2018 23:59:34 // 12 Feb 2006, 19:17 // 12 Feb 2006, 19:17:22 - if i == 6 { + // 2013 Jan 06 15:04:05 + if i == 4 { + p.yearlen = i + p.moi = i + 1 + p.setYear() + p.stateDate = dateYearWs + } else if i == 6 { p.stateDate = dateDigitSt } else { p.stateDate = dateDigitWs @@ -704,6 +712,30 @@ iterRunes: // 18 January 2018 // 8 January 2018 + case dateYearWs: + // 2013 Jan 06 15:04:05 + if r == ' ' { + p.molen = i - p.moi + p.set(p.moi, "Jan") + p.dayi = i + 1 + p.stateDate = dateYearWsMonthWs + } + case dateYearWsMonthWs: + // 2013 Jan 06 15:04:05 + switch r { + case ',': + p.daylen = i - p.dayi + p.setDay() + i++ + p.stateTime = timeStart + break iterRunes + case ' ': + p.daylen = i - p.dayi + p.setDay() + p.stateTime = timeStart + break iterRunes + } + case dateDigitChineseYear: // dateDigitChineseYear // 2014年04月08日 @@ -1949,6 +1981,8 @@ iterRunes: // Mon, 02 Jan 2006 15:04:05 MST return p, nil + case dateYearWsMonthWs: + return p, nil } return nil, unknownErr(datestr) diff --git a/parseany_test.go b/parseany_test.go index 7fea1e6..e8ea12e 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -154,6 +154,8 @@ var testInputs = []dateTest{ {in: "07 Feb 2004 09:07", out: "2004-02-07 09:07:00 +0000 UTC"}, {in: "7 Feb 2004 9:7:8", out: "2004-02-07 09:07:08 +0000 UTC"}, {in: "07 Feb 2004 09:07:08.123", out: "2004-02-07 09:07:08.123 +0000 UTC"}, + /// yyyy mmm dd https://github.com/araddon/dateparse/issues/141 + {in: "2013 May 02 11:37:55", out: "2013-05-02 11:37:55 +0000 UTC"}, // dd-mon-yyyy 12 Feb 2006, 19:17:08 GMT {in: "07 Feb 2004, 09:07:07 GMT", out: "2004-02-07 09:07:07 +0000 UTC"}, // dd-mon-yyyy 12 Feb 2006, 19:17:08 +0100