From 18938f16ae75df76755cb92fded6eaa826ea70fa Mon Sep 17 00:00:00 2001 From: Klondike Dragon Date: Tue, 12 Dec 2023 21:42:48 -0700 Subject: [PATCH] Implement support for yyyy mon dd (2013 May 02) Incorporate PR https://github.com/araddon/dateparse/pull/142 from https://github.com/dferstay to fix https://github.com/araddon/dateparse/issues/141 --- parseany.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++- parseany_test.go | 4 ++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/parseany.go b/parseany.go index 582e58a..717c35a 100644 --- a/parseany.go +++ b/parseany.go @@ -95,6 +95,8 @@ const ( dateAlphaSlashDigitSlash dateWeekdayComma dateWeekdayAbbrevComma + dateYearWs + dateYearWsMonthWs ) const ( // Time state @@ -426,7 +428,15 @@ 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 + if !p.setYear() { + return p, unknownErr(datestr) + } + p.stateDate = dateYearWs + } else if i == 6 { p.stateDate = dateDigitSt } else { p.stateDate = dateDigitWs @@ -795,6 +805,45 @@ iterRunes: } break iterRunes } + + case dateYearWs: + // 2013 Jan 06 15:04:05 + // 2013 January 06 15:04:05 + if r == ' ' { + p.molen = i - p.moi + // Must be a valid short or long month + if p.molen == 3 { + p.set(p.moi, "Jan") + p.dayi = i + 1 + p.stateDate = dateYearWsMonthWs + } else { + possibleFullMonth := strings.ToLower(p.datestr[p.moi:(p.moi + p.molen)]) + if i > 3 && isMonthFull(possibleFullMonth) { + p.fullMonth = possibleFullMonth + p.dayi = i + 1 + p.stateDate = dateYearWsMonthWs + } else { + return p, unknownErr(datestr) + } + } + } + case dateYearWsMonthWs: + // 2013 Jan 06 15:04:05 + // 2013 January 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日 @@ -2142,6 +2191,11 @@ iterRunes: // Mon, 02 Jan 2006 15:04:05 MST return p, nil + case dateYearWsMonthWs: + // 2013 May 02 11:37:55 + // 2013 December 02 11:37:55 + return p, nil + } return p, unknownErr(datestr) diff --git a/parseany_test.go b/parseany_test.go index 190e2c1..0a545f9 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -404,6 +404,10 @@ var testInputs = []dateTest{ {in: "2018-09-30 21:09:13.123 AMT", out: "2018-09-30 21:09:13.123 +0000 UTC", zname: "AMT"}, {in: "2018-09-30 08:09:13.123 am AMT", out: "2018-09-30 08:09:13.123 +0000 UTC", zname: "AMT"}, {in: "2018-09-30 08:09:13.123am AMT", out: "2018-09-30 08:09:13.123 +0000 UTC", zname: "AMT"}, + /// 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"}, + {in: "2013 June 02 11:37:55", out: "2013-06-02 11:37:55 +0000 UTC"}, + {in: "2013 December 02 11:37:55", out: "2013-12-02 11:37:55 +0000 UTC"}, // yyyy-mm-dd hh:mm:ss,000 {in: "2014-05-11 08:20:13,787", out: "2014-05-11 08:20:13.787 +0000 UTC"}, // yyyy-mm-dd hh:mm:ss +0000