Commit 3104a4e5 authored by Volker Krause's avatar Volker Krause
Browse files

Change context date for IATA BCBP to QDateTime

We don't need the time, but we can't safely pass a QDate through JS.
parent 1b918291
......@@ -99,7 +99,7 @@ private Q_SLOTS:
const auto refArray = QJsonDocument::fromJson(f.readAll()).array();
QVERIFY(!refArray.isEmpty());
const auto res = IataBcbpParser::parse(bcbp, QDate(2018, 4, 2));
const auto res = IataBcbpParser::parse(bcbp, QDateTime({2018, 4, 2}, {}));
const auto resJson = JsonLdDocument::toJson(res);
if (refArray != resJson) {
......
......@@ -160,7 +160,7 @@ QVariant ExtractorPostprocessorPrivate::processFlightReservation(FlightReservati
// expand ticketToken for IATA BCBP data
const auto bcbp = res.reservedTicket().value<Ticket>().ticketTokenData();
if (!bcbp.isEmpty()) {
const auto bcbpData = IataBcbpParser::parse(bcbp, m_contextDate.date());
const auto bcbpData = IataBcbpParser::parse(bcbp, m_contextDate);
if (bcbpData.size() == 1) {
res = JsonLdDocument::apply(bcbpData.at(0), res).value<FlightReservation>();
// standardize on the BCBP booking reference, not some secondary one we might have in structured data for example
......
......@@ -26,16 +26,16 @@ static QString stripLeadingZeros(const QString &s)
return s.mid(d);
}
QVector<QVariant> IataBcbpParser::parse(const QString& message, const QDate &externalIssueDate)
QVector<QVariant> IataBcbpParser::parse(const QString& message, const QDateTime &externalIssueDate)
{
IataBcbp bcbp(message);
if (!bcbp.isValid()) {
return {};
}
return parse(bcbp, externalIssueDate.isValid() ? externalIssueDate : QDate(1970, 1, 1));
return parse(bcbp, externalIssueDate.isValid() ? externalIssueDate : QDateTime({1970, 1, 1}, {}));
}
QVector<QVariant> IataBcbpParser::parse(const IataBcbp &bcbp, const QDate &contextDate)
QVector<QVariant> IataBcbpParser::parse(const IataBcbp &bcbp, const QDateTime &contextDate)
{
const auto count = bcbp.uniqueMandatorySection().numberOfLegs();
const auto issueDate = bcbp.uniqueConditionalSection().dateOfIssue(contextDate);
......@@ -62,7 +62,7 @@ QVector<QVariant> IataBcbpParser::parse(const IataBcbp &bcbp, const QDate &conte
Flight flight;
const auto rms = bcbp.repeatedMandatorySection(i);
flight.setDepartureDay(rms.dateOfFlight(issueDate.isValid() ? issueDate : contextDate));
flight.setDepartureDay(rms.dateOfFlight(issueDate.isValid() ? QDateTime(issueDate, {}) : contextDate));
Airport dep;
dep.setIataCode(rms.fromCityAirportCode());
......
......@@ -8,7 +8,7 @@
#include "kitinerary_export.h"
#include <QDate>
#include <QDateTime>
#include <QVector>
#include <memory>
......@@ -32,8 +32,8 @@ namespace IataBcbpParser
* @param externalIssueDate The date the boarding pass was issued (or a sufficiently close approximation).
* This is necessary as by default the BCBP data only contains day and month of the flight, not the year.
*/
QVector<QVariant> parse(const QString &message, const QDate &externalIssueDate = QDate());
KITINERARY_EXPORT QVector<QVariant> parse(const IataBcbp &bcbp, const QDate &contextDate);
QVector<QVariant> parse(const QString &message, const QDateTime &externalIssueDateTime = QDateTime());
KITINERARY_EXPORT QVector<QVariant> parse(const IataBcbp &bcbp, const QDateTime &contextDate);
}
}
......
......@@ -59,17 +59,17 @@ bool IataBcbpUniqueConditionalSection::isValid() const
return true;
}
QDate IataBcbpUniqueConditionalSection::dateOfIssue(const QDate &contextDate) const
QDate IataBcbpUniqueConditionalSection::dateOfIssue(const QDateTime &contextDate) const
{
const auto day = dayOfIssue() - 1;
if (m_data.size() < 11 || day < 0) {
return {};
}
const auto year = contextDate.year() - contextDate.year() % 10 + yearOfIssue();
const auto year = contextDate.date().year() - contextDate.date().year() % 10 + yearOfIssue();
const auto d = QDate(year, 1, 1).addDays(day);
// TODO shouldn't this rather be d > contextDate?
if (year > contextDate.year()) {
if (year > contextDate.date().year()) {
return QDate(year - 10, 1, 1).addDays(day);
}
return d;
......@@ -97,14 +97,14 @@ bool IataBcbpRepeatedMandatorySection::isValid() const
&& dayOfFlight() <= 366;
}
QDate IataBcbpRepeatedMandatorySection::dateOfFlight(const QDate& contextDate) const
QDate IataBcbpRepeatedMandatorySection::dateOfFlight(const QDateTime& contextDate) const
{
const auto day = dayOfFlight() - 1;
if (day < 0) {
return {}; // no set
}
const auto d = QDate(contextDate.year(), 1, 1).addDays(day);
if (d < contextDate) {
const auto d = QDate(contextDate.date().year(), 1, 1).addDays(day);
if (d < contextDate.date()) {
return QDate(d.year() + 1, 1, 1).addDays(day);
}
return d;
......
......@@ -8,7 +8,7 @@
#include "kitinerary_export.h"
#include <QDate>
#include <QDateTime>
#include <QMetaType>
namespace KItinerary {
......@@ -74,7 +74,7 @@ public:
explicit IataBcbpUniqueConditionalSection(QStringView data);
bool isValid() const;
Q_INVOKABLE QDate dateOfIssue(const QDate &contextDate = QDate::currentDate()) const;
Q_INVOKABLE QDate dateOfIssue(const QDateTime &contextDate = QDateTime::currentDateTime()) const;
};
/** Repeated mandatory sections of an IATA BCBP, occurs once per leg. */
......@@ -102,7 +102,7 @@ public:
* @param contextDate A date before the flight to determine
* the full year which is not specified in the pass itself.
*/
Q_INVOKABLE QDate dateOfFlight(const QDate &contextDate = QDate::currentDate()) const;
Q_INVOKABLE QDate dateOfFlight(const QDateTime &contextDate = QDateTime::currentDateTime()) const;
};
/** Conditional (optional) sections of an IATA BCBP, occurs once per leg. */
......
......@@ -31,7 +31,7 @@ ExtractorDocumentNode IataBcbpDocumentProcessor::createNodeFromData(const QByteA
void IataBcbpDocumentProcessor::preExtract(ExtractorDocumentNode &node, [[maybe_unused]] const ExtractorEngine *engine) const
{
const auto bcbp = node.content<IataBcbp>();
node.addResult(IataBcbpParser::parse(bcbp, node.contextDateTime().date()));
node.addResult(IataBcbpParser::parse(bcbp, node.contextDateTime()));
}
bool IataBcbpDocumentProcessor::matches(const ExtractorFilter &filter, const ExtractorDocumentNode &node) const
......
......@@ -233,7 +233,7 @@ static void dumpVdv(const QByteArray &data)
std::cout << " version: " << ticket.trailer()->version << std::endl;
}
void dumpIataBcbp(const QString &data, const QDate &contextDate)
void dumpIataBcbp(const QString &data, const QDateTime &contextDate)
{
IataBcbp ticket(data);
if (!ticket.isValid()) {
......@@ -255,7 +255,7 @@ void dumpIataBcbp(const QString &data, const QDate &contextDate)
const auto rcs = ticket.repeatedConditionalSection(i);
dumpGadget(&rcs, " ");
std::cout << " Airline use section: " << qPrintable(ticket.airlineUseSection(i)) << std::endl;
std::cout << " Date of flight: " << qPrintable(rms.dateOfFlight(issueDate.isValid() ? issueDate : contextDate).toString(Qt::ISODate)) << std::endl;
std::cout << " Date of flight: " << qPrintable(rms.dateOfFlight(issueDate.isValid() ? QDateTime(issueDate, {}) : contextDate).toString(Qt::ISODate)) << std::endl;
}
if (ticket.hasSecuritySection()) {
......@@ -302,7 +302,7 @@ int main(int argc, char **argv)
if (IataBcbp::maybeIataBcbp(data)) {
std::cout << "IATA Barcoded Boarding Pass" << std::endl;
dumpIataBcbp(QString::fromUtf8(data), contextDate);
dumpIataBcbp(QString::fromUtf8(data), QDateTime(contextDate, {}));
} else if (SSBv3Ticket::maybeSSB(data)) {
std::cout << "ERA SSB Ticket" << std::endl;
dumpSsbv3Ticket(data);
......
Supports Markdown
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