mirror of
https://github.com/araddon/dateparse.git
synced 2025-01-07 21:43:50 +08:00
parse unix
This commit is contained in:
parent
99b6954ef6
commit
bb9e1dcbca
16
parseany.go
16
parseany.go
@ -3,6 +3,7 @@ package dateparse
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
u "github.com/araddon/gou"
|
u "github.com/araddon/gou"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
//"unicode/utf8"
|
//"unicode/utf8"
|
||||||
@ -212,6 +213,21 @@ func ParseAny(datestr string) (time.Time, error) {
|
|||||||
u.Error(err)
|
u.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case f.Has(HAS_NUMERIC) && !f.Has(HAS_ALPHA) && !f.Has(HAS_WHITESPACE):
|
||||||
|
if len(datestr) >= len("13980450781991351") {
|
||||||
|
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
||||||
|
return time.Unix(0, nanoSecs), nil
|
||||||
|
} else {
|
||||||
|
u.Error(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
||||||
|
return time.Unix(secs, 0), nil
|
||||||
|
} else {
|
||||||
|
u.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
u.Errorf("Could not find format: %s", datestr)
|
u.Errorf("Could not find format: %s", datestr)
|
||||||
}
|
}
|
||||||
|
@ -59,11 +59,18 @@ func TestParse(t *testing.T) {
|
|||||||
//u.Debugf("%v", f)
|
//u.Debugf("%v", f)
|
||||||
//u.Debugf("%v", ts)
|
//u.Debugf("%v", ts)
|
||||||
assert.T(t, ts.In(time.UTC).Unix() == 1241805471)
|
assert.T(t, ts.In(time.UTC).Unix() == 1241805471)
|
||||||
|
|
||||||
ts, err = ParseAny("03/19/2012 10:11:59")
|
ts, err = ParseAny("03/19/2012 10:11:59")
|
||||||
assert.T(t, err == nil)
|
assert.T(t, err == nil)
|
||||||
//u.Debug(ts.Unix(), ts)
|
//u.Debug(ts.Unix(), ts)
|
||||||
assert.T(t, ts.Unix() == 1332151919)
|
assert.T(t, ts.Unix() == 1332151919)
|
||||||
|
|
||||||
|
// Unix Time Stamp
|
||||||
|
ts, err = ParseAny("1332151919")
|
||||||
|
assert.T(t, err == nil)
|
||||||
|
u.Debug(ts.Unix(), ts)
|
||||||
|
assert.T(t, ts.Unix() == 1332151919)
|
||||||
|
|
||||||
ts2, err := ParseAny("2009-08-12T22:15:09-07:00")
|
ts2, err := ParseAny("2009-08-12T22:15:09-07:00")
|
||||||
assert.T(t, err == nil)
|
assert.T(t, err == nil)
|
||||||
//u.Debug(ts2.In(time.UTC), " ", ts2.Unix())
|
//u.Debug(ts2.In(time.UTC), " ", ts2.Unix())
|
||||||
|
Loading…
Reference in New Issue
Block a user