add another weird date format: 08/21/71

This commit is contained in:
Aaron Raddon 2014-12-15 11:20:40 -08:00
parent 148ceeb09c
commit abacbd8024
2 changed files with 14 additions and 10 deletions

View File

@ -36,6 +36,10 @@ const (
var _ = u.EMPTY var _ = u.EMPTY
var (
shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06"}
)
// Given an unknown date format, detect the type, parse, return time // Given an unknown date format, detect the type, parse, return time
func ParseAny(datestr string) (time.Time, error) { func ParseAny(datestr string) (time.Time, error) {
@ -391,19 +395,13 @@ iterRunes:
} }
} }
} else { } else {
if len(datestr) == len("01/02/2006") { for _, parseFormat := range shortDates {
if t, err := time.Parse("01/02/2006", datestr); err == nil { if t, err := time.Parse(parseFormat, datestr); err == nil {
return t, nil return t, nil
} else {
return time.Time{}, err
}
} else {
if t, err := time.Parse("1/2/2006", datestr); err == nil {
return t, nil
} else {
return time.Time{}, err
} }
} }
return time.Time{}, fmt.Errorf("Unrecognized dateformat: %v", datestr)
} }
case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace case ST_DIGITSLASHWSCOLON: // starts digit then slash 02/ more digits/slashes then whitespace

View File

@ -103,6 +103,12 @@ func TestParse(t *testing.T) {
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC))) assert.T(t, "2014-03-31 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
// what type of date is this? 08/21/71
ts, err = ParseAny("08/21/71")
assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
assert.T(t, "1971-08-21 00:00:00 +0000 UTC" == fmt.Sprintf("%v", ts.In(time.UTC)))
ts, err = ParseAny("4/8/2014 22:05") ts, err = ParseAny("4/8/2014 22:05")
assert.Tf(t, err == nil, "%v", err) assert.Tf(t, err == nil, "%v", err)
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC)) //u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))