mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
ensure second, millisecond ints as dates use location
This commit is contained in:
parent
5bd34cf747
commit
e2f3c465e9
18
parseany.go
18
parseany.go
@ -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
|
||||||
|
@ -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)))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user