www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Infinity loop with dates comparison

reply "Suliman" <evermind live.ru> writes:
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);
	}
Aug 11 2015
next sibling parent "anonymous" <anonymous example.com> writes:
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.
Aug 11 2015
prev sibling next sibling parent "cym13" <cpicard openmailbox.org> writes:
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); }
Aug 11 2015
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
Aug 11 2015