diff --git a/src/libqof/qof/gnc-datetime.cpp b/src/libqof/qof/gnc-datetime.cpp index 6191cf72f2..2a193ae084 100644 --- a/src/libqof/qof/gnc-datetime.cpp +++ b/src/libqof/qof/gnc-datetime.cpp @@ -213,6 +213,12 @@ private: Date m_greg; friend GncDateTimeImpl::GncDateTimeImpl(const GncDateImpl&, DayPart); + friend bool operator<(const GncDateImpl&, const GncDateImpl&); + friend bool operator>(const GncDateImpl&, const GncDateImpl&); + friend bool operator==(const GncDateImpl&, const GncDateImpl&); + friend bool operator<=(const GncDateImpl&, const GncDateImpl&); + friend bool operator>=(const GncDateImpl&, const GncDateImpl&); + friend bool operator!=(const GncDateImpl&, const GncDateImpl&); }; /* Member function definitions for GncDateTimeImpl. @@ -427,6 +433,13 @@ GncDateImpl::format(const char* format) const return ss.str(); } +bool operator<(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg < b.m_greg; } +bool operator>(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg > b.m_greg; } +bool operator==(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg == b.m_greg; } +bool operator<=(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg <= b.m_greg; } +bool operator>=(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg >= b.m_greg; } +bool operator!=(const GncDateImpl& a, const GncDateImpl& b) { return a.m_greg != b.m_greg; } + /* =================== Presentation-class Implementations ====================*/ /* GncDateTime */ @@ -519,3 +532,10 @@ GncDate::year_month_day() const { return m_impl->year_month_day(); } + +bool operator<(const GncDate& a, const GncDate& b) { return *(a.m_impl) < *(b.m_impl); } +bool operator>(const GncDate& a, const GncDate& b) { return *(a.m_impl) > *(b.m_impl); } +bool operator==(const GncDate& a, const GncDate& b) { return *(a.m_impl) == *(b.m_impl); } +bool operator<=(const GncDate& a, const GncDate& b) { return *(a.m_impl) <= *(b.m_impl); } +bool operator>=(const GncDate& a, const GncDate& b) { return *(a.m_impl) >= *(b.m_impl); } +bool operator!=(const GncDate& a, const GncDate& b) { return *(a.m_impl) != *(b.m_impl); } diff --git a/src/libqof/qof/gnc-datetime.hpp b/src/libqof/qof/gnc-datetime.hpp index 59fb0594b3..2d879d6b6d 100644 --- a/src/libqof/qof/gnc-datetime.hpp +++ b/src/libqof/qof/gnc-datetime.hpp @@ -270,6 +270,23 @@ private: std::unique_ptr m_impl; friend GncDateTime::GncDateTime(const GncDate&, DayPart); + friend bool operator<(const GncDate&, const GncDate&); + friend bool operator>(const GncDate&, const GncDate&); + friend bool operator==(const GncDate&, const GncDate&); + friend bool operator<=(const GncDate&, const GncDate&); + friend bool operator>=(const GncDate&, const GncDate&); + friend bool operator!=(const GncDate&, const GncDate&); }; +/**@{ + * Standard comparison operators working on GncDate objects. + */ +bool operator<(const GncDate& a, const GncDate& b); +bool operator>(const GncDate& a, const GncDate& b); +bool operator==(const GncDate& a, const GncDate& b); +bool operator<=(const GncDate& a, const GncDate& b); +bool operator>=(const GncDate& a, const GncDate& b); +bool operator!=(const GncDate& a, const GncDate& b); +/**@}*/ + #endif // __GNC_DATETIME_HPP__ diff --git a/src/libqof/qof/test/gtest-gnc-datetime.cpp b/src/libqof/qof/test/gtest-gnc-datetime.cpp index 0730bd3999..b914896eb7 100644 --- a/src/libqof/qof/test/gtest-gnc-datetime.cpp +++ b/src/libqof/qof/test/gtest-gnc-datetime.cpp @@ -183,6 +183,38 @@ TEST(gnc_date_constructors, test_str_format_constructor) } } +TEST(gnc_date_operators, test_equality) +{ + GncDate a(2017, 1, 6); + GncDate b(2017, 1, 6); + GncDate c(2015, 6, 13); + EXPECT_TRUE (a == b); + EXPECT_FALSE (a == c); + EXPECT_TRUE (a != c); + EXPECT_FALSE (a != b); +} + +TEST(gnc_date_operators, test_more_less_than) +{ + GncDate a(2017, 1, 6); + GncDate b(2017, 1, 6); + GncDate c(2015, 6, 13); + EXPECT_TRUE (a >= b); + EXPECT_TRUE (a <= b); + EXPECT_FALSE (a > b); + EXPECT_FALSE (a < b); + + EXPECT_TRUE (a > c); + EXPECT_TRUE (a >= c); + EXPECT_FALSE (a < c); + EXPECT_FALSE (a <= c); + + EXPECT_TRUE (c < a); + EXPECT_TRUE (c <= a); + EXPECT_FALSE (c > a); + EXPECT_FALSE (c >= a); +} + TEST(gnc_datetime_constructors, test_default_constructor) { GncDateTime atime;