optimize parse date performance

This commit is contained in:
Oleg Ozimok 2018-09-26 16:34:18 +03:00 committed by Oleg Ozimok
parent f39e9544b2
commit e091ba5193

View File

@ -18,6 +18,31 @@ import (
var TimeLocation = time.UTC var TimeLocation = time.UTC
var ParseDateFormats = []string{
time.RFC3339,
"2006-01-02T15:04:05", // iso8601 without timezone
time.RFC1123Z,
time.RFC1123,
time.RFC822Z,
time.RFC822,
time.RFC850,
time.ANSIC,
time.UnixDate,
time.RubyDate,
"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
"2006-01-02",
"02 Jan 2006",
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05Z07:00", // RFC3339 without T
"2006-01-02 15:04:05",
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
}
var errNegativeNotAllowed = errors.New("unable to cast negative value") var errNegativeNotAllowed = errors.New("unable to cast negative value")
// ToTimeE casts an interface to a time.Time type. // ToTimeE casts an interface to a time.Time type.
@ -1127,30 +1152,7 @@ func ToDurationSliceE(i interface{}) ([]time.Duration, error) {
// predefined list of formats. If no suitable format is found, an error is // predefined list of formats. If no suitable format is found, an error is
// returned. // returned.
func StringToDate(s string) (time.Time, error) { func StringToDate(s string) (time.Time, error) {
return parseDateWith(s, []string{ return parseDateWith(s, ParseDateFormats)
time.RFC3339,
"2006-01-02T15:04:05", // iso8601 without timezone
time.RFC1123Z,
time.RFC1123,
time.RFC822Z,
time.RFC822,
time.RFC850,
time.ANSIC,
time.UnixDate,
time.RubyDate,
"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
"2006-01-02",
"02 Jan 2006",
"2006-01-02 15:04:05 -07:00",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05Z07:00", // RFC3339 without T
"2006-01-02 15:04:05",
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
})
} }
func parseDateWith(s string, dates []string) (d time.Time, e error) { func parseDateWith(s string, dates []string) (d time.Time, e error) {