# # # patch "dates.cc" # from [d84fe04e921485e907b2466e9b8247eddac74ceb] # to [ba11e025ca1a3524f6797956db69b89aba1655a5] # # patch "dates.hh" # from [89c3e1b55598ecb6580af784fe95b32b201aeb9d] # to [cc5126b4615d16165892a528031bb42f6993474f] # ============================================================ --- dates.cc d84fe04e921485e907b2466e9b8247eddac74ceb +++ dates.cc ba11e025ca1a3524f6797956db69b89aba1655a5 @@ -657,6 +657,25 @@ UNIT_TEST(date, from_unix_epoch) #undef OK } +UNIT_TEST(date, comparisons) +{ + date_t may = date_t::from_string("2000-05-01T00:00:00"), + jun = date_t::from_string("2000-06-01T00:00:00"), + jul = date_t::from_string("2000-07-01T00:00:00"); + + // testing all comparisons operators + UNIT_TEST_CHECK(may < jun); + UNIT_TEST_CHECK(jun < jul); + UNIT_TEST_CHECK(may < jul); + + UNIT_TEST_CHECK(jul > may); + + UNIT_TEST_CHECK(may == date_t::from_string("2000-05-01T00:00:00")); + UNIT_TEST_CHECK(may != date_t::from_string("2000-05-01T00:00:01")); + UNIT_TEST_CHECK(may != date_t::from_string("2000-09-01T00:00:00")); + UNIT_TEST_CHECK(may != date_t::from_string("1999-05-01T00:00:00")); +} + #endif // Local Variables: ============================================================ --- dates.hh 89c3e1b55598ecb6580af784fe95b32b201aeb9d +++ dates.hh cc5126b4615d16165892a528031bb42f6993474f @@ -43,6 +43,20 @@ struct date_t // Write out date as a string. std::string as_iso_8601_extended() const; + // Date comparison operators + bool operator <(struct date_t const & other) const + { return d < other.d; }; + bool operator <=(struct date_t const & other) const + { return d <= other.d; }; + bool operator >(struct date_t const & other) const + { return d > other.d; }; + bool operator >=(struct date_t const & other) const + { return d >= other.d; }; + bool operator ==(struct date_t const & other) const + { return d == other.d; }; + bool operator !=(struct date_t const & other) const + { return d != other.d; }; + private: // The date as an unsigned 64-bit count of seconds since the Unix epoch // (1970-01-01T00:00:00).