Boost logo

Boost :

From: Val Samko (boost_at_[hidden])
Date: 2004-04-10 23:27:07


Jeff,

Saturday, April 10, 2004, 4:00:13 PM, you wrote:

JG> date d2(2004,Jan,31);
JG> date r2 = add_month(d2,1); //presumably we want to back up to Feb 29?
JG> date r3 = add_month(d2,13); //presumably we want to back up to Feb 28?
I think it is natural to back up to the last day of the month,
if the day we have (i.e. 31 in this case) is not present in that
month.

JG> date first_date_of_month(const date& orig)
JG> date last_date_of_month(const date& orig)
JG> date add_month(const date& d, int months)

JG> Of course the other option would be to include these in the date itself so you
JG> could write:
JG> date d(2004, Jan, 10);
JG> date fd = d.first_date_of_month();
JG> date ld = d.last_date_of_month();

I think, the first approach (non member functions) is better, since
a) these function do not need to know the implementation details of
the date class
b) having some other date class I could easily implement equivalent
functions, keeping the syntax consistent.

JG> For add month we could do:
JG> date next = ld.add_month(13);
JG> although the prefered syntax for add_month would actually be something like:
JG> date next = ld + months(13);

JG> There are some technical complications with the last syntax, but it should be
JG> doable given some time ;-) Thoughts?
what sort of complications?
   struct months { months(int m) : mo(m){}; int mo; };
   date operator + (const date& d, const month& m) { return add_month(d, m.mo); }

Anyway, I think, three namespace level functions would be sufficient.

Val Samko
http://val.digiways.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk