Boost logo

Boost Users :

From: Jeff Garland (jeff_at_[hidden])
Date: 2007-05-27 17:08:26


On Sun, 27 May 2007 21:06:09 +0200, Seweryn Habdank-Wojewódzki wrote
 
> So to summarize I know one exact date (clicked one), and I want to
> generate others. Below is small example:
>
> bg::date const final_date(2020,12,31);
> bg::months const every_month(1); // this TYPE is variable in runtime
> bg::date d0(2007,3,4);
> do
> {
> d0 += every_month;
> cout << d0 << '\n';
> }
> while (d0 < final_date);
>
> I am trying to use boost::variant for that purpose.

Ok, I would suggest you take a look at the date_iterator -- I think this will
provide the framework you need. The library provides basically 4 variations
on the date_iterator - month_iterator, year_iterator, week_iterator, and
day_iterator. These take a date as a starting point and will then calculate
the next increment. so you can change your code to something like:
     month_iterator itr(date(2007,3,4));

     //the following code is generic and would work with a date, week,
     //month, or year iterator.
     do
     {
         itr++;
         cout << *itr << '\n';
     }
     while (itr < final_date);

It turns out that you can use the base type
date_itr_base<boost::gregorian::date> to virtually dispatch. You can also
replace the functor in the iterators to create your own logic if you want.
You can look at the examples/test and source for more details.

Jeff


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net