Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Callbacks and Timers » Date iterator
Date iterator [message #27482] Tue, 20 July 2010 11:16 Go to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Hi everybody,

I use U++ soonly and i have a question about date iteration.

An example:
Date datetmp, datemin(2009, 12, 31), datemax(2010, 12, 31);

for(datetmp = datemin; datetmp < datemax; datetmp.day++)
{
 ...
}



When i compile, datetmp.day becomes 2009-12-32 but not 2010-01-01.
Do i must implement another method to do that or there is an simplest manner to iterate with date?

Thank you
Re: Date iterator [message #27483 is a reply to message #27482] Tue, 20 July 2010 11:29 Go to previous messageGo to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
here is my response but it's not elegant!!


String ss;
Date datetmp, datemin(2009, 12, 31), datemax(2010, 12, 31);

for(datetmp = datemin; datetmp < datemax; )
{
  Time tm = ToTime(datetmp);
  tm += 24*3600;
  
  datetmp = (Date&) tm;

  ss = "";
  ss << datetmp;

  PrompOK(ss);
}


I need something like

datetmp.nexday();
or
datetmp.nextmonth(); ....
Re: Date iterator [message #27484 is a reply to message #27482] Tue, 20 July 2010 11:32 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
ratah wrote on Tue, 20 July 2010 11:16


Date datetmp, datemin(2009, 12, 31), datemax(2010, 12, 31);

for(datetmp = datemin; datetmp < datemax; datetmp.day++)
{
 ...
}




See TimeDate.h with Date definition:
Date& operator+=(Date& a, int b);


instead of datetmp.day++ (which does post-increment only day, not whole date), just do:

Date datetmp, datemin(2009, 12, 31), datemax(2010, 12, 31);

for(datetmp = datemin; datetmp < datemax; datetmp += 1 )
{
 ...
}

Re: Date iterator [message #27485 is a reply to message #27482] Tue, 20 July 2010 11:34 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
For next month just read the DateTime.h further.. Smile
Date AddMonths(Date date, int months);

So "datetmp = AddMonths( datetmp, 1 );" should work IMHO.
Re: Date iterator [message #27486 is a reply to message #27485] Tue, 20 July 2010 12:24 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3354
Registered: August 2008
Senior Veteran
Hello Ratah

Yes, follow mr_ped advide.

For example:
Date d(2009, 12, 31);

d += 1;

d will be the January 1st, 2010.

If you add an int to a Date, you add days.

If you add an int to a Time, you add seconds.


Best regards
IƱaki
Re: Date iterator [message #27487 is a reply to message #27486] Tue, 20 July 2010 14:18 Go to previous messageGo to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
mr_ped > koldo

Thank you,

I use another notation
datetmp++
instead of
datetmp += 1


and it is marked no match operator++ in ++datatmp

[Updated on: Tue, 20 July 2010 14:18]

Report message to a moderator

Re: Date iterator [message #27491 is a reply to message #27482] Wed, 21 July 2010 08:12 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
yes, "++" is not defined for Date class (in either form), use "+=" instead. Or define your own operator somewhere in your code (header? It's short stub which will be very likely inlined anyway), like this:
Date& operator++(Date& a) { return a += 1; }
Date& operator++(Date& a, int) { return a += 1; }
Date& operator--(Date& a) { return a -= 1; }
Date& operator--(Date& a, int) { return a -= 1; }
Re: Date iterator [message #27555 is a reply to message #27482] Fri, 23 July 2010 11:05 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
BTW, in cases like this one, it is often easier to work with scalar dates - use Get and Set to convert Date to/from number of dates since the start of the era.

Mirek
Previous Topic: PROPOSAL: delegate like separation of method and args in Callbacks
Next Topic: Time callbacks when ticks overlap
Goto Forum:
  


Current Time: Thu Mar 28 12:59:15 CET 2024

Total time taken to generate the page: 0.01738 seconds