diff --git a/dateparse/main.go b/dateparse/main.go index 7e17d9f..cb543a8 100644 --- a/dateparse/main.go +++ b/dateparse/main.go @@ -56,13 +56,13 @@ func main() { for name, parser := range parsers { time.Local = nil - table.AddRow(name, "time.Local = nil", parser(datestr, nil), parser(datestr, nil).In(time.UTC)) + table.AddRow(name, "time.Local = nil", parser(datestr, nil, false), parser(datestr, nil, true)) if timezone != "" { time.Local = loc - table.AddRow(name, "time.Local = timezone arg", parser(datestr, loc), parser(datestr, loc).In(time.UTC)) + table.AddRow(name, "time.Local = timezone arg", parser(datestr, loc, false), parser(datestr, loc, true)) } time.Local = time.UTC - table.AddRow(name, "time.Local = time.UTC", parser(datestr, time.UTC), parser(datestr, time.UTC).In(time.UTC)) + table.AddRow(name, "time.Local = time.UTC", parser(datestr, time.UTC, false), parser(datestr, time.UTC, true)) } fmt.Println(table.Render()) @@ -72,20 +72,38 @@ func stuff() (string, string) { return "more", "stuff" } -type parser func(datestr string, loc *time.Location) time.Time +type parser func(datestr string, loc *time.Location, utc bool) string -func parseLocal(datestr string, loc *time.Location) time.Time { +func parseLocal(datestr string, loc *time.Location, utc bool) string { time.Local = loc - t, _ := dateparse.ParseLocal(datestr) - return t + t, err := dateparse.ParseLocal(datestr) + if err != nil { + return err.Error() + } + if utc { + return t.In(time.UTC).String() + } + return t.String() } -func parseIn(datestr string, loc *time.Location) time.Time { - t, _ := dateparse.ParseIn(datestr, loc) - return t +func parseIn(datestr string, loc *time.Location, utc bool) string { + t, err := dateparse.ParseIn(datestr, loc) + if err != nil { + return err.Error() + } + if utc { + return t.In(time.UTC).String() + } + return t.String() } -func parseAny(datestr string, loc *time.Location) time.Time { - t, _ := dateparse.ParseAny(datestr) - return t +func parseAny(datestr string, loc *time.Location, utc bool) string { + t, err := dateparse.ParseAny(datestr) + if err != nil { + return err.Error() + } + if utc { + return t.In(time.UTC).String() + } + return t.String() } diff --git a/parseany.go b/parseany.go index 81de181..7ba499c 100644 --- a/parseany.go +++ b/parseany.go @@ -374,6 +374,7 @@ iterRunes: // 04/2/2014 03:00:37 // 3/1/2012 10:11:59 // 4/8/2014 22:05 + // 4/8/14 22:05 switch r { case ':': state = stateDigitSlashWSColon @@ -384,6 +385,7 @@ iterRunes: // 04/2/2014 03:00:37 // 3/1/2012 10:11:59 // 4/8/2014 22:05 + // 4/8/14 22:05 // 3/1/2012 10:11:59 AM switch r { case ':': @@ -397,6 +399,7 @@ iterRunes: // 04/2/2014 03:00:37 // 3/1/2012 10:11:59 // 4/8/2014 22:05 + // 4/8/14 22:05 // 3/1/2012 10:11:59 AM switch r { case 'A', 'P': @@ -773,7 +776,7 @@ iterRunes: } } } else { - for _, layout := range []string{"01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04"} { + for _, layout := range []string{"01/02/2006 15:04", "01/2/2006 15:04", "1/02/2006 15:04", "1/2/2006 15:04", "1/2/06 15:04", "01/02/06 15:04"} { if t, err := parse(layout, datestr, loc); err == nil { return t, nil } @@ -810,6 +813,7 @@ iterRunes: // 3/1/2012 10:11:59 // 03/1/2012 10:11:59 // 3/01/2012 10:11:59 + // 4/8/14 22:05 if firstSlash == 4 { for _, layout := range []string{"2006/01/02 15:04:05", "2006/1/02 15:04:05", "2006/01/2 15:04:05", "2006/1/2 15:04:05"} { if t, err := parse(layout, datestr, loc); err == nil { diff --git a/parseany_test.go b/parseany_test.go index 29bc70d..bc813f0 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -199,10 +199,18 @@ func TestParse(t *testing.T) { ts = MustParse("4/8/2014 22:05") assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) - + ts = MustParse("4/18/2014 22:05") + assert.Equal(t, "2014-04-18 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) ts = MustParse("04/08/2014 22:05") assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("4/8/14 22:05") + assert.Equal(t, "2014-04-08 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("4/18/14 22:05") + assert.Equal(t, "2014-04-18 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("10/18/14 22:05") + assert.Equal(t, "2014-10-18 22:05:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + ts = MustParse("04/2/2014 4:00:51") assert.Equal(t, "2014-04-02 04:00:51 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))