Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 11, 2015 Infinity loop with dates comparison | ||||
---|---|---|---|---|
| ||||
The code look very trivial, but I am getting infinity loop like: 2014-Aug-02 2014-Aug-02 2014-Aug-02 ... 2014-Aug-02 Date startDate = Date.fromISOExtString("2014-08-01"); Date currentDate = to!(Date)(Clock.currTime()-1.days); //because current day is not finished writeln(startDate); writeln(currentDate); Date nextday; while (nextday < currentDate) { nextday = startDate + 1.days; writeln(nextday); } |
August 11, 2015 Re: Infinity loop with dates comparison | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tuesday, 11 August 2015 at 19:56:02 UTC, Suliman wrote: > Date startDate = Date.fromISOExtString("2014-08-01"); [...] > Date nextday; > > while (nextday < currentDate) > { > nextday = startDate + 1.days; > writeln(nextday); > } startDate doesn't change, so every iteration just sets nextday to 2014-08-01 + 1 day = 2014-08-02. |
August 11, 2015 Re: Infinity loop with dates comparison | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tuesday, 11 August 2015 at 19:56:02 UTC, Suliman wrote:
> The code look very trivial, but I am getting infinity loop like:
> 2014-Aug-02
> 2014-Aug-02
> 2014-Aug-02
> ...
> 2014-Aug-02
>
>
> Date startDate = Date.fromISOExtString("2014-08-01");
>
> Date currentDate = to!(Date)(Clock.currTime()-1.days); //because current day is not finished
>
> writeln(startDate);
> writeln(currentDate);
>
> Date nextday;
>
> while (nextday < currentDate)
> {
> nextday = startDate + 1.days;
> writeln(nextday);
> }
This isn't a D problem, you just always set nextday to the same value that doesn't change (startDate + 1.days).
Maybe what you meant was:
nextday = startDate;
while (nextday < currentDate)
{
nextday = nextday + 1.days;
writeln(nextday);
}
|
August 11, 2015 Re: Infinity loop with dates comparison | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tue, Aug 11, 2015 at 07:56:00PM +0000, Suliman via Digitalmars-d-learn wrote: [...] > Date startDate = Date.fromISOExtString("2014-08-01"); > > Date currentDate = to!(Date)(Clock.currTime()-1.days); //because current > day is not finished > > writeln(startDate); > writeln(currentDate); > > Date nextday; > > while (nextday < currentDate) > { > nextday = startDate + 1.days; ^^^^^^^^^ Because you're always computing from startDate, which is constant. That should be 'nextday' instead. > writeln(nextday); > } T -- Making non-nullable pointers is just plugging one hole in a cheese grater. -- Walter Bright |
Copyright © 1999-2021 by the D Language Foundation