ParseDate should handle DateFormat

This commit is contained in:
Dan Jones 2024-02-25 12:36:43 -06:00
commit 391452e3d9
6 changed files with 17 additions and 940 deletions

View file

@ -7,6 +7,8 @@ import (
"github.com/markusmobius/go-dateparser/date"
)
const DateFormat = "January 02, 2006 at 03:04:05PM -0700"
// These are somewhat arbitrary, but reasonably useful min and max times
var (
MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900
@ -21,12 +23,16 @@ func ParseDate(in string) (t time.Time, err error) {
return MaxTime, nil
}
d, err := dp.Parse(&dp.Configuration{
conf := dp.Configuration{
CurrentTime: time.Now().Local(),
ReturnTimeAsPeriod: true,
}, in)
}
d, err := dp.Parse(&conf, in)
t = d.Time
if err != nil {
d, err = dp.Parse(&conf, in, DateFormat)
t = d.Time
return
}

View file

@ -23,9 +23,11 @@ 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", -2*60*60))
exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -7200))
var ts int64 = 1708876012
tsd := time.Unix(ts, 0)
ent := "February 25, 2024 at 04:00:13AM +0230"
entd, _ := time.Parse(DateFormat, ent)
tests := []struct {
name string
@ -44,6 +46,7 @@ func TestParseDate(t *testing.T) {
{"max", MaxTime, ""},
{exact, exactd, ""},
{fmt.Sprint(ts), tsd, ""},
{ent, entd, ""},
{"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")},
}
for _, tt := range tests {