[pim/eventviews] /: Move KCalUtils::Stringify::tzUTCOffsetStr here

Volker Krause <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit dcc87af3d47f10386a4ac3b49f4757fa88e4e91d by Volker Krause.
Committed on 09/08/2026 at 06:46.
Pushed by vkrause into branch 'master'.

Move KCalUtils::Stringify::tzUTCOffsetStr here

This is the only user.

M  +1    -0    CMakeLists.txt
A  +4    -0    autotests/CMakeLists.txt
A  +46   -0    autotests/timelabelutiltest.cpp     [License: LGPL(v2.0+)]
M  +1    -0    src/CMakeLists.txt
M  +3    -4    src/agenda/timelabels.cpp
A  +32   -0    src/agenda/timelabelutil.cpp     [License: LGPL(v2.0+)]
A  +19   -0    src/agenda/timelabelutil_p.h     [License: LGPL(v2.0+)]
M  +3    -3    src/agenda/timescaleconfigdialog.cpp

https://invent.kde.org/pim/eventviews/-/commit/dcc87af3d47f10386a4ac3b49f4757fa88e4e91d

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2c5f0861..3719d160 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -166,6 +166,7 @@ add_compile_definitions(QT_ENABLE_STRICT_MODE_UP_TO=0x060B00)
 add_subdirectory(src)
 if(BUILD_TESTING)
     find_package(Qt6 ${QT_REQUIRED_VERSION} CONFIG REQUIRED Test)
+    add_subdirectory(autotests)
     add_subdirectory(tests)
 endif()
 
diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt
new file mode 100644
index 00000000..85fefc11
--- /dev/null
+++ b/autotests/CMakeLists.txt
@@ -0,0 +1,4 @@
+# SPDX-FileCopyrightText: Volker Krause <[email protected]>
+# SPDX-License-Identifier: BSD-3-Clause
+
+ecm_add_tests(timelabelutiltest.cpp LINK_LIBRARIES Qt::Test)
diff --git a/autotests/timelabelutiltest.cpp b/autotests/timelabelutiltest.cpp
new file mode 100644
index 00000000..c5cca60e
--- /dev/null
+++ b/autotests/timelabelutiltest.cpp
@@ -0,0 +1,46 @@
+/*
+  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
+  SPDX-License-Identifier: LGPL-2.0-or-later
+*/
+
+#include "../src/agenda/timelabelutil.cpp"
+
+#include <QTest>
+
+using namespace EventViews;
+
+class TimeLabelUtilTest : public QObject
+{
+    Q_OBJECT
+private Q_SLOTS:
+    static void testUTCoffsetStrings()
+    {
+        const QTimeZone tz1(5 * 60 * 60); // 5 hrs
+        QCOMPARE(tzUTCOffsetStr(tz1), QStringLiteral("+05:00"));
+
+        const QTimeZone tz2(-5 * 60 * 60); //-5 hrs
+        QCOMPARE(tzUTCOffsetStr(tz2), QStringLiteral("-05:00"));
+
+        const QTimeZone tz3(0);
+        QCOMPARE(tzUTCOffsetStr(tz3), QStringLiteral("+00:00"));
+
+        const QTimeZone tz4(30 * 60 * 60); // 30 hrs -- out-of-range
+        QCOMPARE(tzUTCOffsetStr(tz4), QStringLiteral("+00:00"));
+
+        const QTimeZone tz5((5 * 60 * 60) + (30 * 60)); // 5:30
+        QCOMPARE(tzUTCOffsetStr(tz5), QStringLiteral("+05:30"));
+
+        const QTimeZone tz6(-((11 * 60 * 60) + (59 * 60))); //-11:59
+        QCOMPARE(tzUTCOffsetStr(tz6), QStringLiteral("-11:59"));
+
+        const QTimeZone tz7(12 * 60 * 60); // 12:00
+        QCOMPARE(tzUTCOffsetStr(tz7), QStringLiteral("+12:00"));
+
+        const QTimeZone tz8(-((12 * 60 * 60) + (59 * 60))); //-12:59
+        QCOMPARE(tzUTCOffsetStr(tz8), QStringLiteral("-12:59"));
+    }
+};
+
+QTEST_APPLESS_MAIN(TimeLabelUtilTest)
+
+#include "timelabelutiltest.moc"
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a32d687b..043d69d9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -26,6 +26,7 @@ target_sources(
         agenda/calendardecoration.cpp
         agenda/decorationlabel.cpp
         agenda/timelabels.cpp
+        agenda/timelabelutil.cpp
         agenda/timelabelszone.cpp
         agenda/timescaleconfigdialog.cpp
         agenda/viewcalendar.cpp
diff --git a/src/agenda/timelabels.cpp b/src/agenda/timelabels.cpp
index a3410f9b..178c92a8 100644
--- a/src/agenda/timelabels.cpp
+++ b/src/agenda/timelabels.cpp
@@ -12,10 +12,9 @@ using namespace Qt::Literals::StringLiterals;
 #include "agendaview.h"
 #include "prefs.h"
 #include "timelabelszone.h"
+#include "timelabelutil_p.h"
 #include "timescaleconfigdialog.h"
 
-#include <KCalUtils/Stringify>
-
 #include <KLocalizedString>
 
 #include <QHelpEvent>
@@ -425,7 +424,7 @@ QString TimeLabels::headerToolTip() const
     toolTip += i18nc("title for timezone info, the timezone id and utc offset",
                      "<b>%1 (%2)</b>",
                      i18n(mTimezone.id().constData()),
-                     KCalUtils::Stringify::tzUTCOffsetStr(mTimezone));
+                     EventViews::tzUTCOffsetStr(mTimezone));
     toolTip += "<hr>"_L1;
     toolTip += i18nc("heading for timezone display name", "<i>Name:</i> %1", mTimezone.displayName(now, QTimeZone::LongName));
     toolTip += "<br/>"_L1;
@@ -469,7 +468,7 @@ bool TimeLabels::event(QEvent *event)
                          cellToHour(cell),
                          cellToSuffix(cell),
                          i18n(mTimezone.id().constData()),
-                         KCalUtils::Stringify::tzUTCOffsetStr(mTimezone));
+                         EventViews::tzUTCOffsetStr(mTimezone));
         toolTip += "</qt>"_L1;
 
         QToolTip::showText(helpEvent->globalPos(), toolTip, this);
diff --git a/src/agenda/timelabelutil.cpp b/src/agenda/timelabelutil.cpp
new file mode 100644
index 00000000..ec625a36
--- /dev/null
+++ b/src/agenda/timelabelutil.cpp
@@ -0,0 +1,32 @@
+/*
+  This file is part of the kcalutils library.
+
+  SPDX-FileCopyrightText: 2001 Cornelius Schumacher <[email protected]>
+  SPDX-FileCopyrightText: 2004 Reinhold Kainhofer <[email protected]>
+  SPDX-FileCopyrightText: 2005 Rafal Rzepecki <[email protected]>
+  SPDX-FileCopyrightText: 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
+  SPDX-FileCopyrightText: 2017 Allen Winter <[email protected]>
+
+  SPDX-License-Identifier: LGPL-2.0-or-later
+*/
+
+#include "timelabelutil_p.h"
+
+#include <QTimeZone>
+
+QString EventViews::tzUTCOffsetStr(const QTimeZone &tz)
+{
+    int const currentOffset = tz.offsetFromUtc(QDateTime::currentDateTimeUtc());
+    int const absOffset = qAbs(currentOffset);
+    int const utcOffsetHrs = absOffset / 3600; // in hours
+    int const utcOffsetMins = (absOffset % 3600) / 60; // in minutes
+
+    const QString hrStr = QStringLiteral("%1").arg(utcOffsetHrs, 2, 10, QLatin1Char('0'));
+    const QString mnStr = QStringLiteral("%1").arg(utcOffsetMins, 2, 10, QLatin1Char('0'));
+
+    if (currentOffset < 0) {
+        return QStringLiteral("-%1:%2").arg(hrStr, mnStr);
+    } else {
+        return QStringLiteral("+%1:%2").arg(hrStr, mnStr);
+    }
+}
diff --git a/src/agenda/timelabelutil_p.h b/src/agenda/timelabelutil_p.h
new file mode 100644
index 00000000..f708439a
--- /dev/null
+++ b/src/agenda/timelabelutil_p.h
@@ -0,0 +1,19 @@
+/*
+  This file is part of the kcalutils library.
+
+  SPDX-FileCopyrightText: 2001-2003 Cornelius Schumacher <[email protected]>
+  SPDX-FileCopyrightText: 2004 Reinhold Kainhofer <[email protected]>
+  SPDX-FileCopyrightText: 2009-2010 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
+
+  SPDX-License-Identifier: LGPL-2.0-or-later
+*/
+
+#pragma once
+
+class QString;
+class QTimeZone;
+
+namespace EventViews
+{
+[[nodiscard]] QString tzUTCOffsetStr(const QTimeZone &tz);
+}
diff --git a/src/agenda/timescaleconfigdialog.cpp b/src/agenda/timescaleconfigdialog.cpp
index 90311af0..d248af3a 100644
--- a/src/agenda/timescaleconfigdialog.cpp
+++ b/src/agenda/timescaleconfigdialog.cpp
@@ -4,9 +4,9 @@
   SPDX-License-Identifier: GPL-2.0-or-later WITH LicenseRef-Qt-Commercial-exception-1.0
 */
 #include "timescaleconfigdialog.h"
-#include "prefs.h"
 
-#include <KCalUtils/Stringify>
+#include "prefs.h"
+#include "timelabelutil_p.h"
 
 #include <KLocalizedString>
 
@@ -40,7 +40,7 @@ using TimeZoneNamePair = QPair<QString, QByteArray>;
 static QString tzWithUTC(const QByteArray &zoneId)
 {
     auto tz = QTimeZone(zoneId);
-    return QStringLiteral("%1 (%2)").arg(i18n(zoneId.constData()), KCalUtils::Stringify::tzUTCOffsetStr(tz));
+    return QStringLiteral("%1 (%2)").arg(i18n(zoneId.constData()), EventViews::tzUTCOffsetStr(tz));
 }
 
 TimeScaleConfigDialog::TimeScaleConfigDialog(const PrefsPtr &preferences, QWidget *parent)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.