mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
support yyyddmm integer values as date
This commit is contained in:
parent
ed500d214a
commit
b93aaa0470
11
parseany.go
11
parseany.go
@ -335,6 +335,11 @@ iterRunes:
|
||||
switch state {
|
||||
case ST_DIGIT:
|
||||
// unixy timestamps ish
|
||||
// 13980450781991351 nanoseconds
|
||||
// 13980450781991 microseconds
|
||||
// 1384216367189
|
||||
// 1332151919 seconds
|
||||
// 20140601 yyyymmdd
|
||||
if len(datestr) >= len("13980450781991351") {
|
||||
if nanoSecs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
||||
return time.Unix(0, nanoSecs), nil
|
||||
@ -353,6 +358,12 @@ iterRunes:
|
||||
} else {
|
||||
return time.Time{}, err
|
||||
}
|
||||
} else if len(datestr) == len("20140601") {
|
||||
if t, err := time.Parse("20060102", datestr); err == nil {
|
||||
return t, nil
|
||||
} else {
|
||||
return time.Time{}, err
|
||||
}
|
||||
} else {
|
||||
if secs, err := strconv.ParseInt(datestr, 10, 64); err == nil {
|
||||
return time.Unix(secs, 0), nil
|
||||
|
@ -43,6 +43,8 @@ import (
|
||||
10/13/2014
|
||||
01/02/2006
|
||||
|
||||
20140601
|
||||
|
||||
2016-03-14 00:00:00.000
|
||||
2006-01-02
|
||||
2014-05-11 08:20:13,787 // i couldn't find parser for this in go?
|
||||
@ -255,6 +257,12 @@ func TestParse(t *testing.T) {
|
||||
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
|
||||
assert.T(t, "2014-05-11 08:20:13.787 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||
|
||||
// yyyy mm dd
|
||||
ts, err = ParseAny("20140601")
|
||||
assert.Tf(t, err == nil, "%v", err)
|
||||
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
|
||||
assert.T(t, "2014-06-01 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||
|
||||
ts, err = ParseAny("1332151919")
|
||||
assert.Tf(t, err == nil, "%v", err)
|
||||
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
|
||||
|
Loading…
Reference in New Issue
Block a user