Another date format from salesforce

This commit is contained in:
Aaron Raddon 2017-07-17 14:46:18 -07:00
parent b5a89160d8
commit cab5e508ca
2 changed files with 27 additions and 6 deletions

View File

@ -35,6 +35,7 @@ const (
ST_DIGITDASHTZ ST_DIGITDASHTZ
ST_DIGITDASHTZDIGIT ST_DIGITDASHTZDIGIT
ST_DIGITDASHTDELTA ST_DIGITDASHTDELTA
ST_DIGITDASHTDELTACOLON
ST_DIGITSLASH ST_DIGITSLASH
ST_DIGITSLASHWS ST_DIGITSLASHWS
ST_DIGITSLASHWSCOLON ST_DIGITSLASHWSCOLON
@ -116,6 +117,7 @@ iterRunes:
// 2006-01-02T15:04:05Z07:00 // 2006-01-02T15:04:05Z07:00
// 2017-06-25T17:46:57.45706582-07:00 // 2017-06-25T17:46:57.45706582-07:00
// 2006-01-02T15:04:05.999999999Z07:00 // 2006-01-02T15:04:05.999999999Z07:00
// 2006-01-02T15:04:05+0000
// 2012-08-03 18:31:59.257000000 // 2012-08-03 18:31:59.257000000
// 2014-04-26 17:24:37.3186369 // 2014-04-26 17:24:37.3186369
// 2017-01-27 00:07:31.945167 // 2017-01-27 00:07:31.945167
@ -203,9 +205,11 @@ iterRunes:
// 2006-01-02T15:04:05.999999999Z07:00 // 2006-01-02T15:04:05.999999999Z07:00
// 2006-01-02T15:04:05Z07:00 // 2006-01-02T15:04:05Z07:00
// With another dash aka time-zone at end // 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.45706582-07:00
// 2017-06-25T17:46:57+04:00 // 2017-06-25T17:46:57+04:00
// 2006-01-02T15:04:05+0000
switch r { switch r {
case '-', '+': case '-', '+':
state = ST_DIGITDASHTDELTA state = ST_DIGITDASHTDELTA
@ -216,6 +220,10 @@ iterRunes:
if unicode.IsDigit(r) { if unicode.IsDigit(r) {
state = ST_DIGITDASHTZDIGIT state = ST_DIGITDASHTZDIGIT
} }
case ST_DIGITDASHTDELTA:
if r == ':' {
state = ST_DIGITDASHTDELTACOLON
}
case ST_DIGITSLASH: // starts digit then slash 02/ case ST_DIGITSLASH: // starts digit then slash 02/
// 2014/07/10 06:55:38.156283 // 2014/07/10 06:55:38.156283
// 03/19/2012 10:11:59 // 03/19/2012 10:11:59
@ -451,6 +459,10 @@ iterRunes:
return time.Parse("2006-01", datestr) return time.Parse("2006-01", datestr)
} }
case ST_DIGITDASHTDELTA: 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 // With another +/- time-zone at end
// 2006-01-02T15:04:05.999999999+07:00 // 2006-01-02T15:04:05.999999999+07:00
// 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") 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))) 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) assert.NotEqual(t, nil, err)
ts = MustParse("2014-05-11 08:20:13 +00:00") 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))) 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") 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))) 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") 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))) 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") ts = MustParse("2016-06-21T19:55:00+0100")
assert.Equal(t, "2014-05-11 08:20:13 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) 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 // yyyymmdd and similar
ts = MustParse("2014") ts = MustParse("2014")