From e2f3c465e969d5981ef2f7f83e18d266d5df908c Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Sat, 26 Aug 2017 14:12:55 -0700 Subject: [PATCH] ensure second, millisecond ints as dates use location --- parseany.go | 20 +++++++++++++++----- parseany_test.go | 3 +++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/parseany.go b/parseany.go index b9b8ac5..40e6e4d 100644 --- a/parseany.go +++ b/parseany.go @@ -551,26 +551,36 @@ iterRunes: // 1332151919 seconds // 20140601 yyyymmdd // 2014 yyyy + t := time.Time{} if len(datestr) > len("1499979795437000") { if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil { - return time.Unix(0, nanoSecs), nil + t = time.Unix(0, nanoSecs) } } else if len(datestr) > len("1499979795437") { if microSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil { - return time.Unix(0, microSecs*1000), nil + t = time.Unix(0, microSecs*1000) } } else if len(datestr) > len("1332151919") { if miliSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil { - return time.Unix(0, miliSecs*1000*1000), nil + t = time.Unix(0, miliSecs*1000*1000) } } else if len(datestr) == len("20140601") { return parse("20060102", datestr, loc) } else if len(datestr) == len("2014") { return parse("2006", datestr, loc) } - if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil { - return time.Unix(secs, 0), nil + if t.IsZero() { + if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil { + t = time.Unix(secs, 0) + } } + if !t.IsZero() { + if loc == nil { + return t, nil + } + return t.In(loc), nil + } + case stateDigitDash: // starts digit then dash 02- // 2006-01-02 // 2006-01 diff --git a/parseany_test.go b/parseany_test.go index 958ffb3..4a969ee 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -511,6 +511,9 @@ func TestParse(t *testing.T) { ts = MustParse("1384216367111") assert.Equal(t, "2013-11-12 00:32:47.111 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts, _ = ParseIn("1384216367111", time.UTC) + assert.Equal(t, "2013-11-12 00:32:47.111 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("1384216367111222") assert.Equal(t, "2013-11-12 00:32:47.111222 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))