From cab5e508ca518e1d8e50dc9c84bc6fc44d0dd6b4 Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Mon, 17 Jul 2017 14:46:18 -0700 Subject: [PATCH 1/2] Another date format from salesforce --- parseany.go | 18 +++++++++++++++--- parseany_test.go | 15 ++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/parseany.go b/parseany.go index 9984d43..bbc31dd 100644 --- a/parseany.go +++ b/parseany.go @@ -35,6 +35,7 @@ const ( ST_DIGITDASHTZ ST_DIGITDASHTZDIGIT ST_DIGITDASHTDELTA + ST_DIGITDASHTDELTACOLON ST_DIGITSLASH ST_DIGITSLASHWS ST_DIGITSLASHWSCOLON @@ -116,6 +117,7 @@ iterRunes: // 2006-01-02T15:04:05Z07:00 // 2017-06-25T17:46:57.45706582-07:00 // 2006-01-02T15:04:05.999999999Z07:00 + // 2006-01-02T15:04:05+0000 // 2012-08-03 18:31:59.257000000 // 2014-04-26 17:24:37.3186369 // 2017-01-27 00:07:31.945167 @@ -203,9 +205,11 @@ iterRunes: // 2006-01-02T15:04:05.999999999Z07:00 // 2006-01-02T15:04:05Z07:00 // With another dash aka time-zone at end - // ST_DIGITDASHTDASH - // 2017-06-25T17:46:57.45706582-07:00 - // 2017-06-25T17:46:57+04:00 + // ST_DIGITDASHTDELTA + // ST_DIGITDASHTDELTACOLON + // 2017-06-25T17:46:57.45706582-07:00 + // 2017-06-25T17:46:57+04:00 + // 2006-01-02T15:04:05+0000 switch r { case '-', '+': state = ST_DIGITDASHTDELTA @@ -216,6 +220,10 @@ iterRunes: if unicode.IsDigit(r) { state = ST_DIGITDASHTZDIGIT } + case ST_DIGITDASHTDELTA: + if r == ':' { + state = ST_DIGITDASHTDELTACOLON + } case ST_DIGITSLASH: // starts digit then slash 02/ // 2014/07/10 06:55:38.156283 // 03/19/2012 10:11:59 @@ -451,6 +459,10 @@ iterRunes: return time.Parse("2006-01", datestr) } case ST_DIGITDASHTDELTA: + // 2006-01-02T15:04:05+0000 + return time.Parse("2006-01-02T15:04:05-0700", datestr) + + case ST_DIGITDASHTDELTACOLON: // With another +/- time-zone at end // 2006-01-02T15:04:05.999999999+07:00 // 2006-01-02T15:04:05.999999999-07:00 diff --git a/parseany_test.go b/parseany_test.go index 6e61f3e..19f4887 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -398,20 +398,29 @@ func TestParse(t *testing.T) { ts = MustParse("2014-05-11 08:20:13,787") assert.Equal(t, "2014-05-11 08:20:13.787 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) - _, err = ParseAny("2014-13-13 08:20:13,787") // month 13 doesn't exist + _, err = ParseAny("2014-13-13 08:20:13,787") // month 13 doesn't exist so error assert.NotEqual(t, nil, err) ts = MustParse("2014-05-11 08:20:13 +00:00") assert.Equal(t, "2014-05-11 08:20:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("2014-05-11 08:20:13 +0000") + assert.Equal(t, "2014-05-11 08:20:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("2016-06-21T19:55:00+01:00") assert.Equal(t, "2016-06-21 18:55:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) ts = MustParse("2016-06-21T19:55:00.799+01:00") assert.Equal(t, "2016-06-21 18:55:00.799 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) - ts = MustParse("2014-05-11 08:20:13 +0000") - assert.Equal(t, "2014-05-11 08:20:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("2016-06-21T19:55:00+0100") + assert.Equal(t, "2016-06-21 18:55:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + + ts = MustParse("2016-06-21T19:55:00-0700") + assert.Equal(t, "2016-06-22 02:55:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + + ts = MustParse("2016-06-21T19:55:00.799+0100") + assert.Equal(t, "2016-06-21 18:55:00.799 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) // yyyymmdd and similar ts = MustParse("2014") From 3bd2566cb7783f405a4696e323459bc758ec9d0e Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Mon, 17 Jul 2017 15:08:31 -0700 Subject: [PATCH 2/2] doc update --- README.md | 6 +----- example/main.go | 16 +++++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9cd5fb6..e7851da 100644 --- a/README.md +++ b/README.md @@ -88,11 +88,7 @@ func main() { table.AddHeaders("Input", "Parsed, and Output as %v") for _, dateExample := range examples { - t, err := dateparse.ParseAny(dateExample) - if err != nil { - panic(err.Error()) - } - table.AddRow(dateExample, fmt.Sprintf("%v", t)) + table.AddRow(dateExample, fmt.Sprintf("%v", dateparse.MustParse(dateExample))) } fmt.Println(table.Render()) } diff --git a/example/main.go b/example/main.go index bed1a75..d5b6922 100644 --- a/example/main.go +++ b/example/main.go @@ -14,9 +14,10 @@ var examples = []string{ "Mon Jan 02 15:04:05 -0700 2006", "Monday, 02-Jan-06 15:04:05 MST", "Mon, 02 Jan 2006 15:04:05 MST", + "Tue, 11 Jul 2017 16:28:13 +0200 (CEST)", + "Mon, 02 Jan 2006 15:04:05 -0700", "Mon Aug 10 15:44:11 UTC+0100 2015", "Fri Jul 03 2015 18:04:07 GMT+0100 (GMT Daylight Time)", - "Mon, 02 Jan 2006 15:04:05 -0700", "12 Feb 2006, 19:17", "2015-02-18 00:12:00 +0000 GMT", "2015-02-18 00:12:00 +0000 UTC", @@ -44,10 +45,12 @@ var examples = []string{ "2014/4/02 03:00:51", "2012/03/19 10:11:59", "2012/03/19 10:11:59.3186369", - // yyyy-mm-dd + // yyyy-mm-ddT "2009-08-12T22:15:09-07:00", "2009-08-12T22:15:09Z", "2009-08-12T22:15:09", + "2006-01-02T15:04:05+0000", + // yyyy-mm-dd hh:mm:ss "2014-04-26 17:24:37.3186369", "2014-04-26 17:24:37.318636", "2012-08-03 18:31:59.257000000 +0000 UTC", @@ -76,10 +79,7 @@ func main() { table.AddHeaders("Input", "Parsed, and Output as %v") for _, dateExample := range examples { - t, err := dateparse.ParseAny(dateExample) - if err != nil { - panic(err.Error()) - } + t := dateparse.MustParse(dateExample) table.AddRow(dateExample, fmt.Sprintf("%v", t)) } fmt.Println(table.Render()) @@ -95,9 +95,10 @@ func main() { | Mon Jan 02 15:04:05 -0700 2006 | 2006-01-02 15:04:05 -0700 -0700 | | Monday, 02-Jan-06 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST | | Mon, 02 Jan 2006 15:04:05 MST | 2006-01-02 15:04:05 +0000 MST | +| Tue, 11 Jul 2017 16:28:13 +0200 (CEST) | 2017-07-11 16:28:13 +0200 +0200 | +| Mon, 02 Jan 2006 15:04:05 -0700 | 2006-01-02 15:04:05 -0700 -0700 | | 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 | -| Mon, 02 Jan 2006 15:04:05 -0700 | 2006-01-02 15:04:05 -0700 -0700 | | 12 Feb 2006, 19:17 | 2006-02-12 19:17:00 +0000 UTC | | 2015-02-18 00:12:00 +0000 GMT | 2015-02-18 00:12:00 +0000 +0000 | | 2015-02-18 00:12:00 +0000 UTC | 2015-02-18 00:12:00 +0000 +0000 | @@ -127,6 +128,7 @@ func main() { | 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 PDT | | 2009-08-12T22:15:09Z | 2009-08-12 22:15:09 +0000 UTC | | 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC | +| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 +0000 | | 2014-04-26 17:24:37.3186369 | 2014-04-26 17:24:37.3186369 +0000 UTC | | 2014-04-26 17:24:37.318636 | 2014-04-26 17:24:37.318636 +0000 UTC | | 2012-08-03 18:31:59.257000000 +0000 UTC | 2012-08-03 18:31:59.257 +0000 +0000 |