diff --git a/tools/parse_date.go b/tools/parse_date.go index d060299..0fa117a 100644 --- a/tools/parse_date.go +++ b/tools/parse_date.go @@ -1,6 +1,7 @@ package tools import ( + "strconv" "time" dp "github.com/markusmobius/go-dateparser" @@ -23,6 +24,19 @@ func ParseDate(in string) (t time.Time, err error) { return MaxTime, nil } + var er error + for _, format := range []string{time.RFC3339, DateFormat} { + if t, er = time.ParseInLocation(format, in, nil); er == nil { + return + } + } + + var ts int64 + if ts, er = strconv.ParseInt(in, 10, 0); er == nil { + t = time.Unix(ts, 0) + return + } + conf := dp.Configuration{ CurrentTime: time.Now().Local(), ReturnTimeAsPeriod: true, @@ -32,12 +46,11 @@ func ParseDate(in string) (t time.Time, err error) { d, err := dp.Parse(&conf, in) t = d.Time if err != nil { - d, err = dp.Parse(&conf, in, DateFormat) - t = d.Time return } y, mon, day, h, loc := t.Year(), t.Month(), t.Day(), t.Hour(), t.Location() + switch d.Period { case date.Second: t = t.Truncate(time.Second) diff --git a/tools/parse_date_test.go b/tools/parse_date_test.go index 7aa30b2..3099b88 100644 --- a/tools/parse_date_test.go +++ b/tools/parse_date_test.go @@ -23,7 +23,7 @@ func TestParseDate(t *testing.T) { firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc) firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc) exact := "2075-02-12T12:13:54.536-02:00" - exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -7200)) + exactd, _ := time.ParseInLocation(time.RFC3339, exact, nil) var ts int64 = 1708876012 tsd := time.Unix(ts, 0) ent := "February 25, 2024 at 04:00:13AM +0230"