Merge pull request #24 from araddon/salesforce_format

Another date format from salesforce
This commit is contained in:
Aaron Raddon 2017-07-17 16:05:23 -07:00 committed by GitHub
commit 4fa7a8202c
4 changed files with 37 additions and 18 deletions

View File

@ -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())
}

View File

@ -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 |

View File

@ -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
// 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

View File

@ -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")