Ref T259, Ref T243 timestamp utility functions

This commit is contained in:
Klaus Basan
2018-03-04 22:26:11 +01:00
parent c061091bd2
commit 7beab371a1
3 changed files with 35 additions and 1 deletions

View File

@@ -46,6 +46,9 @@ namespace BlackMisc
//! Timestamp as ms value //! Timestamp as ms value
qint64 getMSecsSinceEpoch() const { return m_timestampMSecsSinceEpoch; } qint64 getMSecsSinceEpoch() const { return m_timestampMSecsSinceEpoch; }
//! Time difference in ms
qint64 getTimeDifferenceMs(qint64 compareTime) const { return compareTime - this->getMSecsSinceEpoch(); }
//! Set to null //! Set to null
void setTimestampToNull(); void setTimestampToNull();
@@ -170,6 +173,9 @@ namespace BlackMisc
//! Timestamp with offset added for interpolation //! Timestamp with offset added for interpolation
qint64 getAdjustedMSecsSinceEpoch() const { return this->getMSecsSinceEpoch() + this->getTimeOffsetMs(); } qint64 getAdjustedMSecsSinceEpoch() const { return this->getMSecsSinceEpoch() + this->getTimeOffsetMs(); }
//! Time difference in ms (this -> compare)
qint64 getAdjustedTimeDifferenceMs(qint64 compareTime) const { return this->getAdjustedMSecsSinceEpoch() - compareTime; }
//! Timestamp and offset //! Timestamp and offset
QString getTimestampAndOffset(bool formatted) const; QString getTimestampAndOffset(bool formatted) const;

View File

@@ -102,6 +102,17 @@ namespace BlackMisc
}); });
} }
template<class OBJ, class CONTAINER>
OBJ ITimestampObjectList<OBJ, CONTAINER>::findClosestTimeDistance(qint64 msSinceEpoch) const
{
if (this->container().isEmpty()) { return OBJ(); }
const auto closest = std::min_element(this->container().cbegin(), this->container().cend(), [ = ](const ITimestampBased & a, const ITimestampBased & b)
{
return qAbs(a.getTimeDifferenceMs(msSinceEpoch)) < qAbs(b.getTimeDifferenceMs(msSinceEpoch));
});
return *closest;
}
template <class OBJ, class CONTAINER> template <class OBJ, class CONTAINER>
bool ITimestampObjectList<OBJ, CONTAINER>::hasInvalidTimestamps() const bool ITimestampObjectList<OBJ, CONTAINER>::hasInvalidTimestamps() const
{ {
@@ -377,6 +388,17 @@ namespace BlackMisc
return true; return true;
} }
template<class OBJ, class CONTAINER>
OBJ ITimestampWithOffsetObjectList<OBJ, CONTAINER>::findClosestTimeDistanceAdjusted(qint64 msSinceEpoch) const
{
if (this->container().isEmpty()) { return OBJ(); }
const auto closest = std::min_element(this->container().cbegin(), this->container().cend(), [ = ](const ITimestampWithOffsetBased & a, const ITimestampWithOffsetBased & b)
{
return qAbs(a.getAdjustedTimeDifferenceMs(msSinceEpoch)) < qAbs(b.getAdjustedTimeDifferenceMs(msSinceEpoch));
});
return *closest;
}
// see here for the reason of thess forward instantiations // see here for the reason of thess forward instantiations
// https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl // https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl
//! \cond PRIVATE //! \cond PRIVATE

View File

@@ -47,6 +47,9 @@ namespace BlackMisc
//! Objects without valid timestamp //! Objects without valid timestamp
CONTAINER findInvalidTimestamps() const; CONTAINER findInvalidTimestamps() const;
//! Find closest (or default)
OBJ findClosestTimeDistance(qint64 msSinceEpoch) const;
//! Has invalid timestamp //! Has invalid timestamp
bool hasInvalidTimestamps() const; bool hasInvalidTimestamps() const;
@@ -146,7 +149,10 @@ namespace BlackMisc
//! \remark all object must have a valid timestamp //! \remark all object must have a valid timestamp
bool isSortedAdjustedLatestFirst() const; bool isSortedAdjustedLatestFirst() const;
protected: //! Closest adjusted time difference
OBJ findClosestTimeDistanceAdjusted(qint64 msSinceEpoch) const;
protected:
//! Constructor //! Constructor
ITimestampWithOffsetObjectList(); ITimestampWithOffsetObjectList();
}; };