From 96c3b2ff300003fa5ea70d50ad471822bf6d3ec4 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 13:12:13 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Use=20ParseDate=20in=20Parse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also limit to English, or we get lots of false positives --- tools/parse.go | 3 +-- tools/parse_date.go | 1 + tools/parse_test.go | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/parse.go b/tools/parse.go index 38d7b5e..021c6a2 100644 --- a/tools/parse.go +++ b/tools/parse.go @@ -5,7 +5,6 @@ import ( "regexp" "strconv" "strings" - "time" ) func ParseBytes(in []byte) any { @@ -34,7 +33,7 @@ func ParseString(in string) any { return i } else if f, err := strconv.ParseFloat(s, 64); err == nil { return f - } else if t, err := time.Parse(time.RFC3339, s); err == nil { + } else if t, err := ParseDate(s); err == nil { return t } else if err := json.Unmarshal([]byte(s), &j); err == nil { return j diff --git a/tools/parse_date.go b/tools/parse_date.go index e110f0f..d060299 100644 --- a/tools/parse_date.go +++ b/tools/parse_date.go @@ -26,6 +26,7 @@ func ParseDate(in string) (t time.Time, err error) { conf := dp.Configuration{ CurrentTime: time.Now().Local(), ReturnTimeAsPeriod: true, + Languages: []string{"en"}, } d, err := dp.Parse(&conf, in) diff --git a/tools/parse_test.go b/tools/parse_test.go index 2e8e49f..826af9b 100644 --- a/tools/parse_test.go +++ b/tools/parse_test.go @@ -10,6 +10,7 @@ import ( func TestParse(t *testing.T) { when := time.Now() + now := when.Local().Truncate(time.Second) tests := []struct { name string in string @@ -22,6 +23,8 @@ func TestParse(t *testing.T) { {"false", "false", false}, {"nil", "nil", nil}, {"time", when.Format(time.RFC3339), when}, + {"now", "now", now}, + {"DateFormat", now.Format(DateFormat), now}, {"json-obj", `{"foo":"bar","baz":"quux"}`, json.RawMessage(`{"foo":"bar","baz":"quux"}`)}, {"json-arr", `["foo",42,"bar", null,"quux", true]`, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)}, {"empty", "", ""}, @@ -48,7 +51,7 @@ func getParseTestRunner(in string, exp any) func(*testing.T) { if expT, ok := exp.(time.Time); ok { ti, gotTime := out.(time.Time) if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") { - assert.WithinRange(t, expT, ti.Add(-time.Second), ti.Add(time.Second)) + assert.WithinRange(t, expT, ti.Add(-2*time.Second), ti.Add(2*time.Second)) } } else { assert.Equal(t, exp, out) @@ -57,7 +60,7 @@ func getParseTestRunner(in string, exp any) func(*testing.T) { if expT, ok := exp.(time.Time); ok { ti, gotTime := out.(time.Time) if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") { - assert.WithinRange(t, expT, ti.Add(-time.Second), ti.Add(time.Second)) + assert.WithinRange(t, expT, ti.Add(-2*time.Second), ti.Add(2*time.Second)) } } else { assert.Equal(t, exp, out)