mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
Wrote tests for 100% coverage
Update RetryAmbiguousDateWithSwap detection logic after testing
This commit is contained in:
parent
1bdd6d3fc9
commit
aca798503a
20
parseany.go
20
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
|
// 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
|
// by retrying in this case, we can fix a common situation with no assumptions
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil && strings.Contains(err.Error(), "month out of range") {
|
if p.ambiguousMD {
|
||||||
// create the option to reverse the preference
|
// if it errors out with the following error, swap before we
|
||||||
preferMonthFirst := PreferMonthFirst(!p.preferMonthFirst)
|
// get out of this function to reduce scope it needs to be applied on
|
||||||
// turn off the retry to avoid endless recursion
|
_, err := p.parse()
|
||||||
retryAmbiguousDateWithSwap := RetryAmbiguousDateWithSwap(false)
|
if err != nil && strings.Contains(err.Error(), "month out of range") {
|
||||||
modifiedOpts := append(opts, preferMonthFirst, retryAmbiguousDateWithSwap)
|
// create the option to reverse the preference
|
||||||
p, err = parseTime(datestr, time.Local, modifiedOpts...)
|
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...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -669,3 +669,38 @@ func TestInLocation(t *testing.T) {
|
|||||||
assert.Equal(t, zeroTime, ts.Unix())
|
assert.Equal(t, zeroTime, ts.Unix())
|
||||||
assert.NotEqual(t, nil, err)
|
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)))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user