Boost logo

Boost :

From: Andrew Webber (boost_at_[hidden])
Date: 2007-05-31 13:02:33


Thanks Jeff. I'm extremely happy to contribute (even just a little
bit) to a boost library.

I have a new patch for you. I intend for this to supercede the old
patch. The iterators may still not be "fully compliant", but I've
also added the <, <=. >, and >= operators. I apologize for the tabs
in my code... I don't know of a great way to convert them to spaces at
this point.

Index: C:/My Documents/Code/Code
Library/Boost/include/boost-1_34/boost/date_time/date_iterator.hpp
===================================================================
--- C:/My Documents/Code/Code
Library/Boost/include/boost-1_34/boost/date_time/date_iterator.hpp
(revision 592)
+++ C:/My Documents/Code/Code
Library/Boost/include/boost-1_34/boost/date_time/date_iterator.hpp
(working copy)
@@ -51,6 +51,12 @@
       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 current_
!= rhs.current_; }
+ bool operator< (const date_itr_base& rhs) { return current_ <
rhs.current_; }
+ bool operator<= (const date_itr_base& rhs) { return current_
<= rhs.current_; }
+ bool operator> (const date_itr_base& rhs) { return current_ >
rhs.current_; }
+ bool operator>= (const date_itr_base& rhs) { return current_
>= rhs.current_; }
     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 +87,20 @@
       date_itr_base<date_type>(d),
       of_(factor)
     {}
+ 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;
+ }
   private:
     virtual duration_type get_offset(const date_type& current) const
     {

Also, here is some demo code for all of the newly added operators.

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";
    }
    else
    {
        std::cout << "day1 and day2 not equal\n";
    }

    if(day1 == month1)
    {
        std::cout << "day iterator == month iterator\n";
    }
    ++month1;
    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";

    boost::gregorian::day_iterator today_iter(today);
    boost::gregorian::day_iterator tomorrow_iter(today +
boost::gregorian::days(1));

    if(today_iter < tomorrow_iter)
    {
        std::cout << *today_iter << " is less than " << *tomorrow_iter << "\n";
    }
    else
    {
        std::cout << *today_iter << " is not less than " <<
*tomorrow_iter << "\n";
    }

    if(today_iter <= today_iter)
    {
        std::cout << *today_iter << " is less than or equal to " <<
*today_iter << "\n";
    }
    else
    {
        std::cout << *today_iter << " is not less than or equal to "
<< *today_iter << "\n";
    }

    if(today_iter <= tomorrow_iter)
    {
        std::cout << *today_iter << " is less than or equal to " <<
*tomorrow_iter << "\n";
    }
    else
    {
        std::cout << *today_iter << " is not less than or equal to "
<< *tomorrow_iter << "\n";
    }

    if(today_iter > tomorrow_iter)
    {
        std::cout << *today_iter << " is greater than " <<
*tomorrow_iter << "\n";
    }
    else
    {
        std::cout << *today_iter << " is not greater than " <<
*tomorrow_iter << "\n";
    }

    if(today_iter >= today_iter)
    {
        std::cout << *today_iter << " is greater than or equal to " <<
*today_iter << "\n";
    }
    else
    {
        std::cout << *today_iter << " is not greater than or equal to
" << *today_iter << "\n";
    }

    if(today_iter >= tomorrow_iter)
    {
        std::cout << *today_iter << " is greater than or equal to " <<
*tomorrow_iter << "\n";
    }
    else
    {
        std::cout << *today_iter << " is not greater than or equal to
" << *tomorrow_iter << "\n";
    }
    std::cout << "\n";

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

Please let me know if there is anything else I should add. Thanks!

Andy

On 5/31/07, Jeff Garland <jeff_at_[hidden]> wrote:
> Andrew Webber wrote:
> > 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.
>
> Thx :-)
>
> > 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.
>
> This has been on the todo list for way to long...
>
> > 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.
>
> Not at all.
>
> > 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.
> >
> >...snip details...
> >
> > Is this something that may be considered for inclusion in the date_time library?
>
> Absolutely -- patches are always the best way to get something into the
> library :-) I think there's actually a couple more things to get the
> iterators up to full compliance, but I have no problem moving things along.
> So I'll check your fixes into the head...probably be a month or so before I
> get the cycles to do the work.
>
> Thx!
>
> Jeff
>
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>


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