🐛 Only truncate minutes and seconds
Also ensure we're getting date.Time for specific times
This commit is contained in:
parent
70b82761c2
commit
44e79916d3
2 changed files with 33 additions and 14 deletions
|
|
@ -7,10 +7,6 @@ import (
|
||||||
"github.com/markusmobius/go-dateparser/date"
|
"github.com/markusmobius/go-dateparser/date"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
day = time.Hour * 24
|
|
||||||
)
|
|
||||||
|
|
||||||
// These are somewhat arbitrary, but reasonably useful min and max times
|
// These are somewhat arbitrary, but reasonably useful min and max times
|
||||||
var (
|
var (
|
||||||
MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900
|
MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900
|
||||||
|
|
@ -25,21 +21,30 @@ func ParseDate(in string) (t time.Time, err error) {
|
||||||
return MaxTime, nil
|
return MaxTime, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
d, err := dp.Parse(nil, in)
|
d, err := dp.Parse(&dp.Configuration{
|
||||||
|
CurrentTime: time.Now().Local(),
|
||||||
|
ReturnTimeAsPeriod: true,
|
||||||
|
}, in)
|
||||||
|
t = d.Time
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t = d.Time.Local()
|
|
||||||
trunc := time.Second
|
y, mon, day, h, loc := t.Year(), t.Month(), t.Day(), t.Hour(), t.Location()
|
||||||
switch d.Period {
|
switch d.Period {
|
||||||
|
case date.Second:
|
||||||
|
t = t.Truncate(time.Second)
|
||||||
case date.Minute:
|
case date.Minute:
|
||||||
trunc = time.Minute
|
t = t.Truncate(time.Minute)
|
||||||
case date.Hour:
|
case date.Hour:
|
||||||
trunc = time.Hour
|
t = time.Date(y, mon, day, h, 0, 0, 0, loc)
|
||||||
case date.Day:
|
case date.Day:
|
||||||
trunc = day
|
t = time.Date(y, mon, day, 0, 0, 0, 0, loc)
|
||||||
// @todo Handle other cases separately
|
case date.Month:
|
||||||
|
t = time.Date(y, mon, 1, 0, 0, 0, 0, loc)
|
||||||
|
case date.Year:
|
||||||
|
t = time.Date(y, 1, 1, 0, 0, 0, 0, loc)
|
||||||
}
|
}
|
||||||
t = t.Truncate(trunc)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,24 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const day = time.Hour * 24
|
||||||
|
|
||||||
func TestParseDate(t *testing.T) {
|
func TestParseDate(t *testing.T) {
|
||||||
now := time.Now().Local()
|
now := time.Now().Local()
|
||||||
|
y, mon, d, h, loc := now.Year(), now.Month(), now.Day(), now.Hour(), now.Location()
|
||||||
sec := now.Truncate(time.Second)
|
sec := now.Truncate(time.Second)
|
||||||
today := now.Truncate(day)
|
today := time.Date(y, mon, d, 0, 0, 0, 0, loc)
|
||||||
tomorrow := today.Add(day)
|
tomorrow := today.Add(day)
|
||||||
yesterday := today.Add(-day)
|
yesterday := today.Add(-day)
|
||||||
twoMin := now.Add(2 * time.Minute).Truncate(time.Minute)
|
twoMin := now.Add(2 * time.Minute).Truncate(time.Minute)
|
||||||
twoHour := now.Add(2 * time.Hour).Truncate(time.Hour)
|
twoHour := time.Date(y, mon, d, h+2, 0, 0, 0, loc)
|
||||||
|
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))
|
||||||
|
var ts int64 = 1708876012
|
||||||
|
tsd := time.Unix(ts, 0)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
exp time.Time
|
exp time.Time
|
||||||
|
|
@ -28,8 +38,12 @@ func TestParseDate(t *testing.T) {
|
||||||
{"yesterday", yesterday, ""},
|
{"yesterday", yesterday, ""},
|
||||||
{"in two minutes", twoMin, ""},
|
{"in two minutes", twoMin, ""},
|
||||||
{"in two hours", twoHour, ""},
|
{"in two hours", twoHour, ""},
|
||||||
|
{"this month", firstMonth, ""},
|
||||||
|
{"this year", firstYear, ""},
|
||||||
{"min", MinTime, ""},
|
{"min", MinTime, ""},
|
||||||
{"max", MaxTime, ""},
|
{"max", MaxTime, ""},
|
||||||
|
{exact, exactd, ""},
|
||||||
|
{fmt.Sprint(ts), tsd, ""},
|
||||||
{"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")},
|
{"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue