The task seemed easy enough: given a start and end date, generate all dates between them. I came up with code that works, but it feels hacky. For example, to find all dates between today, 2020-12-29, and January 6th next year, 2021-01-06, I would write:
between(0, inf, X),
Day is 29+X,
format_time(string(Date), "%F", date(2020, 12, Day)),
( Date @>= "2021-01-06" -> ! ; true )
What bothers me:
- the
inf
in thebetween
, this is begging for trouble? - the conditional. I actually got it right by trial and error, which means that I am not a good programmer, but could also mean that others might have trouble deciphering the meaning if they read the code. (comments might help)
The other approach was to do something like this:
date_time_stamp(date(2020, 12, 29), From),
date_time_stamp(date(2021, 1, 6), To),
Days_between is round((To - From) / (24 * 60 * 60)),
between(0, Days_between, X),
Day is 29 + X,
format_time(string(Date), "%F", date(2020, 12, Day))
… but I am not sure if this is an improvement.
Any idea how to make this cleaner or more readable? Any problems with any of the two approaches?
This is difficult because I don’t know how to easily calculate the number of days between two dates. I also don’t know if the calculation I do in the second example is always correct.