🐛 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"
|
||||
)
|
||||
|
||||
const (
|
||||
day = time.Hour * 24
|
||||
)
|
||||
|
||||
// These are somewhat arbitrary, but reasonably useful min and max times
|
||||
var (
|
||||
MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900
|
||||
|
|
@ -25,21 +21,30 @@ func ParseDate(in string) (t time.Time, err error) {
|
|||
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 {
|
||||
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 {
|
||||
case date.Second:
|
||||
t = t.Truncate(time.Second)
|
||||
case date.Minute:
|
||||
trunc = time.Minute
|
||||
t = t.Truncate(time.Minute)
|
||||
case date.Hour:
|
||||
trunc = time.Hour
|
||||
t = time.Date(y, mon, day, h, 0, 0, 0, loc)
|
||||
case date.Day:
|
||||
trunc = day
|
||||
// @todo Handle other cases separately
|
||||
t = time.Date(y, mon, day, 0, 0, 0, 0, loc)
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue