summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVolker Krause <[email protected]>2017-12-02 10:44:39 +0100
committerVolker Krause <[email protected]>2017-12-02 10:44:39 +0100
commita631be965cb793b2820f1c70500dd939ac4e187c (patch)
tree94b75fa4713718945c265a211071316a3b12249a
parentd6274fd799ead3053d012aee9c1c5df848ced8d7 (diff)
Allow to specify the locale used for parsing dates
This fixes parsing e.g. English booking emails in a French locale.
-rw-r--r--plugins/messageviewer/bodypartformatter/autotests/unstructureddataextractortest.cpp4
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/extractorrule.cpp9
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/extractorrule.h3
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/rules/amadeus.xml4
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/rules/brusselsairlines.xml4
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/rules/eurowings.xml4
-rw-r--r--plugins/messageviewer/bodypartformatter/semantic/rules/fcmtravel.xml4
7 files changed, 22 insertions, 10 deletions
diff --git a/plugins/messageviewer/bodypartformatter/autotests/unstructureddataextractortest.cpp b/plugins/messageviewer/bodypartformatter/autotests/unstructureddataextractortest.cpp
index a26d096..a3e8289 100644
--- a/plugins/messageviewer/bodypartformatter/autotests/unstructureddataextractortest.cpp
+++ b/plugins/messageviewer/bodypartformatter/autotests/unstructureddataextractortest.cpp
@@ -32,9 +32,11 @@ class UnstructuredDataExtractorTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
- void init()
+ void initTestCase()
{
Q_INIT_RESOURCE(rules);
+ // use some exotic locale to ensure the date/time parsing doesn't just work by luck
+ QLocale::setDefault(QLocale(QStringLiteral("fr_FR")));
}
void testExtractText_data()
diff --git a/plugins/messageviewer/bodypartformatter/semantic/extractorrule.cpp b/plugins/messageviewer/bodypartformatter/semantic/extractorrule.cpp
index 90f79ca..d172f9d 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/extractorrule.cpp
+++ b/plugins/messageviewer/bodypartformatter/semantic/extractorrule.cpp
@@ -71,6 +71,11 @@ QString ExtractorRule::format() const
return m_format;
}
+QLocale ExtractorRule::locale() const
+{
+ return m_locale;
+}
+
bool ExtractorRule::load(QXmlStreamReader &reader)
{
m_name = reader.attributes().value(QLatin1String("name")).toString();
@@ -82,6 +87,8 @@ bool ExtractorRule::load(QXmlStreamReader &reader)
if (!m_regexp.isValid()) {
qCWarning(SEMANTIC_LOG) << m_regexp.errorString() << m_regexp.pattern() << "at offset" << m_regexp.patternErrorOffset();
}
+ if (reader.attributes().hasAttribute(QLatin1String("locale")))
+ m_locale = QLocale(reader.attributes().value(QLatin1String("locale")).toString());
return true;
}
@@ -163,7 +170,7 @@ bool ExtractorPropertyRule::match(ExtractorContext *context) const
if (res.hasMatch()) {
auto val = value(res, context);
if (type() == QLatin1String("dateTime") && !format().isEmpty()) {
- const auto dt = QDateTime::fromString(val, format());
+ const auto dt = locale().toDateTime(val, format());
val = dt.toString(Qt::ISODate);
}
diff --git a/plugins/messageviewer/bodypartformatter/semantic/extractorrule.h b/plugins/messageviewer/bodypartformatter/semantic/extractorrule.h
index add2e0b..6086f84 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/extractorrule.h
+++ b/plugins/messageviewer/bodypartformatter/semantic/extractorrule.h
@@ -20,6 +20,7 @@
#ifndef EXTRACTORRULE_H
#define EXTRACTORRULE_H
+#include <QLocale>
#include <QRegularExpression>
#include <QString>
#include <QVector>
@@ -42,6 +43,7 @@ public:
protected:
QString value(const QRegularExpressionMatch &match, ExtractorContext *context) const;
QString format() const;
+ QLocale locale() const;
QRegularExpression m_regexp;
@@ -50,6 +52,7 @@ private:
QString m_type;
QString m_value;
QString m_format;
+ QLocale m_locale;
bool m_repeat = false;
};
diff --git a/plugins/messageviewer/bodypartformatter/semantic/rules/amadeus.xml b/plugins/messageviewer/bodypartformatter/semantic/rules/amadeus.xml
index 572d677..58c1534 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/rules/amadeus.xml
+++ b/plugins/messageviewer/bodypartformatter/semantic/rules/amadeus.xml
@@ -16,11 +16,11 @@
<class type="Airport" name="departureAirport">
<property name="name" match="DEPARTURE: (.*?) [ -]" value="${1}"/>
</class>
- <property name="departureTime" match="([0-9]{2}) ([A-Z]{3}) ([0-9]{2}:[0-9]{2})" value="${1} ${2} ${year} ${3}" type="dateTime" format="dd MMM yyyy hh:mm"/>
+ <property name="departureTime" match="([0-9]{2}) ([A-Z]{3}) ([0-9]{2}:[0-9]{2})" value="${1} ${2} ${year} ${3}" type="dateTime" format="dd MMM yyyy hh:mm" locale="en"/>
<class type="Airport" name="arrivalAirport">
<property name="name" match="ARRIVAL: *(.*?) [ -]" value="${1}"/>
</class>
- <property name="arrivalTime" match="([0-9]{2}) ([A-Z]{3}) ([0-9]{2}:[0-9]{2})" value="${1} ${2} ${year} ${3}" type="dateTime" format="dd MMM yyyy hh:mm"/>
+ <property name="arrivalTime" match="([0-9]{2}) ([A-Z]{3}) ([0-9]{2}:[0-9]{2})" value="${1} ${2} ${year} ${3}" type="dateTime" format="dd MMM yyyy hh:mm" locale="en"/>
</class>
</class>
</extractor>
diff --git a/plugins/messageviewer/bodypartformatter/semantic/rules/brusselsairlines.xml b/plugins/messageviewer/bodypartformatter/semantic/rules/brusselsairlines.xml
index 53a9a26..f494faa 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/rules/brusselsairlines.xml
+++ b/plugins/messageviewer/bodypartformatter/semantic/rules/brusselsairlines.xml
@@ -9,11 +9,11 @@
<class type="Airport" name="departureAirport">
<property name="name" match="([A-Z][\S ]*)\n" value="${1}"/>
</class>
- <property name="departureTime" match="([0-9]{2} [A-Za-z]{3} [0-9]{4}),\s*([0-9]{2}:[0-9]{2})" value="${1} ${2}" type="dateTime" format="dd MMM yyyy hh:mm"/>
+ <property name="departureTime" match="([0-9]{2} [A-Za-z]{3} [0-9]{4}),\s*([0-9]{2}:[0-9]{2})" value="${1} ${2}" type="dateTime" format="dd MMM yyyy hh:mm" locale="en"/>
<class type="Airport" name="arrivalAirport">
<property name="name" match="([A-Z][\S ]*)\n" value="${1}"/>
</class>
- <property name="arrivalTime" match="([0-9]{2} [A-Za-z]{3} [0-9]{4}),\s*([0-9]{2}:[0-9]{2})" value="${1} ${2}" type="dateTime" format="dd MMM yyyy hh:mm"/>
+ <property name="arrivalTime" match="([0-9]{2} [A-Za-z]{3} [0-9]{4}),\s*([0-9]{2}:[0-9]{2})" value="${1} ${2}" type="dateTime" format="dd MMM yyyy hh:mm" locale="en"/>
<variable name="airlineCode" match="[A-Z0-9]{2}" value="${0}"/>
<property name="flightNumber" match="[0-9]{3,4}" value="${0}"/>
diff --git a/plugins/messageviewer/bodypartformatter/semantic/rules/eurowings.xml b/plugins/messageviewer/bodypartformatter/semantic/rules/eurowings.xml
index cd3237c..d3e14f2 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/rules/eurowings.xml
+++ b/plugins/messageviewer/bodypartformatter/semantic/rules/eurowings.xml
@@ -13,11 +13,11 @@
<property name="iataCode" value="${airlineCode}"/>
<property name="name" match="operated by (.*)\n" value="${1}"/>
</class>
- <property name="departureTime" match="Departure:\s*([0-9]{2}:[0-9]{2})" value="${date} ${1}" type="dateTime" format="dd.MM.yyyy hh:mm"/>
+ <property name="departureTime" match="Departure:\s*([0-9]{2}:[0-9]{2})" value="${date} ${1}" type="dateTime" format="dd.MM.yyyy hh:mm" locale="en"/>
<class type="Airport" name="departureAirport">
<property name="name" match="\s*(.*)\n" value="${1}"/>
</class>
- <property name="arrivalTime" match="Arrival:\s*([0-9]{2}:[0-9]{2})" value="${date} ${1}" type="dateTime" format="dd.MM.yyyy hh:mm"/>
+ <property name="arrivalTime" match="Arrival:\s*([0-9]{2}:[0-9]{2})" value="${date} ${1}" type="dateTime" format="dd.MM.yyyy hh:mm" locale="en"/>
<class type="Airport" name="arrivalAirport">
<property name="name" match="\s*(.*)\n" value="${1}"/>
</class>
diff --git a/plugins/messageviewer/bodypartformatter/semantic/rules/fcmtravel.xml b/plugins/messageviewer/bodypartformatter/semantic/rules/fcmtravel.xml
index f14bf8a..b151ddb 100644
--- a/plugins/messageviewer/bodypartformatter/semantic/rules/fcmtravel.xml
+++ b/plugins/messageviewer/bodypartformatter/semantic/rules/fcmtravel.xml
@@ -14,13 +14,13 @@
<property name="name" match=", ([A-Za-z0-9 ]*)\n" value="${1}"/>
</class>
<variable name="depDate" match="Date: *[A-Z][a-z]{2} (.*)\n" value="${1}"/>
- <property name="departureTime" match="Departure: *([0-9]+:[0-9]+)" value="${depDate} ${1}" type="dateTime" format="d MMM yyyy hh:mm"/>
+ <property name="departureTime" match="Departure: *([0-9]+:[0-9]+)" value="${depDate} ${1}" type="dateTime" format="d MMM yyyy hh:mm" locale="en"/>
<class type="Airport" name="departureAirport">
<property name="name" match=" *(.*)\(" value="${1}"/>
<property name="iataCode" match="[A-Z]{3}" value="${0}"/>
</class>
<variable name="arrDate" match="Date: *[A-Z][a-z]{2} (.*)\n" value="${1}"/>
- <property name="arrivalTime" match="Arrival: *([0-9]+:[0-9]+)" value="${arrDate} ${1}" type="dateTime" format="d MMM yyyy hh:mm"/>
+ <property name="arrivalTime" match="Arrival: *([0-9]+:[0-9]+)" value="${arrDate} ${1}" type="dateTime" format="d MMM yyyy hh:mm" locale="en"/>
<class type="Airport" name="arrivalAirport">
<property name="name" match=" *(.*)\(" value="${1}"/>
<property name="iataCode" match="[A-Z]{3}" value="${0}"/>