Boost logo

Boost :

From: Andrew Webber (boost_at_[hidden])
Date: 2007-05-30 12:29:03

I recently set out to evaluate boost::date_time for use in a large
commercial project. I was extremely impressed with the large
feature-set and the obvious depth of thought put into the library.
However, I was a little upset about some small features missing from
day_iterator, month_iterator, and year_iterator. Specifically, I
wished for equality, inequality, post-increment and post-decrement
operators. The inequality operator in particular would add the
ability to use this group of iterators with many of the standard
algorithms such as std::copy.

I'm very new to the boost developers mailing list, so please forgive
me and inform me if I'm breaking protocol in some way here.

I went ahead and implemented the missing iterator operators.
Interestingly, because the design of the iterators, the equality and
inequality operators work between different types of iterators (day,
month or year). I've also added some demo code to test out the new
features. It's definitely not a unit test, but at least you can see
that the operators are working. I've successfully compiled the new
boost::date_time code and demo code under Visual Studio .NET 2003. If
I have time, I will also test the code on gcc 3.4 and gcc 4.0.

Here is a patch file of the changes to boost/date_time/date_iterator.hpp:
Index: C:/My Documents/Code/Code
--- C:/My Documents/Code/Code
(revision 592)
+++ C:/My Documents/Code/Code
(working copy)
@@ -51,6 +51,14 @@
       current_ = current_ + get_neg_offset(current_);
       return *this;
+ bool operator==(const date_itr_base& rhs)
+ {
+ return (current_ == rhs.current_);
+ }
+ bool operator!=(const date_itr_base& rhs)
+ {
+ return !(*this == rhs);
+ }
     virtual duration_type get_offset(const date_type& current) const=0;
     virtual duration_type get_neg_offset(const date_type& current) const=0;
     date_type operator*() {return current_;};
@@ -81,6 +89,20 @@
+ using date_itr_base<date_type>::operator++;
+ const date_itr operator++(int)
+ {
+ date_itr temp(*this);
+ ++*this;
+ return *temp;
+ }
+ using date_itr_base<date_type>::operator--;
+ const date_itr operator--(int)
+ {
+ date_itr temp(*this);
+ --*this;
+ return *temp;
+ }
     virtual duration_type get_offset(const date_type& current) const

Here is the test code:
void date_time_example()
    boost::gregorian::date today(boost::gregorian::day_clock::universal_day());

    boost::gregorian::day_iterator day1(today);
    boost::gregorian::day_iterator day2(today + boost::gregorian::days(10));
    boost::gregorian::month_iterator month1(today);

    if(day1 == day2)
        std::cout << "day1 and day2 equal\n\n";
        std::cout << "day1 and day2 not equal\n";

    if(day1 == month1)
        std::cout << "day iterator == month iterator\n";
    if(day1 != month1)
        std::cout << "now day iterator != month iterator\n";
    std::cout << "\n";

    boost::gregorian::day_iterator day3(day1);

    std::cout << "day3 and day2 not equal\n";
    std::cout << *day3 << " ";
    std::cout << *day2 << "\n";
    day3 = day2++;
    std::cout << "day3 and day2 after day2 postincrement\n";
    std::cout << *day3 << " ";
    std::cout << *day2 << "\n";
    day3 = day2--;
    std::cout << "day3 and day2 after day2 postdecrement\n";
    std::cout << *day3 << " ";
    std::cout << *day2 << "\n\n";

    std::cout << "Now std::copy works because of != operator\n";
    std::copy(day1, day2,
std::ostream_iterator<boost::gregorian::date>(std::cout, "\n"));

Is this something that may be considered for inclusion in the date_time library?

  Andy Webber

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