summaryrefslogtreecommitdiff
path: root/Network/DecodedTime.cpp
diff options
context:
space:
mode:
authorChristoph Berg <myon@debian.org>2026-09-26 23:24:28 +0200
committerChristoph Berg <myon@debian.org>2026-09-26 23:24:28 +0200
commit69344560ea394c06ca94acac46a2e4c3e8b7b229 (patch)
treeebc98370a697a9bbd6b973a14fa5113c475c6db7 /Network/DecodedTime.cpp
parent3e9d10a67671d0bd5c7b8c3ca8088f1818322eff (diff)
parent059831534c7e884f0a607ed86b582fd00d821731 (diff)
Update upstream source from tag 'upstream/3.2.0_rc1+dfsg'
Update to upstream version '3.2.0~rc1+dfsg' with Debian dir dbff457fd2c3616d5c77b863c7bfb2186ed86047
Diffstat (limited to 'Network/DecodedTime.cpp')
-rw-r--r--Network/DecodedTime.cpp65
1 files changed, 65 insertions, 0 deletions
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 <QTime>
+#include <QtMath>
+
+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
+ }
+}