summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@nokia.com>2012-06-08 13:36:16 +0200
committerQt by Nokia <qt-info@nokia.com>2012-06-20 12:38:32 +0200
commit902638e379317dd62bd925c4b9c8f664e0ecd633 (patch)
tree0c2f48cfab4984bc0293f7465bfb8bae43d37895
parent7f139e88444860baf275daaa35b387e7e8a448a3 (diff)
Add leading zeros to years below 1000 in QDate::toString().
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>
-rw-r--r--src/corelib/tools/qdatetime.cpp3
-rw-r--r--tests/auto/corelib/tools/qdate/tst_qdate.cpp44
2 files changed, 32 insertions, 15 deletions
diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
index 021053fc6d..8c139eab03 100644
--- a/src/corelib/tools/qdatetime.cpp
+++ b/src/corelib/tools/qdatetime.cpp
@@ -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;
}
}
}
diff --git a/tests/auto/corelib/tools/qdate/tst_qdate.cpp b/tests/auto/corelib/tools/qdate/tst_qdate.cpp
index 1c8c515f22..f83e2064c8 100644
--- a/tests/auto/corelib/tools/qdate/tst_qdate.cpp
+++ b/tests/auto/corelib/tools/qdate/tst_qdate.cpp
@@ -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
{