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]>
>...snip...
> > 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;

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

int
main()
{

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

}

Jeff


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