mirror of
https://github.com/araddon/dateparse.git
synced 2025-01-06 04:53:46 +08:00
add another weird date format: 08/21/71
This commit is contained in:
parent
148ceeb09c
commit
abacbd8024
18
parseany.go
18
parseany.go
@ -36,6 +36,10 @@ const (
|
||||
|
||||
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
|
||||
func ParseAny(datestr string) (time.Time, error) {
|
||||
|
||||
@ -391,19 +395,13 @@ iterRunes:
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if len(datestr) == len("01/02/2006") {
|
||||
if t, err := time.Parse("01/02/2006", datestr); err == nil {
|
||||
for _, parseFormat := range shortDates {
|
||||
if t, err := time.Parse(parseFormat, datestr); err == 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
|
||||
|
@ -103,6 +103,12 @@ func TestParse(t *testing.T) {
|
||||
//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)))
|
||||
|
||||
// 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")
|
||||
assert.Tf(t, err == nil, "%v", err)
|
||||
//u.Debug(ts.In(time.UTC).Unix(), ts.In(time.UTC))
|
||||
|
Loading…
Reference in New Issue
Block a user