mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-09 21:45:34 +08:00
Ref T259, Ref T243 timestamp utility functions
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user