diff options
Diffstat (limited to 'src/corelib/time')
-rw-r--r-- | src/corelib/time/qdatetime.cpp | 194 | ||||
-rw-r--r-- | src/corelib/time/qdatetimeparser.cpp | 14 | ||||
-rw-r--r-- | src/corelib/time/qtimezoneprivate.cpp | 13 | ||||
-rw-r--r-- | src/corelib/time/qtimezoneprivate_android.cpp | 52 | ||||
-rw-r--r-- | src/corelib/time/qtimezoneprivate_mac.mm | 47 |
5 files changed, 192 insertions, 128 deletions
diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 878a2c1e46..a8d643d483 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -127,7 +127,7 @@ static const char qt_shortMonthNames[][4] = { "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -static int qt_monthNumberFromShortName(QStringRef shortName) +static int qt_monthNumberFromShortName(QStringView shortName) { for (unsigned int i = 0; i < sizeof(qt_shortMonthNames) / sizeof(qt_shortMonthNames[0]); ++i) { if (shortName == QLatin1String(qt_shortMonthNames[i], 3)) @@ -136,9 +136,9 @@ static int qt_monthNumberFromShortName(QStringRef shortName) return -1; } static int qt_monthNumberFromShortName(const QString &shortName) -{ return qt_monthNumberFromShortName(QStringRef(&shortName)); } +{ return qt_monthNumberFromShortName(QStringView(shortName)); } -static int fromShortMonthName(const QStringRef &monthName, int year) +static int fromShortMonthName(QStringView monthName, int year) { // Assume that English monthnames are the default int month = qt_monthNumberFromShortName(monthName); @@ -164,8 +164,8 @@ static ParsedRfcDateTime rfcDateImpl(const QString &s) { ParsedRfcDateTime result; - // Matches "Wdy, dd Mon yyyy HH:mm:ss ±hhmm" (Wdy, being optional) - QRegExp rex(QStringLiteral("^(?:[A-Z][a-z]+,)?[ \\t]*(\\d{1,2})[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d)(?::(\\d\\d))?)?[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?")); + // Matches "[ddd,] dd MMM yyyy[ hh:mm[:ss]] [±hhmm]" - correct RFC 822, 2822, 5322 format + QRegExp rex(QStringLiteral("^[ \\t]*(?:[A-Z][a-z]+,)?[ \\t]*(\\d{1,2})[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d)(?::(\\d\\d))?)?[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?")); if (s.indexOf(rex) == 0) { const QStringList cap = rex.capturedTexts(); result.date = QDate(cap[3].toInt(), qt_monthNumberFromShortName(cap[2]), cap[1].toInt()); @@ -176,8 +176,8 @@ static ParsedRfcDateTime rfcDateImpl(const QString &s) const int minOffset = cap[9].toInt(); result.utcOffset = ((hourOffset * 60 + minOffset) * (positiveOffset ? 60 : -60)); } else { - // Matches "Wdy Mon dd HH:mm:ss yyyy" - QRegExp rex(QStringLiteral("^[A-Z][a-z]+[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d):(\\d\\d))?[ \\t]+(\\d\\d\\d\\d)[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?")); + // Matches "ddd MMM dd[ hh:mm:ss] yyyy [±hhmm]" - permissive RFC 850, 1036 (read only) + QRegExp rex(QStringLiteral("^[ \\t]*[A-Z][a-z]+[ \\t]+([A-Z][a-z]+)[ \\t]+(\\d\\d)(?:[ \\t]+(\\d\\d):(\\d\\d):(\\d\\d))?[ \\t]+(\\d\\d\\d\\d)[ \\t]*(?:([+-])(\\d\\d)(\\d\\d))?")); if (s.indexOf(rex) == 0) { const QStringList cap = rex.capturedTexts(); result.date = QDate(cap[6].toInt(), qt_monthNumberFromShortName(cap[1]), cap[2].toInt()); @@ -207,7 +207,7 @@ static QString toOffsetString(Qt::DateFormat format, int offset) #if QT_CONFIG(datestring) // Parse offset in [+-]HH[[:]mm] format -static int fromOffsetString(const QStringRef &offsetString, bool *valid) noexcept +static int fromOffsetString(QStringView offsetString, bool *valid) noexcept { *valid = false; @@ -228,22 +228,23 @@ static int fromOffsetString(const QStringRef &offsetString, bool *valid) noexcep return 0; // Split the hour and minute parts - const QStringRef time = offsetString.mid(1); - int hhLen = time.indexOf(QLatin1Char(':')); - int mmIndex; + const QStringView time = offsetString.mid(1); + qsizetype hhLen = time.indexOf(QLatin1Char(':')); + qsizetype mmIndex; if (hhLen == -1) mmIndex = hhLen = 2; // [+-]HHmm or [+-]HH format else mmIndex = hhLen + 1; - const QStringRef hhRef = time.left(hhLen); + const QLocale C = QLocale::c(); + const QStringView hhRef = time.left(qMin(hhLen, time.size())); bool ok = false; - const int hour = hhRef.toInt(&ok); + const int hour = C.toInt(hhRef, &ok); if (!ok) return 0; - const QStringRef mmRef = time.mid(mmIndex); - const int minute = mmRef.isEmpty() ? 0 : mmRef.toInt(&ok); + const QStringView mmRef = time.mid(qMin(mmIndex, time.size())); + const int minute = mmRef.isEmpty() ? 0 : C.toInt(mmRef, &ok); if (!ok || minute < 0 || minute > 59) return 0; @@ -1625,6 +1626,29 @@ qint64 QDate::daysTo(const QDate &d) const */ #if QT_CONFIG(datestring) +namespace { + +struct ParsedInt { int value = 0; bool ok = false; }; + +/* + /internal + + Read an int that must be the whole text. QStringRef::toInt() will ignore + spaces happily; but ISO date format should not. +*/ +ParsedInt readInt(QStringView text) +{ + ParsedInt result; + for (const auto &ch : text) { + if (ch.isSpace()) + return result; + } + result.value = QLocale::c().toInt(text, &result.ok); + return result; +} + +} + /*! Returns the QDate represented by the \a string, using the \a format given, or an invalid date if the string cannot be @@ -1676,17 +1700,18 @@ QDate QDate::fromString(const QString &string, Qt::DateFormat format) return QDate(year, month, day); } #endif // textdate - case Qt::ISODate: { - // Semi-strict parsing, must be long enough and have non-numeric separators - if (string.size() < 10 || string.at(4).isDigit() || string.at(7).isDigit() - || (string.size() > 10 && string.at(10).isDigit())) { - return QDate(); - } - const int year = string.midRef(0, 4).toInt(); - if (year <= 0 || year > 9999) - return QDate(); - return QDate(year, string.midRef(5, 2).toInt(), string.midRef(8, 2).toInt()); + case Qt::ISODate: + // Semi-strict parsing, must be long enough and have punctuators as separators + if (string.size() >= 10 && string.at(4).isPunct() && string.at(7).isPunct() + && (string.size() == 10 || !string.at(10).isDigit())) { + QStringView view(string); + const ParsedInt year = readInt(view.mid(0, 4)); + const ParsedInt month = readInt(view.mid(5, 2)); + const ParsedInt day = readInt(view.mid(8, 2)); + if (year.ok && year.value > 0 && year.value <= 9999 && month.ok && day.ok) + return QDate(year.value, month.value, day.value); } + break; } return QDate(); } @@ -2324,22 +2349,21 @@ int QTime::msecsTo(const QTime &t) const #if QT_CONFIG(datestring) -static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format, bool *isMidnight24) +static QTime fromIsoTimeString(QStringView string, Qt::DateFormat format, bool *isMidnight24) { if (isMidnight24) *isMidnight24 = false; const int size = string.size(); - if (size < 5) + if (size < 5 || string.at(2) != QLatin1Char(':')) return QTime(); - bool ok = false; - int hour = string.mid(0, 2).toInt(&ok); - if (!ok) - return QTime(); - const int minute = string.mid(3, 2).toInt(&ok); - if (!ok) + ParsedInt hour = readInt(string.mid(0, 2)); + ParsedInt minute = readInt(string.mid(3, 2)); + if (!hour.ok || !minute.ok) return QTime(); + // FIXME: ISO 8601 allows [,.]\d+ after hour, just as it does after minute + int second = 0; int msec = 0; @@ -2358,40 +2382,57 @@ static QTime fromIsoTimeString(const QStringRef &string, Qt::DateFormat format, // seconds is 4. E.g. 12:34,99999 will expand to 12:34:59.9994. The milliseconds // will then be rounded up AND clamped to 999. - const QStringRef minuteFractionStr = string.mid(6, 5); - const long minuteFractionInt = minuteFractionStr.toLong(&ok); - if (!ok) + const QStringView minuteFractionStr = string.mid(6, qMin(qsizetype(5), string.size() - 6)); + const ParsedInt parsed = readInt(minuteFractionStr); + if (!parsed.ok) return QTime(); - const float minuteFraction = double(minuteFractionInt) / (std::pow(double(10), minuteFractionStr.count())); + const float secondWithMs + = double(parsed.value) * 60 / (std::pow(double(10), minuteFractionStr.size())); - const float secondWithMs = minuteFraction * 60; - const float secondNoMs = std::floor(secondWithMs); - const float secondFraction = secondWithMs - secondNoMs; - second = secondNoMs; + second = std::floor(secondWithMs); + const float secondFraction = secondWithMs - second; msec = qMin(qRound(secondFraction * 1000.0), 999); - } else { + } else if (string.at(5) == QLatin1Char(':')) { // HH:mm:ss or HH:mm:ss.zzz - second = string.mid(6, 2).toInt(&ok); - if (!ok) + const ParsedInt parsed = readInt(string.mid(6, qMin(qsizetype(2), string.size() - 6))); + if (!parsed.ok) return QTime(); - if (size > 8 && (string.at(8) == QLatin1Char(',') || string.at(8) == QLatin1Char('.'))) { - const QStringRef msecStr(string.mid(9, 4)); - int msecInt = msecStr.isEmpty() ? 0 : msecStr.toInt(&ok); + second = parsed.value; + if (size <= 8) { + // No fractional part to read + } else if (string.at(8) == QLatin1Char(',') || string.at(8) == QLatin1Char('.')) { + QStringView msecStr(string.mid(9, qMin(qsizetype(4), string.size() - 9))); + bool ok = true; + // Can't use readInt() here, as we *do* allow trailing space - but not leading: + if (!msecStr.isEmpty() && !msecStr.at(0).isDigit()) + return QTime(); + msecStr = msecStr.trimmed(); + int msecInt = msecStr.isEmpty() ? 0 : QLocale::c().toInt(msecStr, &ok); if (!ok) return QTime(); - const double secondFraction(msecInt / (std::pow(double(10), msecStr.count()))); + const double secondFraction(msecInt / (std::pow(double(10), msecStr.size()))); msec = qMin(qRound(secondFraction * 1000.0), 999); + } else { +#if QT_VERSION >= QT_VERSION_CHECK(6,0,0) // behavior change + // Stray cruft after date-time: tolerate trailing space, but nothing else. + for (const auto &ch : string.mid(8)) { + if (!ch.isSpace()) + return QTime(); + } +#endif } + } else { + return QTime(); } const bool isISODate = format == Qt::ISODate || format == Qt::ISODateWithMs; - if (isISODate && hour == 24 && minute == 0 && second == 0 && msec == 0) { + if (isISODate && hour.value == 24 && minute.value == 0 && second == 0 && msec == 0) { if (isMidnight24) *isMidnight24 = true; - hour = 0; + hour.value = 0; } - return QTime(hour, minute, second, msec); + return QTime(hour.value, minute.value, second, msec); } /*! @@ -2428,7 +2469,7 @@ QTime QTime::fromString(const QString &string, Qt::DateFormat format) case Qt::ISODateWithMs: case Qt::TextDate: default: - return fromIsoTimeString(QStringRef(&string), format, nullptr); + return fromIsoTimeString(QStringView(string), format, nullptr); } } @@ -3410,6 +3451,7 @@ inline qint64 QDateTimePrivate::zoneMSecsToEpochMSecs(qint64 zoneMSecs, const QT DaylightStatus hint, QDate *zoneDate, QTime *zoneTime) { + Q_ASSERT(zone.isValid()); // Get the effective data from QTimeZone QTimeZonePrivate::Data data = zone.d->dataForLocalTime(zoneMSecs, int(hint)); // Docs state any time before 1970-01-01 will *not* have any DST applied @@ -3799,8 +3841,9 @@ QTimeZone QDateTime::timeZone() const case Qt::OffsetFromUTC: return QTimeZone(d->m_offsetFromUtc); case Qt::TimeZone: - Q_ASSERT(d->m_timeZone.isValid()); - return d->m_timeZone; + if (d->m_timeZone.isValid()) + return d->m_timeZone; + break; case Qt::LocalTime: return QTimeZone::systemTimeZone(); } @@ -3884,6 +3927,7 @@ QString QDateTime::timeZoneAbbreviation() const #if !QT_CONFIG(timezone) break; #else + Q_ASSERT(d->m_timeZone.isValid()); return d->m_timeZone.d->abbreviation(toMSecsSinceEpoch()); #endif // timezone case Qt::LocalTime: { @@ -3920,6 +3964,7 @@ bool QDateTime::isDaylightTime() const #if !QT_CONFIG(timezone) break; #else + Q_ASSERT(d->m_timeZone.isValid()); return d->m_timeZone.d->isDaylightTime(toMSecsSinceEpoch()); #endif // timezone case Qt::LocalTime: { @@ -4044,6 +4089,10 @@ void QDateTime::setTimeZone(const QTimeZone &toZone) */ qint64 QDateTime::toMSecsSinceEpoch() const { + // Note: QDateTimeParser relies on this producing a useful result, even when + // !isValid(), at least when the invalidity is a time in a fall-back (that + // we'll have adjusted to lie outside it, but marked invalid because it's + // not what was asked for). Other things may be doing similar. switch (getSpec(d)) { case Qt::UTC: return getMSecs(d); @@ -4058,12 +4107,13 @@ qint64 QDateTime::toMSecsSinceEpoch() const } case Qt::TimeZone: -#if !QT_CONFIG(timezone) - return 0; -#else - return QDateTimePrivate::zoneMSecsToEpochMSecs(d->m_msecs, d->m_timeZone, - extractDaylightStatus(getStatus(d))); +#if QT_CONFIG(timezone) + if (d->m_timeZone.isValid()) { + return QDateTimePrivate::zoneMSecsToEpochMSecs(d->m_msecs, d->m_timeZone, + extractDaylightStatus(getStatus(d))); + } #endif + return 0; } Q_UNREACHABLE(); return 0; @@ -4158,9 +4208,11 @@ void QDateTime::setMSecsSinceEpoch(qint64 msecs) case Qt::TimeZone: Q_ASSERT(!d.isShort()); #if QT_CONFIG(timezone) + d.detach(); + if (!d->m_timeZone.isValid()) + break; // Docs state any LocalTime before 1970-01-01 will *not* have any DST applied // but all affected times afterwards will have DST applied. - d.detach(); if (msecs >= 0) { status = mergeDaylightStatus(status, d->m_timeZone.d->isDaylightTime(msecs) @@ -4433,7 +4485,7 @@ static inline void massageAdjustedDateTime(const QDateTimeData &d, QDate *date, QDateTimePrivate::DaylightStatus status = QDateTimePrivate::UnknownDaylightTime; localMSecsToEpochMSecs(timeToMSecs(*date, *time), &status, date, time); #if QT_CONFIG(timezone) - } else if (spec == Qt::TimeZone) { + } else if (spec == Qt::TimeZone && d->m_timeZone.isValid()) { QDateTimePrivate::zoneMSecsToEpochMSecs(timeToMSecs(*date, *time), d->m_timeZone, QDateTimePrivate::UnknownDaylightTime, @@ -5094,7 +5146,8 @@ QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, const QTimeZone &timeZone { QDateTime dt; dt.setTimeZone(timeZone); - dt.setMSecsSinceEpoch(msecs); + if (timeZone.isValid()) + dt.setMSecsSinceEpoch(msecs); return dt; } @@ -5197,16 +5250,17 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format) if (!date.isValid()) return QDateTime(); if (size == 10) - return QDateTime(date); + return date.startOfDay(); Qt::TimeSpec spec = Qt::LocalTime; - QStringRef isoString(&string); - isoString = isoString.mid(10); // trim "yyyy-MM-dd" + QStringView isoString = QStringView(string).mid(10); // trim "yyyy-MM-dd" - // Must be left with T and at least one digit for the hour: + // Must be left with T (or space) and at least one digit for the hour: if (isoString.size() < 2 - || !(isoString.startsWith(QLatin1Char('T')) - // FIXME: QSql relies on QVariant::toDateTime() accepting a space here: + || !(isoString.startsWith(QLatin1Char('T'), Qt::CaseInsensitive) + // RFC 3339 (section 5.6) allows a space here. (It actually + // allows any separator one considers more readable, merely + // giving space as an example - but let's not go wild !) || isoString.startsWith(QLatin1Char(' ')))) { return QDateTime(); } @@ -5214,7 +5268,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format) int offset = 0; // Check end of string for Time Zone definition, either Z for UTC or [+-]HH:mm for Offset - if (isoString.endsWith(QLatin1Char('Z'))) { + if (isoString.endsWith(QLatin1Char('Z'), Qt::CaseInsensitive)) { spec = Qt::UTC; isoString.chop(1); // trim 'Z' } else { @@ -5345,7 +5399,7 @@ QDateTime QDateTime::fromString(const QString &string, Qt::DateFormat format) if (parts.count() == 5) return QDateTime(date, time, Qt::LocalTime); - QStringRef tz = parts.at(5); + QStringView tz = parts.at(5); if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive)) return QDateTime(); tz = tz.mid(3); diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index 2c566e3584..61214b0c7e 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -369,13 +369,6 @@ static QString unquote(const QStringRef &str) } return ret; } -/*! - \internal - - Parses the format \a newFormat. If successful, returns \c true and - sets up the format. Else keeps the old format and returns \c false. - -*/ static inline int countRepeat(const QString &str, int index, int maxCount) { @@ -394,7 +387,12 @@ static inline void appendSeparator(QStringList *list, const QString &string, int list->append(lastQuote >= from ? unquote(separator) : separator.toString()); } +/*! + \internal + Parses the format \a newFormat. If successful, returns \c true and sets up + the format. Else keeps the old format and returns \c false. +*/ bool QDateTimeParser::parseFormat(const QString &newFormat) { const QLatin1Char quote('\''); @@ -1365,7 +1363,7 @@ QDateTimeParser::scanString(const QDateTime &defaultValue, // given date (which might be a spring-forward, skipping an hour). if (parserType == QVariant::DateTime && !(isSet & HourSectionMask) && !when.isValid()) { qint64 msecs = when.toMSecsSinceEpoch(); - // Fortunately, that gets a useful answer ... + // Fortunately, that gets a useful answer, even though when is invalid ... const QDateTime replace = #if QT_CONFIG(timezone) tspec == Qt::TimeZone diff --git a/src/corelib/time/qtimezoneprivate.cpp b/src/corelib/time/qtimezoneprivate.cpp index 00dc8b4ced..cb019fa1a5 100644 --- a/src/corelib/time/qtimezoneprivate.cpp +++ b/src/corelib/time/qtimezoneprivate.cpp @@ -380,18 +380,15 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs, On the first pass, the case we consider is what hint told us to expect (except when hint was -1 and didn't actually tell us what to expect), so it's likely right. We only get a second pass if the first failed, - by which time the second case, that we're trying, is likely right. If - an overwhelming majority of calls have hint == -1, the Q_LIKELY here - shall be wrong half the time; otherwise, its errors shall be rarer - than that. + by which time the second case, that we're trying, is likely right. */ if (nextFirst ? i == 0 : i) { Q_ASSERT(nextStart != invalidMSecs()); - if (Q_LIKELY(nextStart <= nextTran.atMSecsSinceEpoch)) + if (nextStart <= nextTran.atMSecsSinceEpoch) return nextTran; } else { // If next is invalid, nextFirst is false, to route us here first: - if (nextStart == invalidMSecs() || Q_LIKELY(nextStart > tran.atMSecsSinceEpoch)) + if (nextStart == invalidMSecs() || nextStart > tran.atMSecsSinceEpoch) return tran; } } @@ -420,7 +417,7 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs, int early = offsetFromUtc(recent); int late = offsetFromUtc(imminent); - if (Q_LIKELY(early == late)) { // > 99% of the time + if (early == late) { // > 99% of the time utcEpochMSecs = forLocalMSecs - early * 1000; } else { // Close to a DST transition: early > late is near a fall-back, @@ -432,7 +429,7 @@ QTimeZonePrivate::Data QTimeZonePrivate::dataForLocalTime(qint64 forLocalMSecs, const qint64 forStd = forLocalMSecs - offsetInStd * 1000; // Best guess at the answer: const qint64 hinted = hint > 0 ? forDst : forStd; - if (Q_LIKELY(offsetFromUtc(hinted) == (hint > 0 ? offsetInDst : offsetInStd))) { + if (offsetFromUtc(hinted) == (hint > 0 ? offsetInDst : offsetInStd)) { utcEpochMSecs = hinted; } else if (hint <= 0 && offsetFromUtc(forDst) == offsetInDst) { utcEpochMSecs = forDst; diff --git a/src/corelib/time/qtimezoneprivate_android.cpp b/src/corelib/time/qtimezoneprivate_android.cpp index be4f374fdd..5cb8155dcc 100644 --- a/src/corelib/time/qtimezoneprivate_android.cpp +++ b/src/corelib/time/qtimezoneprivate_android.cpp @@ -1,5 +1,6 @@ /**************************************************************************** ** +** Copyright (C) 2019 The Qt Company Ltd. ** Copyright (C) 2014 Drew Parsons <dparsons@emerall.com> ** Contact: https://www.qt.io/licensing/ ** @@ -53,9 +54,10 @@ QT_BEGIN_NAMESPACE QAndroidTimeZonePrivate::QAndroidTimeZonePrivate() : QTimeZonePrivate() { - // start with system time zone - androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod("java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;"); - init("UTC"); + // Keep in sync with systemTimeZoneId(): + androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod( + "java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;"); + m_id = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;").toString().toUtf8(); } // Create a named time zone @@ -76,32 +78,33 @@ QAndroidTimeZonePrivate::~QAndroidTimeZonePrivate() { } - void QAndroidTimeZonePrivate::init(const QByteArray &ianaId) { - QJNIObjectPrivate jo_ianaId = QJNIObjectPrivate::fromString( QString::fromUtf8(ianaId) ); - androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod( "java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;", static_cast<jstring>(jo_ianaId.object()) ); + const QString iana = QString::fromUtf8(ianaId); + androidTimeZone = QJNIObjectPrivate::callStaticObjectMethod( + "java.util.TimeZone", "getTimeZone", "(Ljava/lang/String;)Ljava/util/TimeZone;", + static_cast<jstring>(QJNIObjectPrivate::fromString(iana).object())); + + // The ID or display name of the zone we've got, if it looks like what we asked for: + const auto match = [iana](const QJNIObjectPrivate &jname) -> QByteArray { + const QString name = jname.toString(); + if (iana.compare(name, Qt::CaseInsensitive)) + return name.toUtf8(); + + return QByteArray(); + }; // Painfully, JNI gives us back a default zone object if it doesn't // recognize the name; so check for whether ianaId is a recognized name of // the zone object we got and ignore the zone if not. // Try checking ianaId against getID(), getDisplayName(): - QJNIObjectPrivate jname = androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;"); - bool found = (jname.toString().toUtf8() == ianaId); - for (int style = 1; !found && style-- > 0;) { - for (int dst = 1; !found && dst-- > 0;) { - jname = androidTimeZone.callObjectMethod("getDisplayName", "(ZI;)Ljava/lang/String;", - bool(dst), style); - found = (jname.toString().toUtf8() == ianaId); + m_id = match(androidTimeZone.callObjectMethod("getID", "()Ljava/lang/String;")); + for (int style = 1; m_id.isEmpty() && style-- > 0;) { + for (int dst = 1; m_id.isEmpty() && dst-- > 0;) { + m_id = match(androidTimeZone.callObjectMethod( + "getDisplayName", "(ZI;)Ljava/lang/String;", bool(dst), style)); } } - - if (!found) - m_id.clear(); - else if (ianaId.isEmpty()) - m_id = systemTimeZoneId(); - else - m_id = ianaId; } QAndroidTimeZonePrivate *QAndroidTimeZonePrivate::clone() const @@ -225,11 +228,10 @@ QTimeZonePrivate::Data QAndroidTimeZonePrivate::previousTransition(qint64 before QByteArray QAndroidTimeZonePrivate::systemTimeZoneId() const { - QJNIObjectPrivate androidSystemTimeZone = QJNIObjectPrivate::callStaticObjectMethod("java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;"); - QJNIObjectPrivate systemTZIdAndroid = androidSystemTimeZone.callObjectMethod<jstring>("getID"); - QByteArray systemTZid = systemTZIdAndroid.toString().toUtf8(); - - return systemTZid; + // Keep in sync with default constructor: + QJNIObjectPrivate androidSystemTimeZone = QJNIObjectPrivate::callStaticObjectMethod( + "java.util.TimeZone", "getDefault", "()Ljava/util/TimeZone;"); + return androidSystemTimeZone.callObjectMethod<jstring>("getID").toString().toUtf8(); } QList<QByteArray> QAndroidTimeZonePrivate::availableTimeZoneIds() const diff --git a/src/corelib/time/qtimezoneprivate_mac.mm b/src/corelib/time/qtimezoneprivate_mac.mm index d3c4fbe5da..1fb48a31d3 100644 --- a/src/corelib/time/qtimezoneprivate_mac.mm +++ b/src/corelib/time/qtimezoneprivate_mac.mm @@ -1,5 +1,6 @@ /**************************************************************************** ** +** Copyright (C) 2019 The Qt Company Ltd. ** Copyright (C) 2013 John Layt <jlayt@kde.org> ** Contact: https://www.qt.io/licensing/ ** @@ -59,22 +60,24 @@ QT_BEGIN_NAMESPACE // Create the system default time zone QMacTimeZonePrivate::QMacTimeZonePrivate() - : m_nstz(0) { - init(systemTimeZoneId()); + // Reset the cached system tz then instantiate it: + [NSTimeZone resetSystemTimeZone]; + m_nstz = [NSTimeZone.systemTimeZone retain]; + Q_ASSERT(m_nstz); + m_id = QString::fromNSString(m_nstz.name).toUtf8(); } // Create a named time zone QMacTimeZonePrivate::QMacTimeZonePrivate(const QByteArray &ianaId) - : m_nstz(0) + : m_nstz(nil) { init(ianaId); } QMacTimeZonePrivate::QMacTimeZonePrivate(const QMacTimeZonePrivate &other) - : QTimeZonePrivate(other), m_nstz(0) + : QTimeZonePrivate(other), m_nstz([other.m_nstz copy]) { - m_nstz = [other.m_nstz copy]; } QMacTimeZonePrivate::~QMacTimeZonePrivate() @@ -94,11 +97,21 @@ void QMacTimeZonePrivate::init(const QByteArray &ianaId) if (m_nstz) m_id = ianaId; } + if (!m_nstz) { + // macOS has been seen returning a systemTimeZone which reports its name + // as Asia/Kolkata, which doesn't appear in knownTimeZoneNames (which + // calls the zone Asia/Calcutta). So explicitly check for the name + // systemTimeZoneId() returns, and use systemTimeZone if we get it: + m_nstz = [NSTimeZone.systemTimeZone retain]; + Q_ASSERT(m_nstz); + if (QString::fromNSString(m_nstz.name).toUtf8() == ianaId) + m_id = ianaId; + } } QString QMacTimeZonePrivate::comment() const { - return QString::fromNSString([m_nstz description]); + return QString::fromNSString(m_nstz.description); } QString QMacTimeZonePrivate::displayName(QTimeZone::TimeType timeType, @@ -201,7 +214,7 @@ bool QMacTimeZonePrivate::hasTransitions() const // TODO Not sure what is returned in event of no transitions, assume will be before requested date NSDate *epoch = [NSDate dateWithTimeIntervalSince1970:0]; const NSDate *date = [m_nstz nextDaylightSavingTimeTransitionAfterDate:epoch]; - const bool result = ([date timeIntervalSince1970] > [epoch timeIntervalSince1970]); + const bool result = (date.timeIntervalSince1970 > epoch.timeIntervalSince1970); return result; } @@ -211,7 +224,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::nextTransition(qint64 afterMSecsSinc const NSTimeInterval seconds = afterMSecsSinceEpoch / 1000.0; NSDate *nextDate = [NSDate dateWithTimeIntervalSince1970:seconds]; nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate]; - const NSTimeInterval nextSecs = [nextDate timeIntervalSince1970]; + const NSTimeInterval nextSecs = nextDate.timeIntervalSince1970; if (nextDate == nil || nextSecs <= seconds) { [nextDate release]; return invalidData(); @@ -237,7 +250,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec NSDate *nextDate = [NSDate dateWithTimeIntervalSince1970:nextSecs]; nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate]; if (nextDate != nil - && (tranSecs = [nextDate timeIntervalSince1970]) < endSecs) { + && (tranSecs = nextDate.timeIntervalSince1970) < endSecs) { // There's a transition within the last year before endSecs: nextSecs = tranSecs; } else { @@ -246,7 +259,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate]; if (nextDate != nil) { NSTimeInterval lateSecs = nextSecs; - nextSecs = [nextDate timeIntervalSince1970]; + nextSecs = nextDate.timeIntervalSince1970; Q_ASSERT(nextSecs <= endSecs - year || nextSecs == tranSecs); /* We're looking at the first ever transition for our zone, at @@ -272,8 +285,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec NSTimeInterval middle = nextSecs / 2 + lateSecs / 2; NSDate *split = [NSDate dateWithTimeIntervalSince1970:middle]; split = [m_nstz nextDaylightSavingTimeTransitionAfterDate:split]; - if (split != nil - && (tranSecs = [split timeIntervalSince1970]) < endSecs) { + if (split != nil && (tranSecs = split.timeIntervalSince1970) < endSecs) { nextDate = split; nextSecs = tranSecs; } else { @@ -290,7 +302,7 @@ QTimeZonePrivate::Data QMacTimeZonePrivate::previousTransition(qint64 beforeMSec while (nextDate != nil && nextSecs < endSecs) { prevSecs = nextSecs; nextDate = [m_nstz nextDaylightSavingTimeTransitionAfterDate:nextDate]; - nextSecs = [nextDate timeIntervalSince1970]; + nextSecs = nextDate.timeIntervalSince1970; if (nextSecs <= prevSecs) // presumably no later data available break; } @@ -305,18 +317,19 @@ QByteArray QMacTimeZonePrivate::systemTimeZoneId() const { // Reset the cached system tz then return the name [NSTimeZone resetSystemTimeZone]; - return QString::fromNSString([[NSTimeZone systemTimeZone] name]).toUtf8(); + Q_ASSERT(NSTimeZone.systemTimeZone); + return QString::fromNSString(NSTimeZone.systemTimeZone.name).toUtf8(); } QList<QByteArray> QMacTimeZonePrivate::availableTimeZoneIds() const { - NSEnumerator *enumerator = [[NSTimeZone knownTimeZoneNames] objectEnumerator]; - QByteArray tzid = QString::fromNSString([enumerator nextObject]).toUtf8(); + NSEnumerator *enumerator = NSTimeZone.knownTimeZoneNames.objectEnumerator; + QByteArray tzid = QString::fromNSString(enumerator.nextObject).toUtf8(); QList<QByteArray> list; while (!tzid.isEmpty()) { list << tzid; - tzid = QString::fromNSString([enumerator nextObject]).toUtf8(); + tzid = QString::fromNSString(enumerator.nextObject).toUtf8(); } std::sort(list.begin(), list.end()); |