Commit 2e9d47b7 authored by Volker Krause's avatar Volker Krause
Browse files

Support date-only field formatting

This needs a bit of heuristics as real-world pass files don't always follow
the specification for this.
parent 06052046
Pipeline #67827 passed with stage
in 2 minutes and 30 seconds
......@@ -44,6 +44,12 @@ private Q_SLOTS:
QCOMPARE(f.value(), QDateTime({2021, 6, 27}, {14, 30}, Qt::OffsetFromUTC, 7200));
QCOMPARE(f.valueDisplayString(), QLatin1String("27/06/2021 14:30"));
obj = QJsonDocument::fromJson(R"({"key":"valid-date","label":"Datum","dateStyle":"PKDateStyleShort","value":"2021-06-27T00:00:00+02:00"})").object();
f = KPkPass::Field(obj, pass.get());
QCOMPARE(f.value().type(), QVariant::DateTime);
QCOMPARE(f.value().toDateTime().date(), QDate({2021, 6, 27}));
QCOMPARE(f.valueDisplayString(), QLatin1String("27/06/2021"));
obj = QJsonDocument::fromJson(R"({"key":"valid-locations","label":"Ort","value":"Freibad Killesberg\n"})").object();
f = KPkPass::Field(obj, pass.get());
......
......@@ -75,13 +75,22 @@ QVariant Field::value() const
QString Field::valueDisplayString() const
{
const auto v = value();
// see
// https://developer.apple.com/library/archive/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/FieldDictionary.html#//apple_ref/doc/uid/TP40012026-CH4-SW6
// however, real-world data doesn't strictly follow that, so we have to guess a bit here...
if (v.type() == QVariant::DateTime) {
const auto dt = v.toDateTime();
auto fmt = QLocale::ShortFormat;
const auto dtStyle = d->obj.value(QLatin1String("dateStyle")).toString();
if (dtStyle == QLatin1String("PKDateStyleLong") || dtStyle == QLatin1String("PKDateStyleFull")) {
fmt = QLocale::LongFormat;
}
return QLocale().toString(v.toDateTime(), fmt);
const auto timeStyle = d->obj.value(QLatin1String("timeStyle")).toString();
if (timeStyle == QLatin1String("PKDateStyleNone") || (timeStyle.isEmpty() && !dtStyle.isEmpty() && dt.time() == QTime(0, 0))) {
return QLocale().toString(dt.date(), fmt);
}
return QLocale().toString(dt, fmt);
}
// TODO respect number formatting options
......
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