stash/pkg/utils/date.go

28 lines
501 B
Go
Raw Permalink Normal View History

2019-02-09 12:30:49 +00:00
package utils
2019-03-05 01:14:52 +00:00
import (
"fmt"
"time"
)
func ParseDateStringAsTime(dateString string) (time.Time, error) {
2019-02-09 12:30:49 +00:00
// https://stackoverflow.com/a/20234207 WTF?
2019-03-05 01:14:52 +00:00
t, e := time.Parse(time.RFC3339, dateString)
if e == nil {
return t, nil
2019-03-05 01:14:52 +00:00
}
t, e = time.Parse("2006-01-02", dateString)
if e == nil {
return t, nil
2019-03-05 01:14:52 +00:00
}
t, e = time.Parse("2006-01-02 15:04:05", dateString)
if e == nil {
return t, nil
2019-03-05 01:14:52 +00:00
}
return time.Time{}, fmt.Errorf("ParseDateStringAsTime failed: dateString <%s>", dateString)
}