mirror of
https://github.com/araddon/dateparse.git
synced 2025-09-19 05:05:15 +08:00
parse strict refs #28
This commit is contained in:
17
parseany.go
17
parseany.go
@@ -84,7 +84,8 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"}
|
||||
//shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"}
|
||||
ErrAmbiguousMMDD = fmt.Errorf("This date has ambiguous mm/dd vs dd/mm type format")
|
||||
)
|
||||
|
||||
// ParseAny parse an unknown date format, detect the layout, parse.
|
||||
@@ -160,6 +161,20 @@ func ParseFormat(datestr string) (string, error) {
|
||||
return string(p.format), nil
|
||||
}
|
||||
|
||||
// ParseStrict parse an unknown date format. IF the date is ambigous
|
||||
// mm/dd vs dd/mm then return an error.
|
||||
// These return errors: 3.3.2014 , 8/8/71 etc
|
||||
func ParseStrict(datestr string) (time.Time, error) {
|
||||
p, err := parseTime(datestr, nil)
|
||||
if err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
if p.ambiguousMD {
|
||||
return time.Time{}, ErrAmbiguousMMDD
|
||||
}
|
||||
return p.parse()
|
||||
}
|
||||
|
||||
type parser struct {
|
||||
loc *time.Location
|
||||
preferMonthFirst bool
|
||||
|
Reference in New Issue
Block a user