Skip to content

Commit

Permalink
Merge pull request #1261 from nyaruka/fix-date-parsing
Browse files Browse the repository at this point in the history
Improve date parsing
  • Loading branch information
rowanseymour authored May 24, 2024
2 parents fafa7e0 + 32f3bdb commit 2cd3dba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions envs/dates.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func dateFromFormats(currentYear int, pattern *regexp.Regexp, d int, m int, y in

remainder := str[match[1]:]

_, err := time.Parse("2006-01-02", fmt.Sprintf("%d-%02d-%02d", year, month, day))
if err != nil {
return dates.ZeroDate, str, fmt.Errorf("string '%s' couldn't be parsed as a date", str)
}

// looks believable, go for it
return dates.NewDate(year, month, day), remainder, nil
}
Expand Down
4 changes: 4 additions & 0 deletions flows/routers/cases/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ var testTests = []struct {
{"has_number_between", dmy, []types.XValue{xs("a string"), xs("10"), xs("not number")}, ERROR},
{"has_number_between", dmy, []types.XValue{}, ERROR},

{"has_date", dmy, []types.XValue{xs("31/02/2022")}, falseResult},
{"has_date", dmy, []types.XValue{xs("33/11/2022")}, falseResult},
{"has_date", dmy, []types.XValue{xs("31/11/2022")}, falseResult},
{"has_date", dmy, []types.XValue{xs("30/02/2022")}, falseResult},
{"has_date", dmy, []types.XValue{xs("last date was 1.10.2017")}, result(xd(time.Date(2017, 10, 1, 15, 24, 30, 123456000, kgl)))},
{"has_date", dmy, []types.XValue{xs("last date was 1.10.99")}, result(xd(time.Date(1999, 10, 1, 15, 24, 30, 123456000, kgl)))},
{"has_date", dmy, []types.XValue{xs("this isn't a valid date 33.2.99")}, falseResult},
Expand Down

0 comments on commit 2cd3dba

Please sign in to comment.