summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2020-09-30 12:27:59 +0200
committerThiago Macieira <thiago.macieira@intel.com>2020-10-07 13:58:33 +0000
commite5dc46d966a0c98eee4360444963b4d0c3ab9e49 (patch)
tree11b09bfc1095a16d914538055bd55abbbecd2ecc /src
parent51a16a686209452e99e281fc1d80170e87ff6fd2 (diff)
Allow millisecond-overflow when the result remains valid
Even before adding support for fractional hours, a fraction of a minute might potentially have represented a whole number of seconds by a fractional part that, due to rounding, was less than the whole number of seconds by less than half a millisecond. Previously, the parsing would have clipped the fractional part at 999 milliseconds, in the preceding second, instead of correctly rounding it up to the whole second. For QTime::fromString(), which can't represent 24:00, and for TextDate, which doesn't allow 24:00 as a synomym for the next day's 0:0, applying such rounding to 23:59:59.999999 would produce an invalid result from a string that does represent a valid time, so use the nearest representable time, as previously. Added some tests and amended others. [ChangeLog][QtCore][QDateTime] QDateTime and QTime, in fromString() with format ISODate or TextDate, now allow a fractional part of the hour, minute or seconds to round up to the next second (hence potentially into the next minute, etc.) when this is the closest representable value to the exact fractional part given. When rounding up would turn a valid result into an invalid one, however, the old behavior of clipping to 999 milliseconds is retained. Change-Id: I8104848d246cdb4545a12819fb4b6755da2b1372 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/global/qnamespace.qdoc23
-rw-r--r--src/corelib/time/qdatetime.cpp36
2 files changed, 39 insertions, 20 deletions
diff --git a/src/corelib/global/qnamespace.qdoc b/src/corelib/global/qnamespace.qdoc
index a877b17a4f..3cddb711c5 100644
--- a/src/corelib/global/qnamespace.qdoc
+++ b/src/corelib/global/qnamespace.qdoc
@@ -709,18 +709,17 @@
fractional part suffix on the preceding field, which may be separated from
that field either by a comma \c{','} or the dot \c{'.'} shown. Precision
beyond milliseconds is accepted but discarded, rounding to the nearest
- millisecond or, when rounding fractional seconds up would change the second
- field, rounded down. The presence of a literal \c T character is used to
- separate the date and time when both are specified. For the \c TextDate and
- \c RFC2822Date formats, \c{ddd} stands for the first three letters of the
- name of the day of the week and \c{MMM} stands for the first three letters
- of the month name. The names of days and months are always in English (C
- locale) regardless of user preferences or system settings. The other format
- characters have the same meaning as for the ISODate format, except that 24
- is not accepted as an hour. Parts of a format enclosed in square brackets
- \c{[...]} are optional; the square brackets do not form part of the
- format. The plus-or-minus character \c{'±'} here stands for either sign
- character, \c{'-'} for minus or \c{'+'} for plus.
+ representable millisecond. The presence of a literal \c T character is used
+ to separate the date and time when both are specified. For the \c TextDate
+ and \c RFC2822Date formats, \c{ddd} stands for the first three letters of
+ the name of the day of the week and \c{MMM} stands for the first three
+ letters of the month name. The names of days and months are always in
+ English (C locale) regardless of user preferences or system settings. The
+ other format characters have the same meaning as for the ISODate format,
+ except that 24 is not accepted as an hour. Parts of a format enclosed in
+ square brackets \c{[...]} are optional; the square brackets do not form part
+ of the format. The plus-or-minus character \c{'±'} here stands for either
+ sign character, \c{'-'} for minus or \c{'+'} for plus.
\sa QDate::toString(), QTime::toString(), QDateTime::toString(),
QDate::fromString(), QTime::fromString(), QDateTime::fromString()
diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp
index da3fee5b81..96932ff129 100644
--- a/src/corelib/time/qdatetime.cpp
+++ b/src/corelib/time/qdatetime.cpp
@@ -2113,6 +2113,7 @@ int QTime::msecsTo(QTime t) const
static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *isMidnight24)
{
+ Q_ASSERT(format == Qt::TextDate || format == Qt::ISODate || format == Qt::ISODateWithMs);
if (isMidnight24)
*isMidnight24 = false;
// Match /\d\d(:\d\d(:\d\d)?)?([,.]\d+)?/ as "HH[:mm[:ss]][.zzz]"
@@ -2145,14 +2146,14 @@ static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *
return QTime();
ParsedInt hour = readInt(string.first(2));
- if (!hour.ok)
+ if (!hour.ok || hour.value > (format == Qt::TextDate ? 23 : 24))
return QTime();
ParsedInt minute;
if (string.size() > 2) {
if (string[2] == u':' && string.size() > 4)
minute = readInt(string.sliced(3, 2));
- if (!minute.ok)
+ if (!minute.ok || minute.value >= 60)
return QTime();
} else if (format == Qt::TextDate) { // Requires minutes
return QTime();
@@ -2167,7 +2168,7 @@ static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *
if (string.size() > 5) {
if (string[5] == u':' && string.size() == 8)
second = readInt(string.sliced(6, 2));
- if (!second.ok)
+ if (!second.ok || second.value >= 60)
return QTime();
} else if (frac.ok) {
if (format == Qt::TextDate) // Doesn't allow fraction of minutes
@@ -2179,13 +2180,32 @@ static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *
}
Q_ASSERT(!(fraction < 0.0) && fraction < 1.0);
- // Round millis to nearest (unlike minutes and seconds, rounded down),
- // but clip to 999 (historical behavior):
- const int msec = frac.ok ? qMin(qRound(1000 * fraction), 999) : 0;
+ // Round millis to nearest (unlike minutes and seconds, rounded down):
+ int msec = frac.ok ? qRound(1000 * fraction) : 0;
+ // But handle overflow gracefully:
+ if (msec == 1000) {
+ // If we can (when data were otherwise valid) validly propagate overflow
+ // into other fields, do so:
+ if (isMidnight24 || hour.value < 23 || minute.value < 59 || second.value < 59) {
+ msec = 0;
+ if (++second.value == 60) {
+ second.value = 0;
+ if (++minute.value == 60) {
+ minute.value = 0;
+ ++hour.value;
+ // May need to propagate further via isMidnight24, see below
+ }
+ }
+ } else {
+ // QTime::fromString() or Qt::TextDate: rounding up would cause
+ // 23:59:59.999... to become invalid; clip to 999 ms instead:
+ msec = 999;
+ }
+ }
// For ISO date format, 24:0:0 means 0:0:0 on the next day:
- if ((format == Qt::ISODate || format == Qt::ISODateWithMs)
- && hour.value == 24 && minute.value == 0 && second.value == 0 && msec == 0) {
+ if (hour.value == 24 && minute.value == 0 && second.value == 0 && msec == 0) {
+ Q_ASSERT(format != Qt::TextDate); // It clipped hour at 23, above.
if (isMidnight24)
*isMidnight24 = true;
hour.value = 0;