👽️ Support changes in go-dateparser
This commit is contained in:
parent
e9c1bf7070
commit
ece9256745
2 changed files with 16 additions and 3 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue