Commit 43b95df6 authored by Volker Krause's avatar Volker Krause

Parse Hafas past-midnight time format correctly

parent 410335fe
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include <QTest> #include <QTest>
#include <QTimeZone> #include <QTimeZone>
#define s(x) QStringLiteral(x)
using namespace KPublicTransport; using namespace KPublicTransport;
class HafasParserTest : public QObject class HafasParserTest : public QObject
...@@ -49,6 +51,25 @@ private Q_SLOTS: ...@@ -49,6 +51,25 @@ private Q_SLOTS:
QCOMPARE(p.error(), Reply::NotFoundError); QCOMPARE(p.error(), Reply::NotFoundError);
QVERIFY(!p.errorMessage().isEmpty()); QVERIFY(!p.errorMessage().isEmpty());
} }
void parseDateTime_data()
{
QTest::addColumn<QString>("date");
QTest::addColumn<QString>("time");
QTest::addColumn<QDateTime>("dt");
QTest::newRow("empty") << QString() << QString() << QDateTime();
QTest::newRow("same day") << s("20190105") << s("142100") << QDateTime({2019, 1, 5}, {14, 21});
QTest::newRow("next day") << s("20190105") << s("01142100") << QDateTime({2019, 1, 6}, {14, 21});
}
void parseDateTime()
{
QFETCH(QString, date);
QFETCH(QString, time);
QFETCH(QDateTime, dt);
QCOMPARE(HafasMgateParser::parseDateTime(date, time), dt);
}
}; };
QTEST_GUILESS_MAIN(HafasParserTest) QTEST_GUILESS_MAIN(HafasParserTest)
......
...@@ -135,16 +135,10 @@ std::vector<Departure> HafasMgateParser::parseStationBoardResponse(const QJsonOb ...@@ -135,16 +135,10 @@ std::vector<Departure> HafasMgateParser::parseStationBoardResponse(const QJsonOb
dep.setRoute(route); dep.setRoute(route);
const auto dateStr = jnyObj.value(QLatin1String("date")).toString(); const auto dateStr = jnyObj.value(QLatin1String("date")).toString();
dep.setScheduledDepartureTime(QDateTime::fromString(dateStr + stbStop.value(QLatin1String("dTimeS")).toString(), QLatin1String("yyyyMMddhhmmss"))); dep.setScheduledDepartureTime(parseDateTime(dateStr, stbStop.value(QLatin1String("dTimeS")).toString()));
const auto dTimeR = stbStop.value(QLatin1String("dTimeR")).toString(); dep.setExpectedDepartureTime(parseDateTime(dateStr, stbStop.value(QLatin1String("dTimeR")).toString()));
if (!dTimeR.isEmpty()) { dep.setScheduledArrivalTime(parseDateTime(dateStr, stbStop.value(QLatin1String("aTimeS")).toString()));
dep.setExpectedDepartureTime(QDateTime::fromString(dateStr + dTimeR, QLatin1String("yyyyMMddhhmmss"))); dep.setExpectedArrivalTime(parseDateTime(dateStr, stbStop.value(QLatin1String("aTimeR")).toString()));
}
dep.setScheduledArrivalTime(QDateTime::fromString(dateStr + stbStop.value(QLatin1String("aTimeS")).toString(), QLatin1String("yyyyMMddhhmmss")));
const auto aTimeR = stbStop.value(QLatin1String("aTimeR")).toString();
if (!aTimeR.isEmpty()) {
dep.setExpectedArrivalTime(QDateTime::fromString(dateStr + aTimeR, QLatin1String("yyyyMMddhhmmss")));
}
dep.setScheduledPlatform(stbStop.value(QLatin1String("dPlatfS")).toString()); dep.setScheduledPlatform(stbStop.value(QLatin1String("dPlatfS")).toString());
dep.setExpectedPlatform(stbStop.value(QLatin1String("dPlatfR")).toString()); dep.setExpectedPlatform(stbStop.value(QLatin1String("dPlatfR")).toString());
...@@ -271,11 +265,8 @@ std::vector<Journey> HafasMgateParser::parseTripSearch(const QJsonObject &obj) c ...@@ -271,11 +265,8 @@ std::vector<Journey> HafasMgateParser::parseTripSearch(const QJsonObject &obj) c
JourneySection section; JourneySection section;
const auto dep = secObj.value(QLatin1String("dep")).toObject(); const auto dep = secObj.value(QLatin1String("dep")).toObject();
section.setScheduledDepartureTime(QDateTime::fromString(dateStr + dep.value(QLatin1String("dTimeS")).toString(), QLatin1String("yyyyMMddhhmmss"))); section.setScheduledDepartureTime(parseDateTime(dateStr, dep.value(QLatin1String("dTimeS")).toString()));
const auto dTimeR = dep.value(QLatin1String("dTimeR")).toString(); section.setExpectedDepartureTime(parseDateTime(dateStr, dep.value(QLatin1String("dTimeR")).toString()));
if (!dTimeR.isEmpty()) {
section.setExpectedDepartureTime(QDateTime::fromString(dateStr + dTimeR, QLatin1String("yyyyMMddhhmmss")));
}
auto locIdx = dep.value(QLatin1String("locX")).toInt(); auto locIdx = dep.value(QLatin1String("locX")).toInt();
if ((unsigned int)locIdx < locs.size()) { if ((unsigned int)locIdx < locs.size()) {
section.setFrom(locs[locIdx]); section.setFrom(locs[locIdx]);
...@@ -284,11 +275,8 @@ std::vector<Journey> HafasMgateParser::parseTripSearch(const QJsonObject &obj) c ...@@ -284,11 +275,8 @@ std::vector<Journey> HafasMgateParser::parseTripSearch(const QJsonObject &obj) c
section.setExpectedDeparturePlatform(dep.value(QLatin1String("dPlatfR")).toString()); section.setExpectedDeparturePlatform(dep.value(QLatin1String("dPlatfR")).toString());
const auto arr = secObj.value(QLatin1String("arr")).toObject(); const auto arr = secObj.value(QLatin1String("arr")).toObject();
section.setScheduledArrivalTime(QDateTime::fromString(dateStr + arr.value(QLatin1String("aTimeS")).toString(), QLatin1String("yyyyMMddhhmmss"))); section.setScheduledArrivalTime(parseDateTime(dateStr, arr.value(QLatin1String("aTimeS")).toString()));
const auto aTimeR = dep.value(QLatin1String("aTimeR")).toString(); section.setExpectedArrivalTime(parseDateTime(dateStr, arr.value(QLatin1String("aTimeR")).toString()));
if (!aTimeR.isEmpty()) {
section.setExpectedArrivalTime(QDateTime::fromString(dateStr + aTimeR, QLatin1String("yyyyMMddhhmmss")));
}
locIdx = arr.value(QLatin1String("locX")).toInt(); locIdx = arr.value(QLatin1String("locX")).toInt();
if ((unsigned int)locIdx < locs.size()) { if ((unsigned int)locIdx < locs.size()) {
section.setTo(locs[locIdx]); section.setTo(locs[locIdx]);
...@@ -332,3 +320,18 @@ QString HafasMgateParser::errorMessage() const ...@@ -332,3 +320,18 @@ QString HafasMgateParser::errorMessage() const
{ {
return m_errorMsg; return m_errorMsg;
} }
QDateTime HafasMgateParser::parseDateTime(const QString &date, const QString &time)
{
if (date.isEmpty() || time.isEmpty()) {
return {};
}
int dayOffset = 0;
if (time.size() > 6) {
dayOffset = time.leftRef(time.size() - 6).toInt();
}
const auto dt = QDateTime::fromString(date + time.rightRef(6), QLatin1String("yyyyMMddhhmmss"));
return dt.addDays(dayOffset);
}
...@@ -55,6 +55,8 @@ public: ...@@ -55,6 +55,8 @@ public:
Reply::Error error() const; Reply::Error error() const;
QString errorMessage() const; QString errorMessage() const;
static QDateTime parseDateTime(const QString &date, const QString &time);
private: private:
Q_DISABLE_COPY(HafasMgateParser) Q_DISABLE_COPY(HafasMgateParser)
std::vector<Departure> parseStationBoardResponse(const QJsonObject &obj) const; std::vector<Departure> parseStationBoardResponse(const QJsonObject &obj) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment