From 863b55b5b57a223b0b8af47e234d88a925a1c6b1 Mon Sep 17 00:00:00 2001 From: Nick Shine Date: Wed, 3 Feb 2021 17:34:49 -0600 Subject: [PATCH] fix: more lenient time range parsing --- pkg/closures/closures.go | 11 ++++++++++- pkg/closures/closures_test.go | 10 ++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pkg/closures/closures.go b/pkg/closures/closures.go index 04f5ebc..f430ee0 100644 --- a/pkg/closures/closures.go +++ b/pkg/closures/closures.go @@ -50,6 +50,7 @@ const ( const ( closureLocation = "America/Chicago" timeLayout = "3:04 pm" + timeLayoutAlt = "Jan 2 - 3:04 pm" ) // Get pulls the current beach/road closures from https://www.cameroncounty.us/spacex/. @@ -172,7 +173,15 @@ func parseTimeRange(timeRange string) (*time.Time, *time.Time, error) { end, err := time.Parse(timeLayout, strings.TrimSpace(times[1])) if err != nil { - return nil, nil, err + // try alternate timeLayout + end, err = time.Parse(timeLayoutAlt, strings.TrimSpace(times[1])) + // fallback to midnight + if err != nil { + end, err = time.Parse(timeLayout, "11:59 pm") + if err != nil { + return &start, nil, err + } + } } return &start, &end, nil diff --git a/pkg/closures/closures_test.go b/pkg/closures/closures_test.go index 7a23c87..a170045 100644 --- a/pkg/closures/closures_test.go +++ b/pkg/closures/closures_test.go @@ -21,6 +21,11 @@ func newTime(t string) *time.Time { return &o } +func newTimeAlt(t string) *time.Time { + o, _ := time.Parse(timeLayoutAlt, t) + return &o +} + func TestGet(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() @@ -95,10 +100,11 @@ func TestParseTimeRange(t *testing.T) { }{ {"valid range", "9:00 am to 9:00 pm", newTime("9:00 am"), newTime("9:00 pm"), assert.NoError}, {"valid range 2", "8:00 am to 5:00 pm", newTime("8:00 am"), newTime("5:00 pm"), assert.NoError}, + {"valid range 3", "8:00 am to Feb 3 - 1:00 pm", newTime("8:00 am"), newTimeAlt("Feb 3 - 1:00 pm"), assert.NoError}, {"invalid range", "8:00 am to to 5:00 pm", nil, nil, assert.Error}, {"invalid range 2", "faketime to faketime", nil, nil, assert.Error}, - {"invalid range 3", "8:00 am to faketime", nil, nil, assert.Error}, - {"invalid range 3", "", nil, nil, assert.Error}, + {"invalid range 3", "8:00 am to faketime", newTime("8:00 am"), newTime("11:59 pm"), assert.NoError}, + {"invalid range 4", "", nil, nil, assert.Error}, } for _, tt := range tests {