From 30cb7ba0f929f3f340eddbfbcb22e1fda0de8309 Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Fri, 5 Feb 2021 13:44:33 -0800 Subject: [PATCH] new date format, fixes #116 --- README.md | 2 ++ example/main.go | 2 ++ parseany.go | 7 +++---- parseany_test.go | 9 ++++++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3e78308..6b4ced0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ var examples = []string{ "Mon Aug 10 15:44:11 UTC+0100 2015", "Thu, 4 Jan 2018 17:53:36 +0000", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", + "Sun, 3 Jan 2021 00:12:23 +0800 (GMT+08:00)", "September 17, 2012 10:09am", "September 17, 2012 at 10:09am PST-08", "September 17, 2012, 10:10:09", @@ -220,6 +221,7 @@ func main() { | Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC | | Thu, 4 Jan 2018 17:53:36 +0000 | 2018-01-04 17:53:36 +0000 UTC | | Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT | +| Sun, 3 Jan 2021 00:12:23 +0800 (GMT+08:00) | 2021-01-03 00:12:23 +0800 +0800 | | September 17, 2012 10:09am | 2012-09-17 10:09:00 +0000 UTC | | September 17, 2012 at 10:09am PST-08 | 2012-09-17 10:09:00 -0800 PST | | September 17, 2012, 10:10:09 | 2012-09-17 10:10:09 +0000 UTC | diff --git a/example/main.go b/example/main.go index 301593d..f7b572f 100644 --- a/example/main.go +++ b/example/main.go @@ -26,6 +26,7 @@ var examples = []string{ "Mon Aug 10 15:44:11 UTC+0100 2015", "Thu, 4 Jan 2018 17:53:36 +0000", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", + "Sun, 3 Jan 2021 00:12:23 +0800 (GMT+08:00)", "September 17, 2012 10:09am", "September 17, 2012 at 10:09am PST-08", "September 17, 2012, 10:10:09", @@ -177,6 +178,7 @@ func main() { | Mon Aug 10 15:44:11 UTC+0100 2015 | 2015-08-10 15:44:11 +0000 UTC | | Thu, 4 Jan 2018 17:53:36 +0000 | 2018-01-04 17:53:36 +0000 UTC | | Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time) | 2015-07-03 18:04:07 +0100 GMT | +| Sun, 3 Jan 2021 00:12:23 +0800 (GMT+08:00) | 2021-01-03 00:12:23 +0800 +0800 | | September 17, 2012 10:09am | 2012-09-17 10:09:00 +0000 UTC | | September 17, 2012 at 10:09am PST-08 | 2012-09-17 10:09:00 -0800 PST | | September 17, 2012, 10:10:09 | 2012-09-17 10:10:09 +0000 UTC | diff --git a/parseany.go b/parseany.go index 185e6e5..b2f9ac0 100644 --- a/parseany.go +++ b/parseany.go @@ -785,8 +785,7 @@ iterRunes: case r == ',': // Mon, 02 Jan 2006 - // p.moi = 0 - // p.molen = i + if i == 3 { p.stateDate = dateWeekdayAbbrevComma p.set(0, "Mon") @@ -1106,7 +1105,7 @@ iterRunes: for ; i < len(datestr); i++ { r := rune(datestr[i]) - //gou.Debugf("%d %s %d iterTimeRunes %s %s", i, string(r), p.stateTime, p.ds(), p.ts()) + // gou.Debugf("%d %s %d iterTimeRunes %s %s", i, string(r), p.stateTime, p.ds(), p.ts()) switch p.stateTime { case timeStart: @@ -1400,7 +1399,7 @@ iterRunes: p.trimExtra() break } - case '+', '-': + case '+', '-', '(': // This really doesn't seem valid, but for some reason when round-tripping a go date // their is an extra +03 printed out. seems like go bug to me, but, parsing anyway. // 00:00:00 +0300 +03 diff --git a/parseany_test.go b/parseany_test.go index 2b9e61d..1aa67ee 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -11,8 +11,8 @@ import ( func TestOne(t *testing.T) { time.Local = time.UTC var ts time.Time - ts = MustParse("06/May/2008 15:04:05 -0700") - assert.Equal(t, "2008-05-06 22:04:05 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("Sun, 3 Jan 2021 00:12:23 +0800 (GMT+08:00)") + assert.Equal(t, "2021-01-02 16:12:23 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) } type dateTest struct { @@ -131,6 +131,8 @@ var testInputs = []dateTest{ {in: "Fri, 3-Jul-15 08:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"}, {in: "Fri, 03-Jul-15 8:08:08 MST", out: "2015-07-03 08:08:08 +0000 UTC"}, {in: "Fri, 03-Jul-15 8:8:8 MST", out: "2015-07-03 08:08:08 +0000 UTC"}, + // day, dd-Mon-yy hh:mm:zz TZ (text) https://github.com/araddon/dateparse/issues/116 + {in: "Sun, 3 Jan 2021 00:12:23 +0800 (GMT+08:00)", out: "2021-01-02 16:12:23 +0000 UTC"}, // RFC850 = "Monday, 02-Jan-06 15:04:05 MST" {in: "Wednesday, 07-May-09 08:00:43 MST", out: "2009-05-07 08:00:43 +0000 UTC"}, {in: "Wednesday, 28-Feb-18 09:01:00 MST", out: "2018-02-28 09:01:00 +0000 UTC"}, @@ -222,7 +224,8 @@ var testInputs = []dateTest{ {in: "2014/4/2 04:08:09 AM", out: "2014-04-02 04:08:09 +0000 UTC"}, {in: "2014/04/02 04:08:09.123 AM", out: "2014-04-02 04:08:09.123 +0000 UTC"}, {in: "2014/04/02 04:08:09.123 PM", out: "2014-04-02 16:08:09.123 +0000 UTC"}, - // dd/mon/yyyy:hh:mm:ss tz redis log? + // dd/mon/yyyy:hh:mm:ss tz nginx-log? https://github.com/araddon/dateparse/issues/118 + // 112.195.209.90 - - [20/Feb/2018:12:12:14 +0800] "GET / HTTP/1.1" 200 190 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36" "-" {in: "06/May/2008:08:11:17 -0700", out: "2008-05-06 15:11:17 +0000 UTC"}, {in: "30/May/2008:08:11:17 -0700", out: "2008-05-30 15:11:17 +0000 UTC"}, // dd/mon/yyy hh:mm:ss tz