From eeec45e1208c57b6c10df86698fdc489941b2cbc Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Wed, 30 Sep 2015 12:09:08 -0700 Subject: [PATCH] 2 new date formats --- parseany.go | 31 ++++++++++++++++++++++++++++--- parseany_test.go | 9 +++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/parseany.go b/parseany.go index 8d42ac4..25a1663 100644 --- a/parseany.go +++ b/parseany.go @@ -2,11 +2,13 @@ package dateparse import ( "fmt" - u "github.com/araddon/gou" "strconv" + "strings" "time" "unicode" "unicode/utf8" + + u "github.com/araddon/gou" ) type DateState int @@ -24,6 +26,7 @@ const ( ST_DIGITSLASHWS ST_DIGITSLASHWSCOLON ST_DIGITSLASHWSCOLONCOLON + ST_DIGITSLASHWSCOLONCOLONAMPM ST_ALPHA ST_ALPHAWS ST_ALPHAWSCOMMA @@ -220,10 +223,22 @@ iterRunes: // 04/2/2014 03:00:37 // 3/1/2012 10:11:59 // 4/8/2014 22:05 + // 3/1/2012 10:11:59 AM switch r { case ':': state = ST_DIGITSLASHWSCOLONCOLON } + case ST_DIGITSLASHWSCOLONCOLON: // starts digit then slash 02/ more digits/slashes then whitespace + // 2014/07/10 06:55:38.156283 + // 03/19/2012 10:11:59 + // 04/2/2014 03:00:37 + // 3/1/2012 10:11:59 + // 4/8/2014 22:05 + // 3/1/2012 10:11:59 AM + switch r { + case 'A', 'P': + state = ST_DIGITSLASHWSCOLONCOLONAMPM + } case ST_ALPHA: // starts alpha // May 8, 2009 5:57:51 PM // Mon Jan _2 15:04:05 2006 @@ -427,6 +442,12 @@ iterRunes: } else { return time.Time{}, err } + case len("2015-09-30 18:48:56.35272715 +0000 UTC"): + if t, err := time.Parse("2006-01-02 15:04:05.00000000 +0000 UTC", datestr); err == nil { + return t, nil + } else { + return time.Time{}, err + } case len("2015-06-25 01:25:37.115208593 +0000 UTC"): if t, err := time.Parse("2006-01-02 15:04:05.000000000 +0000 UTC", datestr); err == nil { return t, nil @@ -518,13 +539,16 @@ iterRunes: } } } - case ST_DIGITSLASHWSCOLONCOLON: // starts digit then slash 02/ more digits/slashes then whitespace double colons + case ST_DIGITSLASHWSCOLONCOLON, ST_DIGITSLASHWSCOLONCOLONAMPM: // starts digit then slash 02/ more digits/slashes then whitespace double colons // 2014/07/10 06:55:38.156283 // 03/19/2012 10:11:59 // 3/1/2012 10:11:59 // 03/1/2012 10:11:59 // 3/01/2012 10:11:59 - + if state == ST_DIGITSLASHWSCOLONCOLONAMPM { + datestr = strings.Replace(datestr, " AM", "", -1) + datestr = strings.Replace(datestr, " PM", "", -1) + } if firstSlash == 4 { if len(datestr) == len("2014/07/10 06:55:38.156283") { if t, err := time.Parse("2006/01/02 15:04:05.000000", datestr); err == nil { @@ -586,6 +610,7 @@ iterRunes: } } } + case ST_ALPHACOMMA: // Starts alpha then comma but no DASH // Mon, 02 Jan 2006 15:04:05 MST if t, err := time.Parse("Jan 2, 2006 3:04:05 PM", datestr); err == nil { diff --git a/parseany_test.go b/parseany_test.go index 23e695a..90b4e00 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -141,6 +141,10 @@ func TestParse(t *testing.T) { //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) assert.T(t, "2014-04-02 03:00:51 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + ts, err = ParseAny("8/8/1965 12:00:00 AM") + //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC), " error ", err) + assert.T(t, "1965-08-08 12:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + ts, err = ParseAny("4/02/2014 03:00:51") assert.Tf(t, err == nil, "%v", err) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) @@ -222,6 +226,11 @@ func TestParse(t *testing.T) { //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) assert.T(t, "2012-08-03 18:31:59.257 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + ts, err = ParseAny("2015-09-30 18:48:56.35272715 +0000 UTC") + assert.Tf(t, err == nil, "%v", err) + //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) + assert.T(t, "2015-09-30 18:48:56.35272715 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + ts, err = ParseAny("2012-08-03 18:31:59.257000000") assert.Tf(t, err == nil, "%v", err) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))