diff --git a/parseany.go b/parseany.go index ae6d4df..c8a4040 100644 --- a/parseany.go +++ b/parseany.go @@ -4,6 +4,7 @@ import ( "fmt" u "github.com/araddon/gou" "strconv" + "strings" "time" "unicode" //"unicode/utf8" @@ -186,16 +187,30 @@ func ParseAny(datestr string) (time.Time, error) { case f.Has(HAS_WHITESPACE) && f.Has(HAS_COLON): // 03/03/2012 10:11:59 // 3/1/2012 10:11:59 - //u.Debugf("trying format: 3/1/2012 10:11:59 ") - //May 8, 2009 5:57:51 PM 2006-01-02 15:04:05.000 - if t, err := time.Parse("01/02/2006 15:04:05", datestr); err == nil { - return t, nil - } else { - if t, err := time.Parse("1/2/2006 15:04:05", datestr); err == nil { + // 4/8/2014 22:05 + colonCt := strings.Count(datestr, ":") + if len(datestr) >= len("03/03/2012 10:11:59") { + if t, err := time.Parse("01/02/2006 15:04:05", datestr); err == nil { return t, nil } else { - u.Error(err) + if t, err := time.Parse("1/2/2006 15:04:05", datestr); err == nil { + return t, nil + } else { + u.Error(err) + } } + } else if colonCt == 1 { + if t, err := time.Parse("01/02/2006 15:04", datestr); err == nil { + return t, nil + } else { + if t, err := time.Parse("1/2/2006 15:04", datestr); err == nil { + return t, nil + } else { + u.Error(err) + } + } + } else { + u.Error("unknown format: ", datestr) } case !f.Has(HAS_SLASH): // 3/1/2014 diff --git a/parseany_test.go b/parseany_test.go index 673cd6d..84afc1d 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -65,6 +65,16 @@ func TestParse(t *testing.T) { //u.Debug(ts.Unix(), ts) assert.T(t, ts.Unix() == 1332151919) + ts, err = ParseAny("4/8/2014 22:05") + assert.T(t, err == nil) + //u.Debug(ts.Unix(), ts) + assert.T(t, ts.Unix() == 1396994700) + + ts, err = ParseAny("04/08/2014 22:05") + assert.T(t, err == nil) + //u.Debug(ts.Unix(), ts) + assert.T(t, ts.Unix() == 1396994700) + // Unix Time Stamp ts, err = ParseAny("1332151919") assert.T(t, err == nil)