From d47efc42ff7aa42a2e19449f034a61b2d0808471 Mon Sep 17 00:00:00 2001 From: "Kevin B. McCarty" Date: Sat, 9 May 2015 19:28:04 +0200 Subject: [PATCH 1/5] Remove redundant IsValid() checks from wxDateTime comparison methods They're not necessary as GetValue() asserts when passed an invalid object anyhow, there is no need to check for this twice. --- include/wx/datetime.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/include/wx/datetime.h b/include/wx/datetime.h index be8bbdc30a..e6f438187d 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -816,37 +816,31 @@ public: inline bool operator<(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() < dt.GetValue(); } inline bool operator<=(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() <= dt.GetValue(); } inline bool operator>(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() > dt.GetValue(); } inline bool operator>=(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() >= dt.GetValue(); } inline bool operator==(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() == dt.GetValue(); } inline bool operator!=(const wxDateTime& dt) const { - wxASSERT_MSG( IsValid() && dt.IsValid(), wxT("invalid wxDateTime") ); return GetValue() != dt.GetValue(); } From fda904ea77e686fd3eb811de01759e27fa3545ed Mon Sep 17 00:00:00 2001 From: "Kevin B. McCarty" Date: Sat, 9 May 2015 19:28:04 +0200 Subject: [PATCH 2/5] Implement wxDateTime::Is{EqualTo,{Earlier,Later}Than}() in terms of operators Avoid duplicating the same code, even if it's trivial, in both places. Notice that these functions are implemented in terms of operators and not vice versa because we have no functions corresponding to operator<=() or operator>=(). --- include/wx/datetime.h | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/include/wx/datetime.h b/include/wx/datetime.h index e6f438187d..6375f06a1a 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -1768,23 +1768,17 @@ inline wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const inline bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); - - return m_time == datetime.m_time; + return *this == datetime; } inline bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); - - return m_time < datetime.m_time; + return *this < datetime; } inline bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const { - wxASSERT_MSG( IsValid() && datetime.IsValid(), wxT("invalid wxDateTime")); - - return m_time > datetime.m_time; + return *this > datetime; } inline bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1, From 56aa94c61e1cb47271aa02c5be99b7945a7010ea Mon Sep 17 00:00:00 2001 From: "Kevin B. McCarty" Date: Sat, 9 May 2015 19:28:04 +0200 Subject: [PATCH 3/5] Allow using wxDateTime::operator==() and !=() with invalid objects Unlike the other operators, we comparing for equality has a well-defined semantics even for invalid objects, so there doesn't seem any reason to not allow it. --- include/wx/datetime.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/wx/datetime.h b/include/wx/datetime.h index 6375f06a1a..c3cedad7a4 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -836,12 +836,15 @@ public: inline bool operator==(const wxDateTime& dt) const { - return GetValue() == dt.GetValue(); + // Intentionally do not call GetValue() here, in order that + // invalid wxDateTimes may be compared for equality + return m_time == dt.m_time; } inline bool operator!=(const wxDateTime& dt) const { - return GetValue() != dt.GetValue(); + // As above, don't use GetValue() here. + return m_time != dt.m_time; } // arithmetics with dates (see also below for more operators) From 5e5d8721504c05b275a26bfee093aefd22242b81 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 12 Apr 2016 20:51:09 +0200 Subject: [PATCH 4/5] Document wxDateTime comparison operators behaviour Mention that they intentionally can't be used with invalid objects except for the exact comparison. --- interface/wx/datetime.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/interface/wx/datetime.h b/interface/wx/datetime.h index e8349bb094..82624aa87b 100644 --- a/interface/wx/datetime.h +++ b/interface/wx/datetime.h @@ -645,7 +645,14 @@ public: @name Date Comparison There are several functions to allow date comparison. To supplement - them, a few global operators, etc taking wxDateTime are defined. + them, the usual comparison operators taking wxDateTime are defined as + well. + + Notice that an invalid wxDateTime object can only be compared for + exact equality, i.e. using @c operator==(), @c operator!=() or + IsEqualTo(), but comparisons involving an invalid wxDateTime object + using any other operators or IsEarlierThan() or IsLaterThan() functions + would result in an assert because their result is not well-defined. */ //@{ From a922d576ec0a24652b440b9424ab74a5bc4b21aa Mon Sep 17 00:00:00 2001 From: "Kevin B. McCarty" Date: Sat, 9 May 2015 19:28:04 +0200 Subject: [PATCH 5/5] Use wxINT64_MIN for the value of invalid wxDateTime Use 0xffffffffffffffff instead of the strange 0xffffffff00000000 that we used before, for some reason. --- include/wx/datetime.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/wx/datetime.h b/include/wx/datetime.h index c3cedad7a4..89215177c9 100644 --- a/include/wx/datetime.h +++ b/include/wx/datetime.h @@ -450,7 +450,7 @@ public: // ------------------------------------------------------------------------ // default ctor does not initialize the object, use Set()! - wxDateTime() { m_time = wxLongLong(wxINT32_MIN, 0); } + wxDateTime() { m_time = wxINT64_MIN; } // from time_t: seconds since the Epoch 00:00:00 UTC, Jan 1, 1970) inline wxDateTime(time_t timet); @@ -704,7 +704,7 @@ public: // ------------------------------------------------------------------------ // is the date valid? - inline bool IsValid() const { return m_time != wxInvalidDateTime.m_time; } + inline bool IsValid() const { return m_time != wxLongLong(wxINT64_MIN); } // get the broken down date/time representation in the given timezone //