From 9486d2dfd39fbd2d560abbc89c2238393d3907bf Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Sun, 11 May 2014 13:33:13 -0700 Subject: [PATCH] two more date formats --- parseany.go | 25 ++++++++++++++++++++++++- parseany_test.go | 17 ++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/parseany.go b/parseany.go index d930ffb..359e674 100644 --- a/parseany.go +++ b/parseany.go @@ -108,6 +108,7 @@ iterRunes: } case 256: // starts digit then slash 02/ // 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/2014 @@ -122,6 +123,7 @@ iterRunes: } case 2048: // starts digit then slash 02/ more digits/slashes then whitespace // 03/19/2012 10:11:59 + // 04/2/2014 03:00:37 // 3/1/2012 10:11:59 // 4/8/2014 22:05 switch r { @@ -246,6 +248,16 @@ iterRunes: } else { u.Error(err) } + } else if len(datestr) == len("01/2/2006 15:04") { + if t, err := time.Parse("01/2/2006 15:04", datestr); err == nil { + return t, nil + } else { + if t, err := time.Parse("1/02/2006 15:04", datestr); err == nil { + return t, nil + } else { + u.Error(err) + } + } } else { if t, err := time.Parse("1/2/2006 15:04", datestr); err == nil { return t, nil @@ -256,13 +268,24 @@ iterRunes: case 268435456: // starts digit then slash 02/ more digits/slashes then whitespace double colons // 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 len(datestr) == len("01/02/2006 15:04:05") { if t, err := time.Parse("01/02/2006 15:04:05", datestr); err == nil { return t, nil } else { u.Error(err) } + } else if len(datestr) == len("01/2/2006 15:04:05") { + if t, err := time.Parse("01/2/2006 15:04:05", datestr); err == nil { + return t, nil + } else { + if t, err := time.Parse("1/02/2006 15:04:05", datestr); err == nil { + return t, nil + } else { + u.Error(err) + } + } } else { if t, err := time.Parse("1/2/2006 15:04:05", datestr); err == nil { return t, nil diff --git a/parseany_test.go b/parseany_test.go index fd98655..155e84e 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -38,6 +38,7 @@ var _ = time.April Apr 7, 2014 4:58:55 PM 03/19/2012 10:11:59 + 04/2/2014 03:00:37 3/1/2014 10/13/2014 01/02/2006 @@ -83,6 +84,16 @@ func TestParse(t *testing.T) { //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) assert.T(t, "2014-04-08 22:05:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + ts, err = ParseAny("04/2/2014 03:00:51") + assert.T(t, err == nil) + //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("4/02/2014 03:00:51") + assert.T(t, err == nil) + //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("1332151919") assert.T(t, err == nil) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) @@ -120,8 +131,8 @@ func TestParse(t *testing.T) { } func TestWIP(t *testing.T) { - // ts, err := ParseAny("2014-05-11 08:20:13,787") + // ts, err := ParseAny("04/2/2014 03:00:51") // assert.T(t, err == nil) - // u.Debug(ts.Unix(), ts) - // assert.T(t, "2014-04-26 17:24:37 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) + // 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))) }