From 0681171d691e1a5d82ad7f178eb80bef33a256c1 Mon Sep 17 00:00:00 2001 From: mvglasow Date: Sat, 14 Jun 2025 21:14:00 +0300 Subject: [PATCH] [traffxml] Introduce timestamp shift operation Signed-off-by: mvglasow --- traffxml/traff_model.cpp | 18 ++++++++++++++++++ traffxml/traff_model.hpp | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/traffxml/traff_model.cpp b/traffxml/traff_model.cpp index 0a3581732..544ed73e0 100644 --- a/traffxml/traff_model.cpp +++ b/traffxml/traff_model.cpp @@ -153,6 +153,12 @@ IsoTime::IsoTime(std::chrono::time_point tp) bool IsoTime::IsPast() { return m_tp < std::chrono::utc_clock::now(); +}\ + +void IsoTime::Shift(IsoTime nowRef) +{ + auto const offset = std::chrono::utc_clock::now() - nowRef.m_tp; + m_tp += offset; } bool IsoTime::operator< (IsoTime & rhs) @@ -256,6 +262,18 @@ std::optional TraffMessage::GetTrafficImpact() return std::nullopt; } +void TraffMessage::ShiftTimestamps() +{ + IsoTime nowRef = m_updateTime; + m_receiveTime.Shift(nowRef); + m_updateTime.Shift(nowRef); + m_expirationTime.Shift(nowRef); + if (m_startTime) + m_startTime.value().Shift(nowRef); + if (m_endTime) + m_endTime.value().Shift(nowRef); +} + void MergeMultiMwmColoring(MultiMwmColoring & delta, MultiMwmColoring & target) { // for each mwm in delta diff --git a/traffxml/traff_model.hpp b/traffxml/traff_model.hpp index a463098f1..26512b81e 100644 --- a/traffxml/traff_model.hpp +++ b/traffxml/traff_model.hpp @@ -72,6 +72,17 @@ public: */ bool IsPast(); + /** + * @brief Shifts time to the present. + * + * This method is intended for testing. It shifts the timestamp by a fixed amount, so that + * `nowRef` corresponds to current time. After this method returns, the timestamp will have the + * same offset from current time that it had from `nowRef` at the time the call was made. + * + * @param nowRef + */ + void Shift(IsoTime nowRef); + bool operator< (IsoTime & rhs); bool operator> (IsoTime & rhs); private: @@ -362,6 +373,16 @@ struct TraffMessage */ std::optional GetTrafficImpact(); + /** + * @brief Shifts timestamps to the present. + * + * This method is intended for testing. It shifts the timestamps of the message by a fixed amount, + * so that `m_updateTime` corresponds to current time, and all other timestamps maintain their + * offset to `m_updateTime`. If `m_startTime` and/or `m_endTime` are set, they may be adjusted + * further to maintain their offset from midnight or the full hour (currently not implemented). + */ + void ShiftTimestamps(); + std::string m_id; IsoTime m_receiveTime = IsoTime::Now(); IsoTime m_updateTime = IsoTime::Now();