]> dgit.raspbian.org Git - nextcloud-desktop.git/commitdiff
Ensure leading zero dates are also correctly parsed
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Tue, 8 Aug 2023 10:48:35 +0000 (18:48 +0800)
committerbackportbot-nextcloud[bot] <backportbot-nextcloud[bot]@users.noreply.github.com>
Wed, 13 Sep 2023 08:47:54 +0000 (08:47 +0000)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/filedetails/datefieldbackend.cpp
src/gui/filedetails/datefieldbackend.h

index 6c043697809fb3bdb920b8d02207c80253f7fd66..89556c8cc3e18e7e3d80fa11be5cc32b49a13607 100644 (file)
@@ -25,16 +25,29 @@ namespace Quick
 DateFieldBackend::DateFieldBackend(QObject *const parent)
     : QObject(parent)
 {
+    _dateFormat = QLocale::system().dateFormat(QLocale::ShortFormat);
+
     // Ensure the date format is for a full year. QLocale::ShortFormat often
     // provides a short year format that is only two years, which is an absolute
-    // pain to work with -- ensure instead we have the full, unambiguous year
-    _dateFormat = QLocale::system().dateFormat(QLocale::ShortFormat);
+    // pain to work with -- ensure instead we have the full, unambiguous year.
     // Check for specifically two y's, no more and no fewer, within format date
-    const QRegularExpression re("(?<!y)y{2}(?!y)");
+    const QRegularExpression yearRe("(?<!y)y{2}(?!y)");
 
-    if (auto match = re.match(_dateFormat); match.hasMatch()) {
+    // To prevent invalid parsings when the user submits a month with a leading
+    // zero, also add an alternative date format that checks with a leading zero
+    // This regex only matches, e.g. dd/M/yyyy which often is the default for
+    // short locale date formats, which removes the leading 0
+    const QRegularExpression monthRe("(?<!M)M{1}(?!M)");
+
+    if (const auto match = yearRe.match(_dateFormat); match.hasMatch()) {
         _dateFormat.replace(match.capturedStart(), match.capturedLength(), "yyyy");
     }
+
+    _leadingZeroMonthDateFormat = _dateFormat;
+
+    if (const auto match = monthRe.match(_dateFormat); match.hasMatch()) {
+        _leadingZeroMonthDateFormat.replace(match.capturedStart(), match.capturedLength(), "MM");
+    }
 }
 
 QDate DateFieldBackend::date() const
@@ -79,7 +92,12 @@ QString DateFieldBackend::dateString() const
 void DateFieldBackend::setDateString(const QString &dateString)
 {
     const auto locale = QLocale::system();
-    const auto date = locale.toDate(dateString, _dateFormat);
+    auto date = locale.toDate(dateString, _dateFormat);
+
+    if (!date.isValid()) {
+        date = locale.toDate(dateString, _leadingZeroMonthDateFormat);
+    }
+
     setDate(date);
 }
 
index 5ed813c47a579ffc88334ac6bb6c3caf27f65aa8..b44fc6822cd4f5cd09a1614ed6ad78a6045f45ec 100644 (file)
@@ -83,6 +83,7 @@ private:
     QDate _maximumDate;
 
     QString _dateFormat;
+    QString _leadingZeroMonthDateFormat;
 };
 
 } // namespace Quick