diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2023-03-24 13:28:28 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2023-04-14 15:19:01 +0200 |
commit | d30d566f8df8715de0c73fd5864143b4fda73f34 (patch) | |
tree | 9ef466e69d85c4f311d498ece2272262ab960020 /src/corelib/time/qtimezoneprivate_tz.cpp | |
parent | c3f00ce3e34d5cfa3ee4227a3a453448ab2f8b31 (diff) |
Minor tidies in the TZ time-zone backend
Taking the size() of a list before append()ing to it saves the need to
subtract 1. Add some missing assertions.
We can now use sliced(), rather than mid(), to trim a string or byte
array. We can do such trimming on a view, rather than allocating for
the trimmed part.
Change-Id: I8eff46ea6545a66c765c2d79aa1162c3044ca1cf
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Mate Barany <mate.barany@qt.io>
Diffstat (limited to 'src/corelib/time/qtimezoneprivate_tz.cpp')
-rw-r--r-- | src/corelib/time/qtimezoneprivate_tz.cpp | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/corelib/time/qtimezoneprivate_tz.cpp b/src/corelib/time/qtimezoneprivate_tz.cpp index 95b5894c3f..2576fe7972 100644 --- a/src/corelib/time/qtimezoneprivate_tz.cpp +++ b/src/corelib/time/qtimezoneprivate_tz.cpp @@ -352,13 +352,15 @@ static QDate calculateDowDate(int year, int month, int dayOfWeek, int week) static QDate calculatePosixDate(const QByteArray &dateRule, int year) { + Q_ASSERT(!dateRule.isEmpty()); bool ok; // Can start with M, J, or a digit if (dateRule.at(0) == 'M') { // nth week in month format "Mmonth.week.dow" QList<QByteArray> dateParts = dateRule.split('.'); if (dateParts.size() > 2) { - int month = dateParts.at(0).mid(1).toInt(&ok); + Q_ASSERT(!dateParts.at(0).isEmpty()); // the 'M' is its [0]. + int month = QByteArrayView{ dateParts.at(0) }.sliced(1).toInt(&ok); int week = ok ? dateParts.at(1).toInt(&ok) : 0; int dow = ok ? dateParts.at(2).toInt(&ok) : 0; if (ok) @@ -367,7 +369,7 @@ static QDate calculatePosixDate(const QByteArray &dateRule, int year) } else if (dateRule.at(0) == 'J') { // Day of Year 1...365, ignores Feb 29. // So March always starts on day 60. - int doy = dateRule.mid(1).toInt(&ok); + int doy = QByteArrayView{ dateRule }.sliced(1).toInt(&ok); if (ok && doy > 0 && doy < 366) { // Subtract 1 because we're adding days *after* the first of // January, unless it's after February in a leap year, when the leap @@ -905,8 +907,8 @@ QTzTimeZoneCacheEntry QTzTimeZoneCache::findEntry(const QByteArray &ianaId) if (ruleIndex == -1) { if (rule.dstOffset != 0) ret.m_hasDst = true; + tran.ruleIndex = ret.m_tranRules.size(); ret.m_tranRules.append(rule); - tran.ruleIndex = ret.m_tranRules.size() - 1; } else { tran.ruleIndex = ruleIndex; } @@ -1308,7 +1310,7 @@ private: path = QFile::symLinkTarget(path); int index = path.indexOf(zoneinfo); if (index >= 0) // Found zoneinfo file; extract zone name from path: - return QStringView{ path }.mid(index + zoneinfo.size()).toUtf8(); + return QStringView{ path }.sliced(index + zoneinfo.size()).toUtf8(); } while (!path.isEmpty() && --iteration > 0); return QByteArray(); @@ -1365,7 +1367,7 @@ QByteArray QTzTimeZonePrivate::staticSystemTimeZoneId() if (ianaId == ":/etc/localtime") ianaId.clear(); else if (ianaId.startsWith(':')) - ianaId = ianaId.mid(1); + ianaId = ianaId.sliced(1); if (ianaId.isEmpty()) { Q_CONSTINIT thread_local static ZoneNameReader reader; |