Skip to content

Commit ffba29a

Browse files
committed
Set the timezone globally for the clock tests
Fix time zone during testing where needed. Closes #5
1 parent 9fc80c0 commit ffba29a

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

AutoTests/tst_ClockTests.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <QtTest>
2222
#include <QDate>
2323
#include <QDateTime>
24+
#include <QTimeZone>
2425
#include <QTextStream>
2526

2627
#include <OrgElement.h>
@@ -34,11 +35,13 @@
3435

3536
using namespace OrgMode;
3637

38+
// Use explicit timezone to ensure consistent DST handling across all platforms
39+
const QTimeZone Berlin("Europe/Berlin");
3740
const QDate today(2015, 4, 24);
38-
const QDateTime six(today, QTime(6,0));
39-
const QDateTime seven(today, QTime(7,0));
40-
const QDateTime eight(today, QTime(8,0));
41-
const QDateTime nine(today, QTime(9,0));
41+
const QDateTime six(today, QTime(6,0), Berlin);
42+
const QDateTime seven(today, QTime(7,0), Berlin);
43+
const QDateTime eight(today, QTime(8,0), Berlin);
44+
const QDateTime nine(today, QTime(9,0), Berlin);
4245
const TimeInterval sixToEight(six, eight);
4346
const TimeInterval sevenToNine(seven, nine);
4447
const TimeInterval sevenToEight(seven, eight);
@@ -59,6 +62,7 @@ class ClockTests : public QObject
5962
Q_OBJECT
6063

6164
private Q_SLOTS:
65+
void initTestCase();
6266
void testTimeIntervals_data();
6367
void testTimeIntervals();
6468
void testTimeIntervalsIsValid_data();
@@ -69,6 +73,17 @@ private Q_SLOTS:
6973
void testAccumulateForInterval();
7074
};
7175

76+
void ClockTests::initTestCase()
77+
{
78+
// Set timezone to Europe/Berlin to ensure consistent DST handling.
79+
// The test data in WeirdClockEntries.org was created assuming German local time,
80+
// and the Parser creates QDateTime objects using the system's local timezone.
81+
qputenv("TZ", "Europe/Berlin");
82+
#ifdef Q_OS_UNIX
83+
tzset();
84+
#endif
85+
}
86+
7287
void ClockTests::testTimeIntervals_data()
7388
{
7489
QTest::addColumn<TimeInterval>("left");
@@ -145,12 +160,18 @@ void ClockTests::testAccumulateForInterval_data()
145160
QTest::addColumn<int>("duration");
146161
QTest::addColumn<int>("total");
147162

163+
// Use explicit Berlin timezone for DST-sensitive date calculations
148164
const QDate mar26(2015, 3, 26);
149165
const QDate mar27(mar26.addDays(1));
150166
const QDate mar23(2015, 3, 23); //Monday
151167
const QDate mar30(mar23.addDays(7)); //Monday a week later
152-
const TimeInterval wk13(mar23, mar30);
153-
QTest::newRow("headline_1_1") << FL1("headline_1_1") << TimeInterval(mar26, mar27) << 60 * 60 << 60 * 150;
168+
// Construct TimeIntervals with explicit timezone to ensure correct DST handling
169+
const auto startOfDay = [](const QDate& date) {
170+
return QDateTime(date, QTime(0, 0), Berlin);
171+
};
172+
const TimeInterval wk13(startOfDay(mar23), startOfDay(mar30));
173+
const TimeInterval mar26to27(startOfDay(mar26), startOfDay(mar27));
174+
QTest::newRow("headline_1_1") << FL1("headline_1_1") << mar26to27 << 60 * 60 << 60 * 150;
154175
QTest::newRow("headline_1_2") << FL1("headline_1_2") << wk13 << 60 * 60 << 60 * 150;
155176
QTest::newRow("headline_1_3 DST switch") << FL1("headline_1_3") << wk13 << 60 * 60 * 11 << 60 * 60 * 11;
156177
QTest::newRow("headline_1 full week") << FL1("headline_1") << wk13 << 52200 << 57600; //14:30h, 15:15h total

0 commit comments

Comments
 (0)