From aca798503a248d0a0afd1c211398b820c40a4959 Mon Sep 17 00:00:00 2001 From: troyspencer Date: Mon, 12 Aug 2019 20:38:23 -0500 Subject: [PATCH] Wrote tests for 100% coverage Update RetryAmbiguousDateWithSwap detection logic after testing --- parseany.go | 20 +++++++++++++------- parseany_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/parseany.go b/parseany.go index 1236b70..4945693 100644 --- a/parseany.go +++ b/parseany.go @@ -217,14 +217,20 @@ func parseTime(datestr string, loc *time.Location, opts ...ParserOption) (p *par // this is because it means that a day is being interpreted as a month and overflowing the valid value for that // by retrying in this case, we can fix a common situation with no assumptions defer func() { - if err != nil && strings.Contains(err.Error(), "month out of range") { - // create the option to reverse the preference - preferMonthFirst := PreferMonthFirst(!p.preferMonthFirst) - // turn off the retry to avoid endless recursion - retryAmbiguousDateWithSwap := RetryAmbiguousDateWithSwap(false) - modifiedOpts := append(opts, preferMonthFirst, retryAmbiguousDateWithSwap) - p, err = parseTime(datestr, time.Local, modifiedOpts...) + if p.ambiguousMD { + // if it errors out with the following error, swap before we + // get out of this function to reduce scope it needs to be applied on + _, err := p.parse() + if err != nil && strings.Contains(err.Error(), "month out of range") { + // create the option to reverse the preference + preferMonthFirst := PreferMonthFirst(!p.preferMonthFirst) + // turn off the retry to avoid endless recursion + retryAmbiguousDateWithSwap := RetryAmbiguousDateWithSwap(false) + modifiedOpts := append(opts, preferMonthFirst, retryAmbiguousDateWithSwap) + p, err = parseTime(datestr, time.Local, modifiedOpts...) + } } + }() } diff --git a/parseany_test.go b/parseany_test.go index 2d92f43..2d1383c 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -669,3 +669,38 @@ func TestInLocation(t *testing.T) { assert.Equal(t, zeroTime, ts.Unix()) assert.NotEqual(t, nil, err) } + +func TestPreferMonthFirst(t *testing.T) { + // default case is true + ts, err := ParseAny("04/02/2014 04:08:09 +0000 UTC") + assert.Equal(t, nil, err) + assert.Equal(t, "2014-04-02 04:08:09 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + + preferMonthFirstTrue := PreferMonthFirst(true) + ts, err = ParseAny("04/02/2014 04:08:09 +0000 UTC", preferMonthFirstTrue) + assert.Equal(t, nil, err) + assert.Equal(t, "2014-04-02 04:08:09 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) + + // allows the day to be preferred before the month, when completely ambiguous + preferMonthFirstFalse := PreferMonthFirst(false) + ts, err = ParseAny("04/02/2014 04:08:09 +0000 UTC", preferMonthFirstFalse) + assert.Equal(t, nil, err) + assert.Equal(t, "2014-02-04 04:08:09 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) +} + +func TestRetryAmbiguousDateWithSwap(t *testing.T) { + // default is false + _, err := ParseAny("13/02/2014 04:08:09 +0000 UTC") + assert.NotEqual(t, nil, err) + + // will fail error if the month preference cannot work due to the value being larger than 12 + retryAmbiguousDateWithSwapFalse := RetryAmbiguousDateWithSwap(false) + _, err = ParseAny("13/02/2014 04:08:09 +0000 UTC", retryAmbiguousDateWithSwapFalse) + assert.NotEqual(t, nil, err) + + // will retry with the other month preference if this error is detected + retryAmbiguousDateWithSwapTrue := RetryAmbiguousDateWithSwap(true) + ts, err := ParseAny("13/02/2014 04:08:09 +0000 UTC", retryAmbiguousDateWithSwapTrue) + assert.Equal(t, nil, err) + assert.Equal(t, "2014-02-13 04:08:09 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) +}