parse unix

This commit is contained in:
Aaron Raddon 2014-04-25 16:59:10 -07:00
parent 99b6954ef6
commit bb9e1dcbca
2 changed files with 23 additions and 0 deletions

View File

@ -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)
} }

View File

@ -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())