From e94589d6b53fb6f37146b738d49ec950fedcb62e Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 7 Sep 2023 04:41:41 +0200 Subject: [PATCH] add a Date::CompareDate(...) and Date::CompareTime(...) methods --- src/date/date.cpp | 26 +++++++++++++++++++++++--- src/date/date.h | 7 +++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/date/date.cpp b/src/date/date.cpp index 5252d44..4ae6062 100644 --- a/src/date/date.cpp +++ b/src/date/date.cpp @@ -241,9 +241,9 @@ bool Date::operator!=(const Date & d) const } -int Date::Compare(const Date & d) const +int Date::CompareDate(const Date & d, bool ignore_year) const { - if( year != d.year ) + if( !ignore_year && year != d.year ) return year - d.year; if( month != d.month ) @@ -252,6 +252,13 @@ int Date::Compare(const Date & d) const if( day != d.day ) return day - d.day; + // dates are equal + return 0; +} + + +int Date::CompareTime(const Date & d) const +{ if( hour != d.hour ) return hour - d.hour; @@ -261,11 +268,24 @@ int Date::Compare(const Date & d) const if( sec != d.sec ) return sec - d.sec; - // dates are equal + // times are equal return 0; } +int Date::Compare(const Date & d, bool ignore_year) const +{ + int res = CompareDate(d, ignore_year); + + if( res == 0 ) + { + res = CompareTime(d); + } + + return res; +} + + bool Date::operator>(const Date & d) const { return Compare(d) > 0; diff --git a/src/date/date.h b/src/date/date.h index 9da4f93..2e487e3 100644 --- a/src/date/date.h +++ b/src/date/date.h @@ -161,12 +161,15 @@ public: time_t operator-(const Date & d) const; + /* - 'Compare' returns zero if this and d are equal + 'Compare' methods returns zero if this and d are equal return value less than zero if this is lower than d and a value greater than zero if this is greater than d */ - int Compare(const Date & d) const; + int CompareDate(const Date & d, bool ignore_year = false) const; + int CompareTime(const Date & d) const; + int Compare(const Date & d, bool ignore_year = false) const; /*