From 62e766bf9e640c42529b89aac2f8744303435c8a Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Sat, 10 Mar 2018 13:05:55 -0800 Subject: [PATCH] easier parsing of comma milliseconds --- parseany.go | 18 +++++++----------- parseany_test.go | 4 ++-- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/parseany.go b/parseany.go index 3d505df..60343a6 100644 --- a/parseany.go +++ b/parseany.go @@ -777,17 +777,12 @@ iterRunes: } switch r { case ',': - if len(datestr) == len("2014-05-11 08:20:13,787") { - // go doesn't seem to parse this one natively? or did i miss it? - t, err := parse("2006-01-02 03:04:05", datestr[:i], loc) - if err == nil { - ms, err := strconv.Atoi(datestr[i+1:]) - if err == nil { - return time.Unix(0, t.UnixNano()+int64(ms)*1e6), nil - } - } - return t, err - } + // hm, lets just swap out comma for period. for some reason go + // won't parse it. + // 2014-05-11 08:20:13,787 + ds := []byte(p.datestr) + ds[i] = '.' + return parseTime(string(ds), loc) case '-', '+': // 03:21:51+00:00 p.stateTime = timeOffset @@ -1203,6 +1198,7 @@ iterRunes: // 12 Feb 2006, 19:17 // 12 Feb 2006, 19:17:22 return p.parse() + case dateDigitWsMolong: // 18 January 2018 // 8 January 2018 diff --git a/parseany_test.go b/parseany_test.go index 8812d2c..7e7ac30 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -116,8 +116,8 @@ func TestInLocation(t *testing.T) { func TestOne(t *testing.T) { time.Local = time.UTC var ts time.Time - ts = MustParse("Mon Aug 10 15:44:11 PST-0700 2015") - assert.Equal(t, "2015-08-10 22:44:11 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + 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))) } func TestParse(t *testing.T) {