From 52bf09eaf7faa2c69217dd37e08b6c372e47cf21 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 4 Feb 2018 07:43:11 +0100 Subject: [PATCH] Ref T241, some improvements for timestamp based * this->m_ => m_ * is[Older/Newer]ThanAdjusted --- src/blackmisc/timestampbased.cpp | 58 ++++++++++++++++++--------- src/blackmisc/timestampbased.h | 12 ++++++ src/blackmisc/timestampobjectlist.cpp | 16 ++++---- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/blackmisc/timestampbased.cpp b/src/blackmisc/timestampbased.cpp index 874d2521a..d7a135b2a 100644 --- a/src/blackmisc/timestampbased.cpp +++ b/src/blackmisc/timestampbased.cpp @@ -30,13 +30,13 @@ namespace BlackMisc QDateTime ITimestampBased::getUtcTimestamp() const { - if (this->m_timestampMSecsSinceEpoch < 0) { return QDateTime(); } - return QDateTime::fromMSecsSinceEpoch(this->m_timestampMSecsSinceEpoch); + if (m_timestampMSecsSinceEpoch < 0) { return QDateTime(); } + return QDateTime::fromMSecsSinceEpoch(m_timestampMSecsSinceEpoch); } void ITimestampBased::setTimestampToNull() { - this->m_timestampMSecsSinceEpoch = -1; + m_timestampMSecsSinceEpoch = -1; } void ITimestampBased::setByYearMonthDayHourMinute(const QString &yyyyMMddhhmmsszzz) @@ -76,48 +76,48 @@ namespace BlackMisc { if (timestamp.isValid()) { - this->m_timestampMSecsSinceEpoch = timestamp.toMSecsSinceEpoch(); + m_timestampMSecsSinceEpoch = timestamp.toMSecsSinceEpoch(); } else { - this->m_timestampMSecsSinceEpoch = -1; // invalid + m_timestampMSecsSinceEpoch = -1; // invalid } } bool ITimestampBased::isNewerThan(const ITimestampBased &otherTimestampObj) const { - return this->m_timestampMSecsSinceEpoch > otherTimestampObj.m_timestampMSecsSinceEpoch; + return m_timestampMSecsSinceEpoch > otherTimestampObj.m_timestampMSecsSinceEpoch; } bool ITimestampBased::isNewerThan(qint64 mSecsSinceEpoch) const { - return this->m_timestampMSecsSinceEpoch > mSecsSinceEpoch; + return m_timestampMSecsSinceEpoch > mSecsSinceEpoch; } bool ITimestampBased::isOlderThan(const ITimestampBased &otherTimestampObj) const { - return this->m_timestampMSecsSinceEpoch < otherTimestampObj.m_timestampMSecsSinceEpoch; + return m_timestampMSecsSinceEpoch < otherTimestampObj.m_timestampMSecsSinceEpoch; } bool ITimestampBased::isOlderThan(qint64 mSecsSinceEpoch) const { - return this->m_timestampMSecsSinceEpoch < mSecsSinceEpoch; + return m_timestampMSecsSinceEpoch < mSecsSinceEpoch; } bool ITimestampBased::isOlderThanNowMinusOffset(int offsetMs) const { if (offsetMs <= 0) { return false; } - return this->m_timestampMSecsSinceEpoch < (QDateTime::currentMSecsSinceEpoch() - offsetMs); + return m_timestampMSecsSinceEpoch < (QDateTime::currentMSecsSinceEpoch() - offsetMs); } bool ITimestampBased::isSame(const ITimestampBased &otherTimestampObj) const { - return this->m_timestampMSecsSinceEpoch == otherTimestampObj.m_timestampMSecsSinceEpoch; + return m_timestampMSecsSinceEpoch == otherTimestampObj.m_timestampMSecsSinceEpoch; } qint64 ITimestampBased::msecsTo(const ITimestampBased &otherTimestampObj) const { - return otherTimestampObj.m_timestampMSecsSinceEpoch - this->m_timestampMSecsSinceEpoch; + return otherTimestampObj.m_timestampMSecsSinceEpoch - m_timestampMSecsSinceEpoch; } qint64 ITimestampBased::absMsecsTo(const ITimestampBased &otherTimestampObj) const @@ -128,18 +128,18 @@ namespace BlackMisc qint64 ITimestampBased::msecsToNow() const { - if (this->m_timestampMSecsSinceEpoch < 0) return QDateTime::currentMSecsSinceEpoch(); - return QDateTime::currentMSecsSinceEpoch() - this->m_timestampMSecsSinceEpoch; + if (m_timestampMSecsSinceEpoch < 0) return QDateTime::currentMSecsSinceEpoch(); + return QDateTime::currentMSecsSinceEpoch() - m_timestampMSecsSinceEpoch; } void ITimestampBased::setCurrentUtcTime() { - this->m_timestampMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch(); + m_timestampMSecsSinceEpoch = QDateTime::currentMSecsSinceEpoch(); } void ITimestampBased::addMsecs(qint64 ms) { - this->m_timestampMSecsSinceEpoch += ms; + m_timestampMSecsSinceEpoch += ms; } QString ITimestampBased::getFormattedUtcTimestampMdhms() const @@ -200,7 +200,7 @@ namespace BlackMisc bool ITimestampBased::hasValidTimestamp() const { - return this->m_timestampMSecsSinceEpoch >= 0; + return m_timestampMSecsSinceEpoch >= 0; } bool ITimestampBased::canHandleIndex(const CPropertyIndex &index) @@ -250,7 +250,7 @@ namespace BlackMisc case IndexUtcTimestampFormattedDhms: { const QDateTime dt = QDateTime::fromString(variant.toQString()); - this->m_timestampMSecsSinceEpoch = dt.toMSecsSinceEpoch(); + m_timestampMSecsSinceEpoch = dt.toMSecsSinceEpoch(); } return; default: break; @@ -263,7 +263,7 @@ namespace BlackMisc int ITimestampBased::comparePropertyByIndex(const CPropertyIndex &index, const ITimestampBased &compareValue) const { Q_UNUSED(index); - return Compare::compare(this->m_timestampMSecsSinceEpoch, compareValue.m_timestampMSecsSinceEpoch); + return Compare::compare(m_timestampMSecsSinceEpoch, compareValue.m_timestampMSecsSinceEpoch); } QString ITimestampWithOffsetBased::getTimestampAndOffset(bool formatted) const @@ -282,6 +282,26 @@ namespace BlackMisc return ts.arg(this->getFormattedUtcTimestampHmsz()).arg(m_timestampMSecsSinceEpoch).arg(this->getTimeOffsetWithUnit()); } + bool ITimestampWithOffsetBased::isNewerThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const + { + return this->getAdjustedMSecsSinceEpoch() > otherTimestampObj.getAdjustedMSecsSinceEpoch(); + } + + bool ITimestampWithOffsetBased::isNewerThanAdjusted(qint64 mSecsSinceEpoch) const + { + return this->getAdjustedMSecsSinceEpoch() > mSecsSinceEpoch; + } + + bool ITimestampWithOffsetBased::isOlderThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const + { + return this->getAdjustedMSecsSinceEpoch() < otherTimestampObj.getAdjustedMSecsSinceEpoch(); + } + + bool ITimestampWithOffsetBased::isOlderThanAdjusted(qint64 mSecsSinceEpoch) const + { + return this->getAdjustedMSecsSinceEpoch() < mSecsSinceEpoch; + } + bool ITimestampWithOffsetBased::canHandleIndex(const CPropertyIndex &index) { if (ITimestampBased::canHandleIndex(index)) { return true; } diff --git a/src/blackmisc/timestampbased.h b/src/blackmisc/timestampbased.h index 4f87a5d78..512434a12 100644 --- a/src/blackmisc/timestampbased.h +++ b/src/blackmisc/timestampbased.h @@ -176,6 +176,18 @@ namespace BlackMisc //! Timestamp and offset QString getFormattedTimestampAndOffset(bool includeRawTimestamp) const; + //! Is this newer than other? + bool isNewerThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const; + + //! Is newer than epoch value? + bool isNewerThanAdjusted(qint64 mSecsSinceEpoch) const; + + //! Is this older than other? + bool isOlderThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const; + + //! Is this older than other? + bool isOlderThanAdjusted(qint64 mSecsSinceEpoch) const; + protected: //! Can given index be handled static bool canHandleIndex(const CPropertyIndex &index); diff --git a/src/blackmisc/timestampobjectlist.cpp b/src/blackmisc/timestampobjectlist.cpp index b2f2dce09..a9ef5db3d 100644 --- a/src/blackmisc/timestampobjectlist.cpp +++ b/src/blackmisc/timestampobjectlist.cpp @@ -227,15 +227,16 @@ namespace BlackMisc template void ITimestampObjectList::push_frontKeepLatestFirst(const OBJ &value, int maxElements) { - Q_ASSERT_X(maxElements < 0 || maxElements > 1, Q_FUNC_INFO, "Max.value wrong range"); CONTAINER &c = this->container(); + if (maxElements > 0 && c.size() >= maxElements) + { + c.truncate(maxElements - 1); + } if (!c.isEmpty() && value.isOlderThan(c.front())) { ITimestampObjectList::sortLatestFirst(); } c.push_front(value); - if (maxElements < 0 || maxElements <= c.size()) { return; } - c.truncate(maxElements); } template @@ -292,13 +293,15 @@ namespace BlackMisc { Q_ASSERT_X(maxElements < 0 || maxElements > 1, Q_FUNC_INFO, "Max.value wrong range"); CONTAINER &c = this->container(); - if (!c.isEmpty() && value.isOlderThan(c.front())) + if (maxElements > 0 && c.size() >= maxElements) + { + c.truncate(maxElements - 1); + } + if (!c.isEmpty() && value.isOlderThanAdjusted(c.front())) { ITimestampWithOffsetObjectList::sortAdjustedLatestFirst(); } c.push_front(value); - if (maxElements < 0 || maxElements <= c.size()) { return; } - c.truncate(maxElements); } template @@ -311,7 +314,6 @@ namespace BlackMisc if (!obj.hasValidTimestamp()) { return false; } if (obj.getAdjustedMSecsSinceEpoch() < max) { return false; } max = obj.getAdjustedMSecsSinceEpoch(); - continue; } return true; }