From fd8f11d2115eb31ff1757a698369c893cfdef95a Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Sat, 24 Mar 2018 17:49:27 -0700 Subject: [PATCH 1/2] parse strict refs #28 --- README.md | 4 ++ dateparse/README.md | 169 +++++++++++++++++++++++++++----------------- dateparse/main.go | 20 ++++-- parseany.go | 17 ++++- parseany_test.go | 29 ++++++++ 5 files changed, 169 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 149ecec..49a0a17 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,10 @@ Parse many date strings without knowing format in advance. Uses a scanner to re // Normal parse. Equivalent Timezone rules as time.Parse() t, err := dateparse.ParseAny("3/1/2014") +// Parse Strict, error on ambigous mm/dd vs dd/mm dates +t, err := dateparse.ParseStrict("3/1/2014") +> returns error + // Parse with Location, equivalent to time.ParseInLocation() timezone/offset // rules. Using location arg, if timezone/offset info exists in the // datestring, it uses the given location rules for any zone interpretation. diff --git a/dateparse/README.md b/dateparse/README.md index ba809b5..c0f6ce6 100644 --- a/dateparse/README.md +++ b/dateparse/README.md @@ -13,19 +13,27 @@ $ dateparse --timezone="America/Denver" "2017-07-19 03:21:00" Your Current time.Local zone is PDT -+------------+---------------------------+-------------------------------+-------------------------------+ -| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | -+------------+---------------------------+-------------------------------+-------------------------------+ -| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | -| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | -| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | -| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | -| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | -+------------+---------------------------+-------------------------------+-------------------------------+ +Layout String: dateparse.ParseFormat() => 2006-01-02 15:04:05 + +Your Using time.Local set to location=America/Denver MDT + ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | ++-------------+---------------------------+-------------------------------+-------------------------------------+ + # Note on this one that the outputed zone is always UTC/0 offset as opposed to above @@ -33,38 +41,53 @@ $ dateparse --timezone="America/Denver" "2017-07-19 03:21:51+00:00" Your Current time.Local zone is PDT -+------------+---------------------------+---------------------------------+-------------------------------+ -| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | -+------------+---------------------------+---------------------------------+-------------------------------+ -| ParseAny | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | -| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | -| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | -| ParseIn | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | -| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | -| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | -| ParseLocal | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | -| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | -| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | -+------------+---------------------------+---------------------------------+-------------------------------+ +Layout String: dateparse.ParseFormat() => 2006-01-02 15:04:05-07:00 + +Your Using time.Local set to location=America/Denver MDT + ++-------------+---------------------------+---------------------------------+-------------------------------------+ +| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | ++-------------+---------------------------+---------------------------------+-------------------------------------+ +| ParseAny | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC day=3 | +| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC day=3 | +| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC day=3 | +| ParseIn | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | +| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | +| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseStrict | time.Local = nil | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | +| ParseStrict | time.Local = timezone arg | 2017-07-19 03:21:51 +0000 +0000 | 2017-07-19 03:21:51 +0000 UTC | +| ParseStrict | time.Local = time.UTC | 2017-07-19 03:21:51 +0000 UTC | 2017-07-19 03:21:51 +0000 UTC | ++-------------+---------------------------+---------------------------------+-------------------------------------+ $ dateparse --timezone="America/Denver" "Monday, 19-Jul-17 03:21:00 MDT" Your Current time.Local zone is PDT -+------------+---------------------------+-------------------------------+-------------------------------+ -| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | -+------------+---------------------------+-------------------------------+-------------------------------+ -| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | -| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | -| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | -| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -+------------+---------------------------+-------------------------------+-------------------------------+ +Layout String: dateparse.ParseFormat() => 02-Jan-06 15:04:05 MDT + +Your Using time.Local set to location=America/Denver MDT + ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| ParseStrict | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:00 -0600 MDT | 2017-07-19 09:21:00 +0000 UTC | +| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | ++-------------+---------------------------+-------------------------------+-------------------------------------+ + # pass in a wrong timezone "MST" (should be MDT) @@ -72,19 +95,27 @@ $ dateparse --timezone="America/Denver" "Monday, 19-Jul-17 03:21:00 MST" Your Current time.Local zone is PDT -+------------+---------------------------+-------------------------------+-------------------------------+ -| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | -+------------+---------------------------+-------------------------------+-------------------------------+ -| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | -| ParseAny | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | -| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | -| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | -| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | -+------------+---------------------------+-------------------------------+-------------------------------+ +Layout String: dateparse.ParseFormat() => 02-Jan-06 15:04:05 MST + +Your Using time.Local set to location=America/Denver MDT + ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | +| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | +| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = nil | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = timezone arg | 2017-07-19 04:21:00 -0600 MDT | 2017-07-19 10:21:00 +0000 UTC | +| ParseStrict | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MST | 2017-07-19 03:21:00 +0000 UTC | ++-------------+---------------------------+-------------------------------+-------------------------------------+ + # note, we are using America/New_York which doesn't recognize MDT so essentially ignores it @@ -92,19 +123,27 @@ $ dateparse --timezone="America/New_York" "Monday, 19-Jul-17 03:21:00 MDT" Your Current time.Local zone is PDT -+------------+---------------------------+-------------------------------+-------------------------------+ -| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | -+------------+---------------------------+-------------------------------+-------------------------------+ -| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 MDT | 2017-07-19 03:21:00 +0000 UTC | -+------------+---------------------------+-------------------------------+-------------------------------+ +Layout String: dateparse.ParseFormat() => 02-Jan-06 15:04:05 MDT + +Your Using time.Local set to location=America/New_York EDT + ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | ++-------------+---------------------------+-------------------------------+-------------------------------------+ +| ParseAny | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseAny | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC day=3 | +| ParseIn | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseIn | time.Local = timezone arg | 2017-07-19 03:21:00 -0400 EDT | 2017-07-19 07:21:00 +0000 UTC | +| ParseIn | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseLocal | time.Local = timezone arg | 2017-07-19 03:21:00 -0400 EDT | 2017-07-19 07:21:00 +0000 UTC | +| ParseLocal | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = nil | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = timezone arg | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +| ParseStrict | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | ++-------------+---------------------------+-------------------------------+-------------------------------------+ + ``` \ No newline at end of file diff --git a/dateparse/main.go b/dateparse/main.go index 36bd5f6..a3ffda6 100644 --- a/dateparse/main.go +++ b/dateparse/main.go @@ -48,7 +48,7 @@ func main() { } loc = l zonename, _ := time.Now().In(l).Zone() - fmt.Printf("Your Using time.Local set to location=%s %v \n", timezone, zonename) + fmt.Printf("\nYour Using time.Local set to location=%s %v \n", timezone, zonename) } fmt.Printf("\n") @@ -57,9 +57,10 @@ func main() { table.AddHeaders("method", "Zone Source", "Parsed", "Parsed: t.In(time.UTC)") parsers := map[string]parser{ - "ParseAny": parseAny, - "ParseIn": parseIn, - "ParseLocal": parseLocal, + "ParseAny": parseAny, + "ParseIn": parseIn, + "ParseLocal": parseLocal, + "ParseStrict": parseStrict, } for name, parser := range parsers { @@ -115,3 +116,14 @@ func parseAny(datestr string, loc *time.Location, utc bool) string { } return t.String() } + +func parseStrict(datestr string, loc *time.Location, utc bool) string { + t, err := dateparse.ParseStrict(datestr) + if err != nil { + return err.Error() + } + if utc { + return t.In(time.UTC).String() + } + return t.String() +} diff --git a/parseany.go b/parseany.go index fadc513..e6d4418 100644 --- a/parseany.go +++ b/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 diff --git a/parseany_test.go b/parseany_test.go index b982c28..f1cf6a7 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -547,3 +547,32 @@ func TestParseLayout(t *testing.T) { _, err = ParseFormat("2009-15-12T22:15Z") assert.NotEqual(t, nil, err) } + +var testParseStrict = []dateTest{ + // mm.dd.yyyy + {in: "3.3.2014"}, + // mm.dd.yy + {in: "08.09.71"}, + // mm/dd/yyyy + {in: "3/5/2014"}, + // mm/dd/yy + {in: "08/08/71"}, + {in: "8/8/71"}, + // mm/dd/yy hh:mm:ss + {in: "04/02/2014 04:08:09"}, + {in: "4/2/2014 04:08:09"}, +} + +func TestParseStrict(t *testing.T) { + + for _, th := range testParseStrict { + _, err := ParseStrict(th.in) + assert.NotEqual(t, nil, err) + } + + _, err := ParseStrict(`{"hello"}`) + assert.NotEqual(t, nil, err) + + _, err = ParseStrict("2009-08-12T22:15Z") + assert.Equal(t, nil, err) +} From d79052e5479f7970dbe71d22e251c99b20c56b3b Mon Sep 17 00:00:00 2001 From: Aaron Raddon Date: Sat, 24 Mar 2018 17:52:32 -0700 Subject: [PATCH 2/2] update readme for parse-strict error --- dateparse/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dateparse/README.md b/dateparse/README.md index c0f6ce6..cfc001c 100644 --- a/dateparse/README.md +++ b/dateparse/README.md @@ -144,6 +144,29 @@ Your Using time.Local set to location=America/New_York EDT | ParseStrict | time.Local = time.UTC | 2017-07-19 03:21:00 +0000 UTC | 2017-07-19 03:21:00 +0000 UTC | +-------------+---------------------------+-------------------------------+-------------------------------------+ +$ dateparse --timezone="America/New_York" "03/03/2017" +Your Current time.Local zone is PDT + +Layout String: dateparse.ParseFormat() => 01/02/2006 + +Your Using time.Local set to location=America/New_York EDT + ++-------------+---------------------------+----------------------------------------------------+----------------------------------------------------+ +| method | Zone Source | Parsed | Parsed: t.In(time.UTC) | ++-------------+---------------------------+----------------------------------------------------+----------------------------------------------------+ +| ParseIn | time.Local = nil | 2017-03-03 00:00:00 +0000 UTC | 2017-03-03 00:00:00 +0000 UTC | +| ParseIn | time.Local = timezone arg | 2017-03-03 00:00:00 -0500 EST | 2017-03-03 05:00:00 +0000 UTC | +| ParseIn | time.Local = time.UTC | 2017-03-03 00:00:00 +0000 UTC | 2017-03-03 00:00:00 +0000 UTC | +| ParseLocal | time.Local = nil | 2017-03-03 00:00:00 +0000 UTC | 2017-03-03 00:00:00 +0000 UTC | +| ParseLocal | time.Local = timezone arg | 2017-03-03 00:00:00 -0500 EST | 2017-03-03 05:00:00 +0000 UTC | +| ParseLocal | time.Local = time.UTC | 2017-03-03 00:00:00 +0000 UTC | 2017-03-03 00:00:00 +0000 UTC | +| ParseStrict | time.Local = nil | This date has ambiguous mm/dd vs dd/mm type format | This date has ambiguous mm/dd vs dd/mm type format | +| ParseStrict | time.Local = timezone arg | This date has ambiguous mm/dd vs dd/mm type format | This date has ambiguous mm/dd vs dd/mm type format | +| ParseStrict | time.Local = time.UTC | This date has ambiguous mm/dd vs dd/mm type format | This date has ambiguous mm/dd vs dd/mm type format | +| ParseAny | time.Local = nil | 2017-03-03 00:00:00 +0000 UTC | 2017-03-03 00:00:00 +0000 UTC day=5 | +| ParseAny | time.Local = timezone arg | 2017-03-03 00:00:00 +0000 UTC | 2017-03-03 00:00:00 +0000 UTC day=5 | +| ParseAny | time.Local = time.UTC | 2017-03-03 00:00:00 +0000 UTC | 2017-03-03 00:00:00 +0000 UTC day=5 | ++-------------+---------------------------+----------------------------------------------------+----------------------------------------------------+ ``` \ No newline at end of file