1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-26 05:51:20 +00:00
go_helpers/time/parse.go
2018-07-05 10:09:39 +02:00

36 lines
1.2 KiB
Go

package time
import (
"errors"
gotime "time"
)
// ErrParseNotPossible describes a collected error which is returned
// when the given date value could not be parsed with any given layout
var ErrParseNotPossible = errors.New("No layout matched given value or other error occurred")
// MultiParse takes multiple layout strings and tries to parse the
// value with them. In case none of the layouts matches or another error
// occurs ErrParseNotPossible is returned
func MultiParse(layouts []string, value string) (gotime.Time, error) {
for _, layout := range layouts {
if t, err := gotime.Parse(layout, value); err == nil {
return t, nil
}
}
return gotime.Time{}, ErrParseNotPossible
}
// MultiParseInLocation takes multiple layouts and tries to parse the
// value with them using time.ParseInLocation. In case none of the layouts
// matches or another error occurs ErrParseNotPossible is returned
func MultiParseInLocation(layouts []string, value string, loc *gotime.Location) (gotime.Time, error) {
for _, layout := range layouts {
if t, err := gotime.ParseInLocation(layout, value, loc); err == nil {
return t, nil
}
}
return gotime.Time{}, ErrParseNotPossible
}