Are there any plans for date slices to do something like
dt in '2017-03-26'..'2017-04-25': # enumerate days
dt in '2016-03'..'2017-03': # enumerate months
I've shown this as strings, but it could be via some helper function, or ...., but the real question is what the nim way to easily work with ranges of dates, and be able to step by different amounts (days, months, years, ...)
and similarly, how to do this with date/times, and with just times, ....
Like this?
import times
iterator `..`*(ds1, ds2: string): TimeInfo =
var
d1 = ds1.parse("yyyy-MM-dd")
d2 = ds2.parse("yyyy-MM-dd")
var interval: TimeInterval
# yield day-by-day
if d1.monthday != d2.monthday:
interval = initInterval(hours=24)
# yield month-by-month
else:
interval = initInterval(months=1)
while d1.toTime <= d2.toTime:
yield d1
d1 = d1 + interval
when isMainModule:
echo "enumerate days"
for dt in "2017-03-10" .. "2017-03-15":
echo dt
echo()
echo "enumerate months"
for dt in "2017-03-10" .. "2017-10-10":
echo dt