From 059831534c7e884f0a607ed86b582fd00d821731 Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Sat, 26 Sep 2026 23:24:03 +0200 Subject: New upstream version 3.2.0~rc1+dfsg --- Network/DecodedTime.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Network/DecodedTime.cpp (limited to 'Network/DecodedTime.cpp') diff --git a/Network/DecodedTime.cpp b/Network/DecodedTime.cpp new file mode 100644 index 00000000..79572479 --- /dev/null +++ b/Network/DecodedTime.cpp @@ -0,0 +1,65 @@ +#include "DecodedTime.hpp" + +#include +#include + +namespace +{ + constexpr int secondsPerDay {24 * 60 * 60}; + + bool parseTime(QString const& encodedTime, QTime& time, int& secondsSinceMidnight) + { + if (encodedTime.size() != 4 && encodedTime.size() != 6) { + return false; + } + for (auto const character : encodedTime) { + if (!character.isDigit()) { + return false; + } + } + + bool ok = false; + auto const hours = encodedTime.mid(0, 2).toInt(&ok); + if (!ok) { + return false; + } + auto const minutes = encodedTime.mid(2, 2).toInt(&ok); + if (!ok) { + return false; + } + auto const seconds = encodedTime.size() == 6 + ? encodedTime.mid(4, 2).toInt(&ok) : 0; + if (!ok) { + return false; + } + + time = QTime {hours, minutes, seconds}; + if (!time.isValid()) { + return false; + } + secondsSinceMidnight = time.hour() * 60 * 60 + time.minute() * 60 + time.second(); + return true; + } +} + +namespace DecodedTime +{ + QDateTime spotTime(QString const& encodedTime, QDateTime const& nowUtc, + double periodSeconds) + { + QTime time; + int secondsSinceMidnight {0}; + if (!parseTime(encodedTime, time, secondsSinceMidnight) || !nowUtc.isValid()) { + return {}; + } + + auto const currentDate = nowUtc.toUTC().date(); + auto const spotDate = secondsSinceMidnight + qCeil(periodSeconds) >= secondsPerDay + ? currentDate.addDays(-1) : currentDate; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + return {spotDate, time, QTimeZone::UTC}; +#else + return {spotDate, time, Qt::UTC}; +#endif + } +} -- cgit v1.2.3