Commit a633d6f4 authored by Volker Krause's avatar Volker Krause

Try harder to parse localized three letter month abbreviations

For en_US the data in QLocale matches commonly used three letter month
shortcuts, for other languages the strings in QLocale can be longer. If we
fail to parse a date containing the 'MMM' format, we'll not try to extend
the contained three letter month codes to what QLocale expects.
parent e31a8c45
......@@ -36,7 +36,7 @@ public:
}
Q_INVOKABLE QJSValue newObject(const QString &typeName) const;
Q_INVOKABLE QDateTime toDateTime(const QString &dt, const QString &format, const QString &locale) const;
Q_INVOKABLE QDateTime toDateTime(const QString &dtStr, const QString &format, const QString &localeName) const;
private:
QJSEngine *m_engine;
......@@ -49,9 +49,27 @@ QJSValue JsApi::newObject(const QString &typeName) const
return v;
}
QDateTime JsApi::toDateTime(const QString &dt, const QString &format, const QString &locale) const
QDateTime JsApi::toDateTime(const QString &dtStr, const QString &format, const QString &localeName) const
{
return QLocale(locale).toDateTime(dt, format);
QLocale locale(localeName);
const auto dt = locale.toDateTime(dtStr, format);
if (dt.isValid()) {
return dt;
}
// try harder for the "MMM" month format
// QLocale expects the exact string in QLocale::shortMonthName(), while we often encounter a three
// letter month identifier. For en_US that's the same, for Swedish it isn't though for example. So
// let's try to fix up the month identifiers to the full short name.
if (format.contains(QLatin1String("MMM"))) {
auto dtStrFixed = dtStr;
for (int i = 0; i < 12; ++i) {
const auto monthName = locale.monthName(i, QLocale::ShortFormat);
dtStrFixed = dtStrFixed.replace(monthName.left(3), monthName);
}
return locale.toDateTime(dtStrFixed, format);
}
return dt;
}
ExtractorEngine::ExtractorEngine() = default;
......
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