From 2e322e5cc2b263875ad1a6a6dc7a5f2fb3f89fe3 Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Sun, 26 Apr 2015 21:27:25 +0200 Subject: [PATCH] Fix some daylight saving time issues by using UTC. This commit switches the internal clock of the hafas provider to use UTC. hafas already provides times that are adjusted for daylight saving so no further adjustment needs to be done by the parser. The patch also uses 1970-01-01 as the reference day so that a day switch during reesult generation won't result in wrong duration displays. This commit fixes issue #185. --- src/parser/parser_hafasbinary.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/parser/parser_hafasbinary.cpp b/src/parser/parser_hafasbinary.cpp index 8568a175..03b2fd6b 100644 --- a/src/parser/parser_hafasbinary.cpp +++ b/src/parser/parser_hafasbinary.cpp @@ -694,7 +694,8 @@ QString ParserHafasBinary::formatDuration(QDateTime durationTime) { QString tmpDuration = durationTime.time().toString("hh:mm"); QDateTime refDate; - refDate.setDate(QDate::currentDate()); + refDate.setTimeSpec(Qt::UTC); + refDate.setDate(QDate(1970, 1, 1)); int days = refDate.daysTo(durationTime); if (days > 0) { tmpDuration = QString::number(days) + "d " + tmpDuration; @@ -705,23 +706,29 @@ QString ParserHafasBinary::formatDuration(QDateTime durationTime) QDateTime ParserHafasBinary::toTime(quint16 time) { QDateTime tmpDateTime; + tmpDateTime.setTimeSpec(Qt::UTC); if (time == 0xffff) return tmpDateTime; int hours = time / 100; int minutes = time % 100; - tmpDateTime.setDate(QDate::currentDate()); - return tmpDateTime.addSecs(((hours * 60) + minutes) * 60); + tmpDateTime.setDate(QDate(1970, 1, 1)); + tmpDateTime.setTime(QTime(hours % 24, minutes)); + return tmpDateTime.addDays(hours / 24); } QDateTime ParserHafasBinary::toTime(quint16 time, QDate baseDate) { QDateTime tmpDateTime; + // Maybe it would be better to use the correct local time for the provider + // here. + tmpDateTime.setTimeSpec(Qt::UTC); if (time == 0xffff) return tmpDateTime; tmpDateTime.setDate(baseDate); int hours = time / 100; int minutes = time % 100; - return tmpDateTime.addSecs(((hours * 60) + minutes) * 60); + tmpDateTime.setTime(QTime(hours % 24, minutes)); + return tmpDateTime.addDays(hours / 24);; } QDate ParserHafasBinary::toDate(quint16 date)