Commit e85a1a9d authored by Volker Krause's avatar Volker Krause
Browse files

Allow lists of format and locale candidates in date/time decoding JS API

Simplifies extractor scripts.
parent 2ba87c61
Pipeline #63462 passed with stages
in 13 minutes and 33 seconds
......@@ -62,7 +62,7 @@ private Q_SLOTS:
JsApi::JsonLd jsonLd(nullptr);
jsonLd.setContextDate({{2018, 4, 1}, {}});
QCOMPARE(jsonLd.toDateTime(input, format, locale), result);
QCOMPARE(jsonLd.toDateTime(input, QJSValue(format), QJSValue(locale)), result);
}
void testToGeoCoordinates_data()
......
......@@ -211,6 +211,32 @@ QDateTime JsApi::JsonLd::toDateTime(const QString &dtStr, const QString &format,
return dt;
}
QDateTime JsApi::JsonLd::toDateTime(const QString &dtStr, const QJSValue &format, const QJSValue &localeName) const
{
if (localeName.isString() && format.isString()) {
return toDateTime(dtStr, format.toString(), localeName.toString());
}
if (format.isArray()) {
const auto count = format.property(QLatin1String("length")).toInt();
for (auto i = 0; i < count; ++i) {
const auto dt = toDateTime(dtStr, format.property(i), localeName);
if (dt.isValid()) {
return dt;
}
}
}
if (format.isString() && localeName.isArray()) {
const auto count = localeName.property(QLatin1String("length")).toInt();
for (auto i = 0; i < count; ++i) {
const auto dt = toDateTime(dtStr, format.toString(), localeName.property(i).toString());
if (dt.isValid()) {
return dt;
}
}
}
return {};
}
QJSValue JsApi::JsonLd::toJson(const QVariant &v) const
{
if (v.canConvert<QVector<QVariant>>()) {
......
......@@ -56,12 +56,12 @@ public:
* used by QLocale and QDateTime. If the year is not part of the date
* it is attempted to be recovered from the context date set on the
* ExtractorEngine (that is, the returned date will be after the context
* date).
* date). Can be a string or an array of strings, which are then tried sequentially.
* @param localeName The locale in which the string is formatted. This is
* relevant when the input contains for example localized month names or
* month abbreviations.
* month abbreviations. Can be a string or an array of strings.
*/
Q_INVOKABLE QDateTime toDateTime(const QString &dtStr, const QString &format, const QString &localeName) const;
Q_INVOKABLE QDateTime toDateTime(const QString &dtStr, const QJSValue &format, const QJSValue &localeName) const;
/** Convert object @p v to a JSON-LD object.
* This is useful when interacting with API returning regular data types,
* such as Uic9183Parser.
......@@ -91,6 +91,7 @@ public:
///@endcond
private:
QJSValue newPlace(const QString &type) const;
QDateTime toDateTime(const QString &dtStr, const QString &format, const QString &localeName) const;
QJSEngine *m_engine;
QDateTime m_contextDate;
};
......
......@@ -25,17 +25,13 @@ function parsePage(page, triggerNode)
res.reservationFor.departureAirport.name = dep[2];
res.reservationFor.departureAirport.iataCode = dep[3];
res.reservationFor.departureTerminal = dep[4];
res.reservationFor.departureTime = JsonLd.toDateTime(flight[3] + dep[1], "dd MMM yyyyhh:mm", "en");
if (isNaN(res.reservationFor.departureTime.getTime()))
res.reservationFor.departureTime = JsonLd.toDateTime(flight[3] + dep[1], "yyyy MMM ddhh:mm", "en");
res.reservationFor.departureTime = JsonLd.toDateTime(flight[3] + dep[1], ["dd MMM yyyyhh:mm", "yyyy MMM ddhh:mm"] , "en");
var arr = page.text.match(/\Arrival.*(\d{2}:\d{2}) (.*) \(([A-Z]{3})\)(.*)\n/);
res.reservationFor.arrivalAirport.name = arr[2];
res.reservationFor.arrivalAirport.iataCode = arr[3];
res.reservationFor.arrivalTerminal = arr[4];
res.reservationFor.arrivalTime = JsonLd.toDateTime(flight[3] + arr[1], "dd MMM yyyyhh:mm", "en");
if (isNaN(res.reservationFor.arrivalTime.getTime()))
res.reservationFor.arrivalTime= JsonLd.toDateTime(flight[3] + arr[1], "yyyy MMM ddhh:mm", "en");
res.reservationFor.arrivalTime = JsonLd.toDateTime(flight[3] + arr[1], ["dd MMM yyyyhh:mm", "yyyy MMM ddhh:mm"], "en");
var seat = page.text.match(/Seat.* (\d{1,3}[A-J]) .*\n/);
if (seat) {
......
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