From 5025a49c028c01c544805ec21e37330cb93ffef8 Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Wed, 23 May 2018 21:11:15 -0700 Subject: [PATCH] new format april 3, 2018 closes #59 --- README.md | 2 ++ example/main.go | 2 ++ parseany.go | 62 +++++++++++++++++++++++++++++++++++++++++------- parseany_test.go | 9 ++++--- 4 files changed, 64 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b1d4ed9..296617a 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ var examples = []string{ "Mon Aug 10 15:44:11 UTC+0100 2015", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", "September 17, 2012 at 10:09am PST-08", + "October 7, 1970", "12 Feb 2006, 19:17", "12 Feb 2006 19:17", "7 oct 70", @@ -207,6 +208,7 @@ func main() { | Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC | | Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT | | September 17, 2012 at 10:09am PST-08 | 2012-09-17 10:09:00 -0800 PST | +| October 7, 1970 | 1970-10-07 00:00:00 +0000 UTC | | 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC | | 12 Feb 2006 19:17 | 2006-02-12 19:17:00 +0000 UTC | | 7 oct 70 | 1970-10-07 00:00:00 +0000 UTC | diff --git a/example/main.go b/example/main.go index c4e374f..6ce8592 100644 --- a/example/main.go +++ b/example/main.go @@ -24,6 +24,7 @@ var examples = []string{ "Mon Aug 10 15:44:11 UTC+0100 2015", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", "September 17, 2012 at 10:09am PST-08", + "October 7, 1970", "12 Feb 2006, 19:17", "12 Feb 2006 19:17", "7 oct 70", @@ -146,6 +147,7 @@ func main() { | Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC | | Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT | | September 17, 2012 at 10:09am PST-08 | 2012-09-17 10:09:00 -0800 PST | +| October 7, 1970 | 1970-10-07 00:00:00 +0000 UTC | | 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC | | 12 Feb 2006 19:17 | 2006-02-12 19:17:00 +0000 UTC | | 7 oct 70 | 1970-10-07 00:00:00 +0000 UTC | diff --git a/parseany.go b/parseany.go index 05e4234..65f3e12 100644 --- a/parseany.go +++ b/parseany.go @@ -6,6 +6,7 @@ package dateparse import ( "fmt" "strconv" + "strings" "time" "unicode" "unicode/utf8" @@ -16,6 +17,21 @@ import ( // gou.SetColorOutput() // } +var months = []string{ + "january", + "february", + "march", + "april", + "may", + "june", + "july", + "august", + "september", + "october", + "november", + "december", +} + type dateState uint8 type timeState uint8 @@ -41,6 +57,7 @@ const ( dateAlphaWsDigitComma dateAlphaWsDigitCommaWs dateAlphaWsDigitCommaWsYear + dateAlphaWsMonth dateAlphaWsAlpha dateAlphaWsAlphaYearmaybe dateWeekdayComma @@ -628,7 +645,10 @@ iterRunes: // dateAlphaWSDigit // May 8, 2009 5:57:51 PM // oct 1, 1970 - // + // dateAlphaWsMonth + // April 8, 2009 + // dateAlphaWsMonthTime + // January 02, 2006 at 3:04pm MST-07 // dateWeekdayComma // Monday, 02 Jan 2006 15:04:05 MST // Monday, 02-Jan-06 15:04:05 MST @@ -643,14 +663,30 @@ iterRunes: switch { case r == ' ': if i > 4 { - // September 17, 2012 at 5:00pm UTC-05 - // This one doesn't follow standard parse methodologies. the "January" - // is difficult to use the format string replace method because of its variable-length (march, june) - // so we just use this format here. If we see more similar to this we will do something else. - p.format = []byte("January 02, 2006 at 3:04pm MST-07") - return p, nil + prefix := strings.ToLower(datestr[0:i]) + for _, month := range months { + if prefix == month { + // len(" 31, 2018") = 9 + if len(datestr[i:]) < 10 { + // April 8, 2009 + p.dayi = i + 1 + p.stateDate = dateAlphaWsMonth + break + } + } + } + if p.stateDate != dateAlphaWsMonth { + // September 17, 2012 at 5:00pm UTC-05 + // This one doesn't follow standard parse methodologies. the "January" + // is difficult to use the format string replace method because of its variable-length (march, june) + // so we just use this format here. If we see more similar to this we will do something else. + p.format = []byte("January 02, 2006 at 3:04pm MST-07") + return p, nil + } + } else { + p.stateDate = dateAlphaWs } - p.stateDate = dateAlphaWs + case r == ',': // p.moi = 0 // p.molen = i @@ -667,6 +703,16 @@ iterRunes: // just lay down the skip, no need to fill and then skip } } + case dateAlphaWsMonth: + // April 8, 2009 + if r == ',' { + if i-p.dayi == 2 { + p.format = []byte("January 02, 2006") + return p, nil + } + p.format = []byte("January 2, 2006") + return p, nil + } case dateWeekdayComma: // Monday, 02 Jan 2006 15:04:05 MST // Monday, 02 Jan 2006 15:04:05 -0700 diff --git a/parseany_test.go b/parseany_test.go index 71b791c..6ae4410 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -13,8 +13,8 @@ func TestOne(t *testing.T) { var ts time.Time // {in: "2015-02-08 03:02:00 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00 +0000 UTC"}, // {in: "2015-02-08 03:02:00.001 +0300 MSK m=+0.000000001", out: "2015-02-08 00:02:00.001 +0000 UTC"}, - ts = MustParse("2018.05") - assert.Equal(t, "2018-05-01 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("April 3, 2018") + assert.Equal(t, "2018-04-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) } type dateTest struct { @@ -58,9 +58,12 @@ var testInputs = []dateTest{ {in: "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", out: "2015-07-03 17:04:07 +0000 UTC"}, {in: "Fri Jul 3 2015 06:04:07 GMT+0100 (GMT Daylight Time)", out: "2015-07-03 05:04:07 +0000 UTC"}, {in: "Fri Jul 3 2015 06:04:07 PST-0700 (Pacific Daylight Time)", out: "2015-07-03 13:04:07 +0000 UTC"}, - // Month dd, yyyy + // Month dd, yyyy time {in: "September 17, 2012 at 5:00pm UTC-05", out: "2012-09-17 17:00:00 +0000 UTC"}, {in: "September 17, 2012 at 10:09am PST-08", out: "2012-09-17 18:09:00 +0000 UTC"}, + // Monty dd, yyyy + {in: "September 17, 2012", out: "2012-09-17 00:00:00 +0000 UTC"}, + {in: "May 7, 2012", out: "2012-05-07 00:00:00 +0000 UTC"}, // ? {in: "Fri, 03 Jul 2015 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"}, {in: "Fri, 03 Jul 2015 08:08:08 PST", out: "2015-07-03 15:08:08 +0000 UTC", loc: "America/Los_Angeles"},