add new format for no seconds

This commit is contained in:
Aaron Raddon 2014-04-27 13:26:17 -07:00
parent bb9e1dcbca
commit 98484a1f48
2 changed files with 32 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
u "github.com/araddon/gou" u "github.com/araddon/gou"
"strconv" "strconv"
"strings"
"time" "time"
"unicode" "unicode"
//"unicode/utf8" //"unicode/utf8"
@ -186,8 +187,9 @@ func ParseAny(datestr string) (time.Time, error) {
case f.Has(HAS_WHITESPACE) && f.Has(HAS_COLON): case f.Has(HAS_WHITESPACE) && f.Has(HAS_COLON):
// 03/03/2012 10:11:59 // 03/03/2012 10:11:59
// 3/1/2012 10:11:59 // 3/1/2012 10:11:59
//u.Debugf("trying format: 3/1/2012 10:11:59 ") // 4/8/2014 22:05
//May 8, 2009 5:57:51 PM 2006-01-02 15:04:05.000 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 { if t, err := time.Parse("01/02/2006 15:04:05", datestr); err == nil {
return t, nil return t, nil
} else { } else {
@ -197,6 +199,19 @@ func ParseAny(datestr string) (time.Time, error) {
u.Error(err) 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): case !f.Has(HAS_SLASH):
// 3/1/2014 // 3/1/2014
// 10/13/2014 // 10/13/2014

View File

@ -65,6 +65,16 @@ func TestParse(t *testing.T) {
//u.Debug(ts.Unix(), ts) //u.Debug(ts.Unix(), ts)
assert.T(t, ts.Unix() == 1332151919) 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 // Unix Time Stamp
ts, err = ParseAny("1332151919") ts, err = ParseAny("1332151919")
assert.T(t, err == nil) assert.T(t, err == nil)