Add leading zeros to years below 1000 in QDate::toString().
authorMitch Curtis <mitch.curtis@nokia.com>
Fri, 8 Jun 2012 11:36:16 +0000 (13:36 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 20 Jun 2012 10:38:32 +0000 (12:38 +0200)
QDate::toString(Qt::ISODate) lacks prefixed 0's on years below 1000.
The ISO 8601 standard dictates that this should be the case.

Task-number: QTBUG-16476
Change-Id: I7e73152bba0f5894bcbaa3f4418732b74ce86bc5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/tools/qdatetime.cpp
tests/auto/corelib/tools/qdate/tst_qdate.cpp

index 021053f..8c139ea 100644 (file)
@@ -767,9 +767,10 @@ QString QDate::toString(Qt::DateFormat f) const
         {
             if (year() < 0 || year() > 9999)
                 return QString();
+            QString year(QString::number(y).rightJustified(4, QLatin1Char('0')));
             QString month(QString::number(m).rightJustified(2, QLatin1Char('0')));
             QString day(QString::number(d).rightJustified(2, QLatin1Char('0')));
-            return QString::number(y) + QLatin1Char('-') + month + QLatin1Char('-') + day;
+            return year + QLatin1Char('-') + month + QLatin1Char('-') + day;
         }
     }
 }
index 1c8c515..f83e206 100644 (file)
@@ -47,7 +47,6 @@ class tst_QDate : public QObject
 {
     Q_OBJECT
 private slots:
-    void toString();
     void isNull_data();
     void isNull();
     void isValid_data();
@@ -86,6 +85,8 @@ private slots:
     void fromStringFormat();
     void toStringFormat_data();
     void toStringFormat();
+    void toStringDateFormat_data();
+    void toStringDateFormat();
     void isLeapYear();
     void yearsZeroToNinetyNine();
     void negativeYear() const;
@@ -106,6 +107,7 @@ private:
 };
 
 Q_DECLARE_METATYPE(QDate)
+Q_DECLARE_METATYPE(Qt::DateFormat)
 
 void tst_QDate::isNull_data()
 {
@@ -960,6 +962,33 @@ void tst_QDate::toStringFormat()
     QCOMPARE( t.toString( format ), str );
 }
 
+void tst_QDate::toStringDateFormat_data()
+{
+    QTest::addColumn<QDate>("date");
+    QTest::addColumn<Qt::DateFormat>("format");
+    QTest::addColumn<QString>("expectedStr");
+
+    QTest::newRow("data0") << QDate(1,1,1) << Qt::ISODate << QString("0001-01-01");
+    QTest::newRow("data1") << QDate(11,1,1) << Qt::ISODate << QString("0011-01-01");
+    QTest::newRow("data2") << QDate(111,1,1) << Qt::ISODate << QString("0111-01-01");
+    QTest::newRow("data3") << QDate(1974,12,1) << Qt::ISODate << QString("1974-12-01");
+}
+
+void tst_QDate::toStringDateFormat()
+{
+    QFETCH(QDate, date);
+    QFETCH(Qt::DateFormat, format);
+    QFETCH(QString, expectedStr);
+
+    QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
+    QCOMPARE(date.toString(Qt::LocaleDate), QLocale().toString(date, QLocale::ShortFormat));
+    QLocale::setDefault(QLocale::German);
+    QCOMPARE(date.toString(Qt::SystemLocaleShortDate), QLocale::system().toString(date, QLocale::ShortFormat));
+    QCOMPARE(date.toString(Qt::LocaleDate), QLocale().toString(date, QLocale::ShortFormat));
+
+    QCOMPARE(date.toString(format), expectedStr);
+}
+
 void tst_QDate::isLeapYear()
 {
     QVERIFY(QDate::isLeapYear(-4801));
@@ -1053,19 +1082,6 @@ void tst_QDate::yearsZeroToNinetyNine()
     }
 }
 
-void tst_QDate::toString()
-{
-    QDate date(1974,12,1);
-    QCOMPARE(date.toString(Qt::SystemLocaleDate),
-                QLocale::system().toString(date, QLocale::ShortFormat));
-    QCOMPARE(date.toString(Qt::LocaleDate),
-                QLocale().toString(date, QLocale::ShortFormat));
-    QLocale::setDefault(QLocale::German);
-    QCOMPARE(date.toString(Qt::SystemLocaleDate),
-                QLocale::system().toString(date, QLocale::ShortFormat));
-    QCOMPARE(date.toString(Qt::LocaleDate),
-                QLocale().toString(date, QLocale::ShortFormat));
-}
 
 void tst_QDate::negativeYear() const
 {