From b93aaa047099cb67819bc1b812cdadfd86646865 Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Tue, 18 Aug 2015 15:50:04 -0700 Subject: [PATCH] support yyyddmm integer values as date --- parseany.go | 11 +++++++++++ parseany_test.go | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/parseany.go b/parseany.go index 788aabc..8d42ac4 100644 --- a/parseany.go +++ b/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 diff --git a/parseany_test.go b/parseany_test.go index 398ee36..23e695a 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -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))