From 62adefbf6b39884ac86cb660798c99059f0700ed Mon Sep 17 00:00:00 2001 From: junichif Date: Wed, 14 Jun 2017 13:56:12 -0700 Subject: [PATCH] parse 2006-01-02 15:04:05 -07:00 --- parseany.go | 15 +++++++++++---- parseany_test.go | 8 ++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/parseany.go b/parseany.go index 8c91508..aca3701 100644 --- a/parseany.go +++ b/parseany.go @@ -500,11 +500,18 @@ iterRunes: } case ST_DIGITDASHWS: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3 // 2013-04-01 22:43:22 - if t, err := time.Parse("2006-01-02 15:04:05", datestr); err == nil { - return t, nil - } else { - return time.Time{}, err + var t time.Time + var err error + + switch len(datestr) { + case len("2006-01-02 15:04:05"): + t, err = time.Parse("2006-01-02 15:04:05", datestr) + case len("2006-01-02 15:04:05 -0700"): + t, err = time.Parse("2006-01-02 15:04:05 -0700", datestr) + case len("2006-01-02 15:04:05 -07:00"): + t, err = time.Parse("2006-01-02 15:04:05 -07:00", datestr) } + return t, err case ST_DIGITDASHWSALPHA: // starts digit then dash 02- then whitespace 1 << 2 << 5 + 3 // 2014-12-16 06:20:00 UTC // 2015-02-18 00:12:00 +0000 UTC diff --git a/parseany_test.go b/parseany_test.go index fc49f96..51d9396 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -383,6 +383,14 @@ func TestParse(t *testing.T) { assertf(t, err == nil, "%v", err) assert(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + ts, err = ParseAny("2014-05-11 08:20:13 +00:00") + assertf(t, err == nil, "%v", err) + assert(t, "2014-05-11 08:20:13 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + + ts, err = ParseAny("2014-05-11 08:20:13 +0000") + assertf(t, err == nil, "%v", err) + assert(t, "2014-05-11 08:20:13 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + // yyyymmdd and similar ts, err = ParseAny("2014") assertf(t, err == nil, "%v", err)