ensure second, millisecond ints as dates use location

This commit is contained in:
Aaron Raddon 2017-08-26 14:12:55 -07:00
parent 5bd34cf747
commit e2f3c465e9
2 changed files with 18 additions and 5 deletions

View File

@ -551,26 +551,36 @@ iterRunes:
// 1332151919 seconds // 1332151919 seconds
// 20140601 yyyymmdd // 20140601 yyyymmdd
// 2014 yyyy // 2014 yyyy
t := time.Time{}
if len(datestr) > len("1499979795437000") { if len(datestr) > len("1499979795437000") {
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil { 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") { } else if len(datestr) > len("1499979795437") {
if microSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil { 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") { } else if len(datestr) > len("1332151919") {
if miliSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil { 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") { } else if len(datestr) == len("20140601") {
return parse("20060102", datestr, loc) return parse("20060102", datestr, loc)
} else if len(datestr) == len("2014") { } else if len(datestr) == len("2014") {
return parse("2006", datestr, loc) return parse("2006", datestr, loc)
} }
if t.IsZero() {
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil { if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
return time.Unix(secs, 0), 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- case stateDigitDash: // starts digit then dash 02-
// 2006-01-02 // 2006-01-02
// 2006-01 // 2006-01

View File

@ -511,6 +511,9 @@ func TestParse(t *testing.T) {
ts = MustParse("1384216367111") ts = MustParse("1384216367111")
assert.Equal(t, "2013-11-12 00:32:47.111 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) 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") ts = MustParse("1384216367111222")
assert.Equal(t, "2013-11-12 00:32:47.111222 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) assert.Equal(t, "2013-11-12 00:32:47.111222 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))