Skip to content

Commit

Permalink
Merge pull request #63 from nickshine/time-parse-fix
Browse files Browse the repository at this point in the history
fix: more lenient time range parsing
  • Loading branch information
nickshine authored Feb 3, 2021
2 parents c7ea33b + 863b55b commit 8401fad
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pkg/closures/closures.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/.
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/closures/closures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8401fad

Please sign in to comment.