Date range iteration

Jordan Wilson wilsonjord at gmail.com
Mon Mar 12 02:11:49 UTC 2018


I wanted to iterate through a date range, so I initially tried:
iota(Date(2016,1,1),Date(2018,1,1),dur!"days"(1));

That wouldn't compile, which is fair enough I guess.

So I tried a for loop:
for (auto i = Date(2016,1,1); i < Date(2018,1,1); 
i+=dur!"days"(1)){}

That seemed to work fine, but I remember reading "for loops are 
bad" somewhere. So I looked for a range type mechanism, and I 
think it's this:
foreach (i; Interval!Date 
(Date(2016,1,1),Date(2018,1,1)).fwdRange ( (a) { return 
a+dur!"days"(1); })){}

My question is if the third way is the proper way of stepping 
through a period of time? It just seems quite complicated to me.

Thanks,

Jordan


More information about the Digitalmars-d-learn mailing list