forked from go-gota/gota
/
date.parser.go
40 lines (32 loc) · 911 Bytes
/
date.parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package series
import (
"strings"
"time"
"github.com/araddon/dateparse"
"github.com/metakeule/fmtdate"
)
// ParseDateTimeFormat parse date and returns time format
func ParseDateTimeFormat(date string) (string, error) {
f, err := dateparse.ParseFormat(date)
if err != nil && strings.Contains(err.Error(), "month out of range") {
format := "DD.MM.YYYY"
if strings.Contains(date, "/") {
format = "DD/MM/YYYY"
}
return format, nil
}
return f, err
}
type DatetimeParserFn func(date string) (time.Time, error)
func DefaultDatetimeParser(date string) (time.Time, error) { // ParseDateTime parse string to time.Time
ts, err := dateparse.ParseAny(date)
if err != nil && strings.Contains(err.Error(), "month out of range") {
format := "DD.MM.YYYY"
if strings.Contains(date, "/") {
format = "DD/MM/YYYY"
}
ts, err = fmtdate.Parse(format, date)
return ts, err
}
return ts, err
}