Commit 163e7d0d authored by David Jarvie's avatar David Jarvie

Fix RFC3339 fractional seconds handling

parent 6585f2a0
Pipeline #46667 passed with stage
in 5 minutes and 46 seconds
......@@ -2,7 +2,7 @@
This file is part of kalarmcal library, which provides access to KAlarm
calendar data.
SPDX-FileCopyrightText: 2005-2019 David Jarvie <djarvie@kde.org>
SPDX-FileCopyrightText: 2005-2020 David Jarvie <djarvie@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -3089,9 +3089,9 @@ void KADateTimeTest::strings_rfc3339()
::tzset();
bool negZero = true;
KADateTime dtlocal(QDate(1999, 2, 9), QTime(3, 45, 6), KADateTime::LocalZone);
KADateTime dtlocal(QDate(1999, 2, 9), QTime(3, 45, 6, 236), KADateTime::LocalZone);
QString s = dtlocal.toString(KADateTime::RFC3339Date);
QCOMPARE(s, QStringLiteral("1999-02-09T03:45:06-08:00"));
QCOMPARE(s, QStringLiteral("1999-02-09T03:45:06.236-08:00"));
KADateTime dtlocal1 = KADateTime::fromString(s, KADateTime::RFC3339Date, &negZero);
QCOMPARE(dtlocal1.qDateTime().toUTC(), dtlocal.qDateTime().toUTC());
QCOMPARE(dtlocal1.timeType(), KADateTime::OffsetFromUTC);
......@@ -3102,9 +3102,9 @@ void KADateTimeTest::strings_rfc3339()
s = dtlocal.toString(KADateTime::RFC3339Date);
QCOMPARE(s, QStringLiteral("1999-02-09T00:00:00-08:00"));
KADateTime dtzone(QDate(1999, 6, 9), QTime(3, 45, 06), london);
KADateTime dtzone(QDate(1999, 6, 9), QTime(3, 45, 06, 230), london);
s = dtzone.toString(KADateTime::RFC3339Date);
QCOMPARE(s, QStringLiteral("1999-06-09T03:45:06+01:00"));
QCOMPARE(s, QStringLiteral("1999-06-09T03:45:06.23+01:00"));
KADateTime dtzone1 = KADateTime::fromString(s, KADateTime::RFC3339Date);
QCOMPARE(dtzone1.qDateTime().toUTC(), dtzone.qDateTime().toUTC());
QCOMPARE(dtzone1.timeType(), KADateTime::OffsetFromUTC);
......@@ -3114,6 +3114,12 @@ void KADateTimeTest::strings_rfc3339()
s = dtzone.toString(KADateTime::RFC3339Date);
QCOMPARE(s, QStringLiteral("1999-06-09T00:00:00+01:00"));
KADateTime dtzone2(QDate(1999, 6, 9), QTime(3, 45, 06, 200), london);
s = dtzone2.toString(KADateTime::RFC3339Date);
QCOMPARE(s, QStringLiteral("1999-06-09T03:45:06.2+01:00"));
KADateTime dtzone3 = KADateTime::fromString(s, KADateTime::RFC3339Date);
QCOMPARE(dtzone3.qDateTime().toUTC(), dtzone2.qDateTime().toUTC());
KADateTime dtutc(QDate(1999, 2, 9), QTime(3, 45, 00), KADateTime::UTC);
s = dtutc.toString(KADateTime::RFC3339Date);
QCOMPARE(s, QStringLiteral("1999-02-09T03:45:00Z"));
......
......@@ -3,7 +3,7 @@
This file is part of kalarmcal library, which provides access to KAlarm
calendar data. Qt5 version of KDE 4 kdelibs/kdecore/date/kdatetime.cpp.
SPDX-FileCopyrightText: 2005-2019 David Jarvie <djarvie@kde.org>
SPDX-FileCopyrightText: 2005-2020 David Jarvie <djarvie@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -1777,11 +1777,12 @@ QString KADateTime::toString(TimeFormat format) const
int msec = d->time().msec();
if (msec) {
int digits = 3;
if (!(msec % 10))
if (!(msec % 10)) {
msec /= 10, --digits;
if (!(msec % 10))
msec /= 10, --digits;
result += QStringLiteral(".%1").arg(numString(d->time().msec(), digits));
if (!(msec % 10))
msec /= 10, --digits;
}
result += QStringLiteral(".%1").arg(numString(msec, digits));
}
if (d->specType == UTC)
return result + QLatin1Char('Z');
......
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