Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2004-05-12 07:04:44

On Wed, 12 May 2004 11:07:25 +0200, Richard Peters wrote
> ----- Original Message -----
> From: "Jeff Garland" <jeff_at_[hidden]>
> > rules you want to apply. So for representing durations you currently only
> > have days, hours, minutes, and seconds.
> Such functionality would come very handy at times, though. Say you want
> something to occur every first day of the month. You'd want a start date
> which is the first day of a month, and increase that date with one
> month to reach the next month. This is currently very difficult,
> because the next date has to be calculated manually. It would be
> nice if a duration of a month or a year was available, although
> comparing the length of a month and the length of 30 days would be
> quite impossible.

Don't disagree, but I think we need to keep 'hard' in perspective.
Non-obvious yes, but not hard. The following will trivially do what you want

using namespace boost::gregorian;

add_years_months(date d, int years, int months)
  int total_months = years*12 + months;
  month_iterator mi(d, total_months);
  return *mi;


  date d = add_years_months(date(2003,Jan,1), 2, 3);
  std::cout << d << std::endl; //April 1 2005



Boost list run by bdawes at, gregod at, cpdaniel at, john at