parse strict refs #28

This commit is contained in:
Aaron Raddon
2018-03-24 17:49:27 -07:00
parent 934b3f146d
commit fd8f11d211
5 changed files with 169 additions and 70 deletions

View File

@@ -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