diff --git a/src/blackmisc/timestampbased.h b/src/blackmisc/timestampbased.h index 7bd0b0678..38a4852e9 100644 --- a/src/blackmisc/timestampbased.h +++ b/src/blackmisc/timestampbased.h @@ -49,6 +49,15 @@ namespace BlackMisc //! Time difference in ms qint64 getTimeDifferenceMs(qint64 compareTime) const { return compareTime - this->getMSecsSinceEpoch(); } + //! Time difference in ms + qint64 getTimeDifferenceMs(const ITimestampBased &compare) const { return compare.getMSecsSinceEpoch() - this->getMSecsSinceEpoch(); } + + //! Time difference in ms + qint64 getAbsTimeDifferenceMs(qint64 compareTime) const { return qAbs(this->getTimeDifferenceMs(compareTime)); } + + //! Time difference in ms + qint64 getAbsTimeDifferenceMs(const ITimestampBased &compare) const { return qAbs(this->getTimeDifferenceMs(compare)); } + //! Set to null void setTimestampToNull(); diff --git a/src/blackmisc/timestampobjectlist.cpp b/src/blackmisc/timestampobjectlist.cpp index 533080141..5b870fd61 100644 --- a/src/blackmisc/timestampobjectlist.cpp +++ b/src/blackmisc/timestampobjectlist.cpp @@ -37,6 +37,7 @@ #include #include #include +#include using namespace BlackConfig; @@ -330,6 +331,45 @@ namespace BlackMisc } } + template + MillisecondsMinMaxMean ITimestampObjectList::getTimestampDifferenceMinMaxMean() const + { + MillisecondsMinMaxMean mmm; + mmm.reset(); + if (this->container().size() < 2) { return mmm; } + + if (m_tsSortHint == NoTimestampSortHint) + { + CONTAINER copy(this->container()); + copy.sortLatestFirst(); + copy.m_tsSortHint = TimestampLatestFirst; + return copy.getTimestampDifferenceMinMaxMean(); + } + + mmm.max = std::numeric_limits::min(); + mmm.min = std::numeric_limits::max(); + qint64 mean = 0; + int c = 0; + OBJ last; + + for (const OBJ &object : this->container()) + { + c++; + last = object; + if (c < 2) { continue; } + + const ITimestampBased &l = last; + const ITimestampBased &o = object; + const qint64 diff = l.getAbsTimeDifferenceMs(o); + if (diff > mmm.max) { mmm.max = diff; } + if (diff < mmm.min) { mmm.min = diff; } + mean += diff; + } + + mmm.mean = mean / c; + return mmm; + } + template void ITimestampWithOffsetObjectList::sortAdjustedLatestFirst() { diff --git a/src/blackmisc/timestampobjectlist.h b/src/blackmisc/timestampobjectlist.h index 659b10665..582650a47 100644 --- a/src/blackmisc/timestampobjectlist.h +++ b/src/blackmisc/timestampobjectlist.h @@ -21,6 +21,23 @@ class QDateTime; namespace BlackMisc { + //! Milliseconds minimum/maximum/mean + struct MillisecondsMinMaxMean + { + qint64 min; //!< Minimum + qint64 max; //!< Maximum + double mean; //!< Mean (average) + + //! Valid? + bool isValid() const { return min >= 0 && max >= 0; } + + //! Reset the values + void reset() { min = -1; max = -1; mean = -1;} + + //! As string + QString asString() const { static const QString s("Min: %1 Max: %2 Mean: %3"); return s.arg(min).arg(max).arg(mean, 0, 'f', 2); } + }; + //! List of objects with timestamp. //! Such objects should implement \sa ITimestampBased template class ITimestampObjectList @@ -30,7 +47,8 @@ namespace BlackMisc enum HintTimestampSort { NoTimestampSortHint, - TimestampLatestFirst + TimestampLatestFirst, + TimestampLatestLast }; //! List of objects before dateTime (older) @@ -125,6 +143,10 @@ namespace BlackMisc //! Adds a time to all values void addMsecs(qint64 msToAdd); + //! Difference of timestamp values + //! \cond timestamp list has to be sorted to get meaninful values + MillisecondsMinMaxMean getTimestampDifferenceMinMaxMean() const; + //! Set the hint void setSortHint(HintTimestampSort hint) { m_tsSortHint = hint; }