| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qtimezoneprivate_icu.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2013 John Layt <jlayt@kde.org> | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include "qtimezone.h" | - | ||||||||||||
| 35 | #include "qtimezoneprivate_p.h" | - | ||||||||||||
| 36 | - | |||||||||||||
| 37 | #include <unicode/ucal.h> | - | ||||||||||||
| 38 | - | |||||||||||||
| 39 | #include <qdebug.h> | - | ||||||||||||
| 40 | #include <qlist.h> | - | ||||||||||||
| 41 | - | |||||||||||||
| 42 | #include <algorithm> | - | ||||||||||||
| 43 | - | |||||||||||||
| 44 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | /* | - | ||||||||||||
| 47 | Private | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | ICU implementation | - | ||||||||||||
| 50 | */ | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | // ICU utilities | - | ||||||||||||
| 53 | - | |||||||||||||
| 54 | // Convert TimeType and NameType into ICU UCalendarDisplayNameType | - | ||||||||||||
| 55 | static UCalendarDisplayNameType ucalDisplayNameType(QTimeZone::TimeType timeType, QTimeZone::NameType nameType) | - | ||||||||||||
| 56 | { | - | ||||||||||||
| 57 | // TODO ICU C UCalendarDisplayNameType does not support full set of C++ TimeZone::EDisplayType | - | ||||||||||||
| 58 | switch (nameType) { | - | ||||||||||||
| 59 | case QTimeZone::ShortName : executed 4 times by 1 test: case QTimeZone::ShortName :Executed by:
| 4 | ||||||||||||
| 60 | case QTimeZone::OffsetName : never executed: case QTimeZone::OffsetName : | 0 | ||||||||||||
| 61 | if (timeType == QTimeZone::DaylightTime)
| 2 | ||||||||||||
| 62 | return UCAL_SHORT_DST; executed 2 times by 1 test: return UCAL_SHORT_DST;Executed by:
| 2 | ||||||||||||
| 63 | // Includes GenericTime | - | ||||||||||||
| 64 | return UCAL_SHORT_STANDARD; executed 2 times by 1 test: return UCAL_SHORT_STANDARD;Executed by:
| 2 | ||||||||||||
| 65 | case QTimeZone::DefaultName : executed 1233 times by 1 test: case QTimeZone::DefaultName :Executed by:
| 1233 | ||||||||||||
| 66 | case QTimeZone::LongName : never executed: case QTimeZone::LongName : | 0 | ||||||||||||
| 67 | if (timeType == QTimeZone::DaylightTime)
| 432-801 | ||||||||||||
| 68 | return UCAL_DST; executed 432 times by 1 test: return UCAL_DST;Executed by:
| 432 | ||||||||||||
| 69 | // Includes GenericTime | - | ||||||||||||
| 70 | return UCAL_STANDARD; executed 801 times by 1 test: return UCAL_STANDARD;Executed by:
| 801 | ||||||||||||
| 71 | } | - | ||||||||||||
| 72 | return UCAL_STANDARD; never executed: return UCAL_STANDARD; | 0 | ||||||||||||
| 73 | } | - | ||||||||||||
| 74 | - | |||||||||||||
| 75 | // Qt wrapper around ucal_getDefaultTimeZone() | - | ||||||||||||
| 76 | static QByteArray ucalDefaultTimeZoneId() | - | ||||||||||||
| 77 | { | - | ||||||||||||
| 78 | int32_t size = 30; | - | ||||||||||||
| 79 | QString result(size, Qt::Uninitialized); | - | ||||||||||||
| 80 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 81 | - | |||||||||||||
| 82 | // size = ucal_getDefaultTimeZone(result, resultLength, status) | - | ||||||||||||
| 83 | size = ucal_getDefaultTimeZone(reinterpret_cast<UChar *>(result.data()), size, &status); | - | ||||||||||||
| 84 | - | |||||||||||||
| 85 | // If overflow, then resize and retry | - | ||||||||||||
| 86 | if (status == U_BUFFER_OVERFLOW_ERROR) {
| 0-1 | ||||||||||||
| 87 | result.resize(size); | - | ||||||||||||
| 88 | status = U_ZERO_ERROR; | - | ||||||||||||
| 89 | size = ucal_getDefaultTimeZone(reinterpret_cast<UChar *>(result.data()), size, &status); | - | ||||||||||||
| 90 | } never executed: end of block | 0 | ||||||||||||
| 91 | - | |||||||||||||
| 92 | // If successful on first or second go, resize and return | - | ||||||||||||
| 93 | if (U_SUCCESS(status)) {
| 0-1 | ||||||||||||
| 94 | result.resize(size); | - | ||||||||||||
| 95 | return result.toUtf8(); executed 1 time by 1 test: return result.toUtf8();Executed by:
| 1 | ||||||||||||
| 96 | } | - | ||||||||||||
| 97 | - | |||||||||||||
| 98 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||
| 99 | } | - | ||||||||||||
| 100 | - | |||||||||||||
| 101 | // Qt wrapper around ucal_getTimeZoneDisplayName() | - | ||||||||||||
| 102 | static QString ucalTimeZoneDisplayName(UCalendar *ucal, QTimeZone::TimeType timeType, | - | ||||||||||||
| 103 | QTimeZone::NameType nameType, | - | ||||||||||||
| 104 | const QString &localeCode) | - | ||||||||||||
| 105 | { | - | ||||||||||||
| 106 | int32_t size = 50; | - | ||||||||||||
| 107 | QString result(size, Qt::Uninitialized); | - | ||||||||||||
| 108 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 109 | - | |||||||||||||
| 110 | // size = ucal_getTimeZoneDisplayName(cal, type, locale, result, resultLength, status) | - | ||||||||||||
| 111 | size = ucal_getTimeZoneDisplayName(ucal, | - | ||||||||||||
| 112 | ucalDisplayNameType(timeType, nameType), | - | ||||||||||||
| 113 | localeCode.toUtf8(), | - | ||||||||||||
| 114 | reinterpret_cast<UChar *>(result.data()), | - | ||||||||||||
| 115 | size, | - | ||||||||||||
| 116 | &status); | - | ||||||||||||
| 117 | - | |||||||||||||
| 118 | // If overflow, then resize and retry | - | ||||||||||||
| 119 | if (status == U_BUFFER_OVERFLOW_ERROR) {
| 0-1237 | ||||||||||||
| 120 | result.resize(size); | - | ||||||||||||
| 121 | status = U_ZERO_ERROR; | - | ||||||||||||
| 122 | size = ucal_getTimeZoneDisplayName(ucal, | - | ||||||||||||
| 123 | ucalDisplayNameType(timeType, nameType), | - | ||||||||||||
| 124 | localeCode.toUtf8(), | - | ||||||||||||
| 125 | reinterpret_cast<UChar *>(result.data()), | - | ||||||||||||
| 126 | size, | - | ||||||||||||
| 127 | &status); | - | ||||||||||||
| 128 | } never executed: end of block | 0 | ||||||||||||
| 129 | - | |||||||||||||
| 130 | // If successful on first or second go, resize and return | - | ||||||||||||
| 131 | if (U_SUCCESS(status)) {
| 0-1237 | ||||||||||||
| 132 | result.resize(size); | - | ||||||||||||
| 133 | return result; executed 1237 times by 1 test: return result;Executed by:
| 1237 | ||||||||||||
| 134 | } | - | ||||||||||||
| 135 | - | |||||||||||||
| 136 | return QString(); never executed: return QString(); | 0 | ||||||||||||
| 137 | } | - | ||||||||||||
| 138 | - | |||||||||||||
| 139 | // Qt wrapper around ucal_get() for offsets | - | ||||||||||||
| 140 | static bool ucalOffsetsAtTime(UCalendar *m_ucal, qint64 atMSecsSinceEpoch, | - | ||||||||||||
| 141 | int *utcOffset, int *dstOffset) | - | ||||||||||||
| 142 | { | - | ||||||||||||
| 143 | *utcOffset = 0; | - | ||||||||||||
| 144 | *dstOffset = 0; | - | ||||||||||||
| 145 | - | |||||||||||||
| 146 | // Clone the ucal so we don't change the shared object | - | ||||||||||||
| 147 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 148 | UCalendar *ucal = ucal_clone(m_ucal, &status); | - | ||||||||||||
| 149 | if (!U_SUCCESS(status))
| 0-8 | ||||||||||||
| 150 | return false; never executed: return false; | 0 | ||||||||||||
| 151 | - | |||||||||||||
| 152 | // Set the date to find the offset for | - | ||||||||||||
| 153 | status = U_ZERO_ERROR; | - | ||||||||||||
| 154 | ucal_setMillis(ucal, atMSecsSinceEpoch, &status); | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | int32_t utc = 0; | - | ||||||||||||
| 157 | if (U_SUCCESS(status)) {
| 0-8 | ||||||||||||
| 158 | status = U_ZERO_ERROR; | - | ||||||||||||
| 159 | // Returns msecs | - | ||||||||||||
| 160 | utc = ucal_get(ucal, UCAL_ZONE_OFFSET, &status) / 1000; | - | ||||||||||||
| 161 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||
| 162 | - | |||||||||||||
| 163 | int32_t dst = 0; | - | ||||||||||||
| 164 | if (U_SUCCESS(status)) {
| 0-8 | ||||||||||||
| 165 | status = U_ZERO_ERROR; | - | ||||||||||||
| 166 | // Returns msecs | - | ||||||||||||
| 167 | dst = ucal_get(ucal, UCAL_DST_OFFSET, &status) / 1000; | - | ||||||||||||
| 168 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||
| 169 | - | |||||||||||||
| 170 | ucal_close(ucal); | - | ||||||||||||
| 171 | if (U_SUCCESS(status)) {
| 0-8 | ||||||||||||
| 172 | *utcOffset = utc; | - | ||||||||||||
| 173 | *dstOffset = dst; | - | ||||||||||||
| 174 | return true; executed 8 times by 1 test: return true;Executed by:
| 8 | ||||||||||||
| 175 | } | - | ||||||||||||
| 176 | return false; never executed: return false; | 0 | ||||||||||||
| 177 | } | - | ||||||||||||
| 178 | - | |||||||||||||
| 179 | // ICU Draft api in v50, should be stable in ICU v51. Available in C++ api from ICU v3.8 | - | ||||||||||||
| 180 | #if U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 181 | // Qt wrapper around qt_ucal_getTimeZoneTransitionDate & ucal_get | - | ||||||||||||
| 182 | static QTimeZonePrivate::Data ucalTimeZoneTransition(UCalendar *m_ucal, | - | ||||||||||||
| 183 | UTimeZoneTransitionType type, | - | ||||||||||||
| 184 | qint64 atMSecsSinceEpoch) | - | ||||||||||||
| 185 | { | - | ||||||||||||
| 186 | QTimeZonePrivate::Data tran = QTimeZonePrivate::invalidData(); | - | ||||||||||||
| 187 | - | |||||||||||||
| 188 | // Clone the ucal so we don't change the shared object | - | ||||||||||||
| 189 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 190 | UCalendar *ucal = ucal_clone(m_ucal, &status); | - | ||||||||||||
| 191 | if (!U_SUCCESS(status)) | - | ||||||||||||
| 192 | return tran; | - | ||||||||||||
| 193 | - | |||||||||||||
| 194 | // Set the date to find the transition for | - | ||||||||||||
| 195 | status = U_ZERO_ERROR; | - | ||||||||||||
| 196 | ucal_setMillis(ucal, atMSecsSinceEpoch, &status); | - | ||||||||||||
| 197 | - | |||||||||||||
| 198 | // Find the transition time | - | ||||||||||||
| 199 | UDate tranMSecs = 0; | - | ||||||||||||
| 200 | status = U_ZERO_ERROR; | - | ||||||||||||
| 201 | bool ok = ucal_getTimeZoneTransitionDate(ucal, type, &tranMSecs, &status); | - | ||||||||||||
| 202 | - | |||||||||||||
| 203 | // Set the transition time to find the offsets for | - | ||||||||||||
| 204 | if (U_SUCCESS(status) && ok) { | - | ||||||||||||
| 205 | status = U_ZERO_ERROR; | - | ||||||||||||
| 206 | ucal_setMillis(ucal, tranMSecs, &status); | - | ||||||||||||
| 207 | } | - | ||||||||||||
| 208 | - | |||||||||||||
| 209 | int32_t utc = 0; | - | ||||||||||||
| 210 | if (U_SUCCESS(status) && ok) { | - | ||||||||||||
| 211 | status = U_ZERO_ERROR; | - | ||||||||||||
| 212 | utc = ucal_get(ucal, UCAL_ZONE_OFFSET, &status) / 1000; | - | ||||||||||||
| 213 | } | - | ||||||||||||
| 214 | - | |||||||||||||
| 215 | int32_t dst = 0; | - | ||||||||||||
| 216 | if (U_SUCCESS(status) && ok) { | - | ||||||||||||
| 217 | status = U_ZERO_ERROR; | - | ||||||||||||
| 218 | dst = ucal_get(ucal, UCAL_DST_OFFSET, &status) / 1000; | - | ||||||||||||
| 219 | } | - | ||||||||||||
| 220 | - | |||||||||||||
| 221 | ucal_close(ucal); | - | ||||||||||||
| 222 | if (!U_SUCCESS(status) || !ok) | - | ||||||||||||
| 223 | return tran; | - | ||||||||||||
| 224 | tran.atMSecsSinceEpoch = tranMSecs; | - | ||||||||||||
| 225 | tran.offsetFromUtc = utc + dst; | - | ||||||||||||
| 226 | tran.standardTimeOffset = utc; | - | ||||||||||||
| 227 | tran.daylightTimeOffset = dst; | - | ||||||||||||
| 228 | // TODO No ICU API, use short name instead | - | ||||||||||||
| 229 | if (dst == 0) | - | ||||||||||||
| 230 | tran.abbreviation = ucalTimeZoneDisplayName(m_ucal, QTimeZone::StandardTime, | - | ||||||||||||
| 231 | QTimeZone::ShortName, QLocale().name()); | - | ||||||||||||
| 232 | else | - | ||||||||||||
| 233 | tran.abbreviation = ucalTimeZoneDisplayName(m_ucal, QTimeZone::DaylightTime, | - | ||||||||||||
| 234 | QTimeZone::ShortName, QLocale().name()); | - | ||||||||||||
| 235 | return tran; | - | ||||||||||||
| 236 | } | - | ||||||||||||
| 237 | #endif // U_ICU_VERSION_SHORT | - | ||||||||||||
| 238 | - | |||||||||||||
| 239 | // Convert a uenum to a QList<QByteArray> | - | ||||||||||||
| 240 | static QList<QByteArray> uenumToIdList(UEnumeration *uenum) | - | ||||||||||||
| 241 | { | - | ||||||||||||
| 242 | QList<QByteArray> list; | - | ||||||||||||
| 243 | int32_t size = 0; | - | ||||||||||||
| 244 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 245 | // TODO Perhaps use uenum_unext instead? | - | ||||||||||||
| 246 | QByteArray result = uenum_next(uenum, &size, &status); | - | ||||||||||||
| 247 | while (U_SUCCESS(status) && !result.isEmpty()) {
| 0-263952 | ||||||||||||
| 248 | list << result; | - | ||||||||||||
| 249 | status = U_ZERO_ERROR; | - | ||||||||||||
| 250 | result = uenum_next(uenum, &size, &status); | - | ||||||||||||
| 251 | } executed 263529 times by 1 test: end of blockExecuted by:
| 263529 | ||||||||||||
| 252 | std::sort(list.begin(), list.end()); | - | ||||||||||||
| 253 | list.erase(std::unique(list.begin(), list.end()), list.end()); | - | ||||||||||||
| 254 | return list; executed 423 times by 1 test: return list;Executed by:
| 423 | ||||||||||||
| 255 | } | - | ||||||||||||
| 256 | - | |||||||||||||
| 257 | // Qt wrapper around ucal_getDSTSavings() | - | ||||||||||||
| 258 | static int ucalDaylightOffset(const QByteArray &id) | - | ||||||||||||
| 259 | { | - | ||||||||||||
| 260 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 261 | const int32_t dstMSecs = ucal_getDSTSavings(reinterpret_cast<const UChar *>(id.data()), &status); | - | ||||||||||||
| 262 | if (U_SUCCESS(status))
| 0-1 | ||||||||||||
| 263 | return (dstMSecs / 1000); executed 1 time by 1 test: return (dstMSecs / 1000);Executed by:
| 1 | ||||||||||||
| 264 | else | - | ||||||||||||
| 265 | return 0; never executed: return 0; | 0 | ||||||||||||
| 266 | } | - | ||||||||||||
| 267 | - | |||||||||||||
| 268 | // Create the system default time zone | - | ||||||||||||
| 269 | QIcuTimeZonePrivate::QIcuTimeZonePrivate() | - | ||||||||||||
| 270 | : m_ucal(0) | - | ||||||||||||
| 271 | { | - | ||||||||||||
| 272 | // TODO No ICU C API to obtain sysem tz, assume default hasn't been changed | - | ||||||||||||
| 273 | init(ucalDefaultTimeZoneId()); | - | ||||||||||||
| 274 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 275 | - | |||||||||||||
| 276 | // Create a named time zone | - | ||||||||||||
| 277 | QIcuTimeZonePrivate::QIcuTimeZonePrivate(const QByteArray &ianaId) | - | ||||||||||||
| 278 | : m_ucal(0) | - | ||||||||||||
| 279 | { | - | ||||||||||||
| 280 | // Need to check validity here as ICu will create a GMT tz if name is invalid | - | ||||||||||||
| 281 | if (availableTimeZoneIds().contains(ianaId))
| 11-412 | ||||||||||||
| 282 | init(ianaId); executed 412 times by 1 test: init(ianaId);Executed by:
| 412 | ||||||||||||
| 283 | } executed 423 times by 1 test: end of blockExecuted by:
| 423 | ||||||||||||
| 284 | - | |||||||||||||
| 285 | QIcuTimeZonePrivate::QIcuTimeZonePrivate(const QIcuTimeZonePrivate &other) | - | ||||||||||||
| 286 | : QTimeZonePrivate(other), m_ucal(0) | - | ||||||||||||
| 287 | { | - | ||||||||||||
| 288 | // Clone the ucal so we don't close the shared object | - | ||||||||||||
| 289 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 290 | m_ucal = ucal_clone(other.m_ucal, &status); | - | ||||||||||||
| 291 | if (!U_SUCCESS(status)) {
| 0 | ||||||||||||
| 292 | m_id.clear(); | - | ||||||||||||
| 293 | m_ucal = 0; | - | ||||||||||||
| 294 | } never executed: end of block | 0 | ||||||||||||
| 295 | } never executed: end of block | 0 | ||||||||||||
| 296 | - | |||||||||||||
| 297 | QIcuTimeZonePrivate::~QIcuTimeZonePrivate() | - | ||||||||||||
| 298 | { | - | ||||||||||||
| 299 | ucal_close(m_ucal); | - | ||||||||||||
| 300 | } executed 424 times by 1 test: end of blockExecuted by:
| 424 | ||||||||||||
| 301 | - | |||||||||||||
| 302 | QTimeZonePrivate *QIcuTimeZonePrivate::clone() | - | ||||||||||||
| 303 | { | - | ||||||||||||
| 304 | return new QIcuTimeZonePrivate(*this); never executed: return new QIcuTimeZonePrivate(*this); | 0 | ||||||||||||
| 305 | } | - | ||||||||||||
| 306 | - | |||||||||||||
| 307 | void QIcuTimeZonePrivate::init(const QByteArray &ianaId) | - | ||||||||||||
| 308 | { | - | ||||||||||||
| 309 | m_id = ianaId; | - | ||||||||||||
| 310 | - | |||||||||||||
| 311 | const QString id = QString::fromUtf8(m_id); | - | ||||||||||||
| 312 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 313 | //TODO Use UCAL_GREGORIAN for now to match QLocale, change to UCAL_DEFAULT once full ICU support | - | ||||||||||||
| 314 | m_ucal = ucal_open(reinterpret_cast<const UChar *>(id.data()), id.size(), | - | ||||||||||||
| 315 | QLocale().name().toUtf8(), UCAL_GREGORIAN, &status); | - | ||||||||||||
| 316 | - | |||||||||||||
| 317 | if (!U_SUCCESS(status)) {
| 0-413 | ||||||||||||
| 318 | m_id.clear(); | - | ||||||||||||
| 319 | m_ucal = 0; | - | ||||||||||||
| 320 | } never executed: end of block | 0 | ||||||||||||
| 321 | } executed 413 times by 1 test: end of blockExecuted by:
| 413 | ||||||||||||
| 322 | - | |||||||||||||
| 323 | QString QIcuTimeZonePrivate::displayName(QTimeZone::TimeType timeType, | - | ||||||||||||
| 324 | QTimeZone::NameType nameType, | - | ||||||||||||
| 325 | const QLocale &locale) const | - | ||||||||||||
| 326 | { | - | ||||||||||||
| 327 | // Return standard offset format name as ICU C api doesn't support it yet | - | ||||||||||||
| 328 | if (nameType == QTimeZone::OffsetName) {
| 0-1237 | ||||||||||||
| 329 | const Data nowData = data(QDateTime::currentDateTimeUtc().toMSecsSinceEpoch()); | - | ||||||||||||
| 330 | // We can't use transitions reliably to find out right dst offset | - | ||||||||||||
| 331 | // Instead use dst offset api to try get it if needed | - | ||||||||||||
| 332 | if (timeType == QTimeZone::DaylightTime)
| 0 | ||||||||||||
| 333 | return isoOffsetFormat(nowData.standardTimeOffset + ucalDaylightOffset(m_id)); never executed: return isoOffsetFormat(nowData.standardTimeOffset + ucalDaylightOffset(m_id)); | 0 | ||||||||||||
| 334 | else | - | ||||||||||||
| 335 | return isoOffsetFormat(nowData.standardTimeOffset); never executed: return isoOffsetFormat(nowData.standardTimeOffset); | 0 | ||||||||||||
| 336 | } | - | ||||||||||||
| 337 | return ucalTimeZoneDisplayName(m_ucal, timeType, nameType, locale.name()); executed 1237 times by 1 test: return ucalTimeZoneDisplayName(m_ucal, timeType, nameType, locale.name());Executed by:
| 1237 | ||||||||||||
| 338 | } | - | ||||||||||||
| 339 | - | |||||||||||||
| 340 | QString QIcuTimeZonePrivate::abbreviation(qint64 atMSecsSinceEpoch) const | - | ||||||||||||
| 341 | { | - | ||||||||||||
| 342 | // TODO No ICU API, use short name instead | - | ||||||||||||
| 343 | if (isDaylightTime(atMSecsSinceEpoch))
| 2 | ||||||||||||
| 344 | return displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, QString()); executed 2 times by 1 test: return displayName(QTimeZone::DaylightTime, QTimeZone::ShortName, QString());Executed by:
| 2 | ||||||||||||
| 345 | else | - | ||||||||||||
| 346 | return displayName(QTimeZone::StandardTime, QTimeZone::ShortName, QString()); executed 2 times by 1 test: return displayName(QTimeZone::StandardTime, QTimeZone::ShortName, QString());Executed by:
| 2 | ||||||||||||
| 347 | } | - | ||||||||||||
| 348 | - | |||||||||||||
| 349 | int QIcuTimeZonePrivate::offsetFromUtc(qint64 atMSecsSinceEpoch) const | - | ||||||||||||
| 350 | { | - | ||||||||||||
| 351 | int stdOffset = 0; | - | ||||||||||||
| 352 | int dstOffset = 0; | - | ||||||||||||
| 353 | ucalOffsetsAtTime(m_ucal, atMSecsSinceEpoch, &stdOffset, & dstOffset); | - | ||||||||||||
| 354 | return stdOffset + dstOffset; executed 2 times by 1 test: return stdOffset + dstOffset;Executed by:
| 2 | ||||||||||||
| 355 | } | - | ||||||||||||
| 356 | - | |||||||||||||
| 357 | int QIcuTimeZonePrivate::standardTimeOffset(qint64 atMSecsSinceEpoch) const | - | ||||||||||||
| 358 | { | - | ||||||||||||
| 359 | int stdOffset = 0; | - | ||||||||||||
| 360 | int dstOffset = 0; | - | ||||||||||||
| 361 | ucalOffsetsAtTime(m_ucal, atMSecsSinceEpoch, &stdOffset, & dstOffset); | - | ||||||||||||
| 362 | return stdOffset; executed 2 times by 1 test: return stdOffset;Executed by:
| 2 | ||||||||||||
| 363 | } | - | ||||||||||||
| 364 | - | |||||||||||||
| 365 | int QIcuTimeZonePrivate::daylightTimeOffset(qint64 atMSecsSinceEpoch) const | - | ||||||||||||
| 366 | { | - | ||||||||||||
| 367 | int stdOffset = 0; | - | ||||||||||||
| 368 | int dstOffset = 0; | - | ||||||||||||
| 369 | ucalOffsetsAtTime(m_ucal, atMSecsSinceEpoch, &stdOffset, & dstOffset); | - | ||||||||||||
| 370 | return dstOffset; executed 2 times by 1 test: return dstOffset;Executed by:
| 2 | ||||||||||||
| 371 | } | - | ||||||||||||
| 372 | - | |||||||||||||
| 373 | bool QIcuTimeZonePrivate::hasDaylightTime() const | - | ||||||||||||
| 374 | { | - | ||||||||||||
| 375 | // TODO No direct ICU C api, work-around below not reliable? Find a better way? | - | ||||||||||||
| 376 | return (ucalDaylightOffset(m_id) != 0); executed 1 time by 1 test: return (ucalDaylightOffset(m_id) != 0);Executed by:
| 1 | ||||||||||||
| 377 | } | - | ||||||||||||
| 378 | - | |||||||||||||
| 379 | bool QIcuTimeZonePrivate::isDaylightTime(qint64 atMSecsSinceEpoch) const | - | ||||||||||||
| 380 | { | - | ||||||||||||
| 381 | // Clone the ucal so we don't change the shared object | - | ||||||||||||
| 382 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 383 | UCalendar *ucal = ucal_clone(m_ucal, &status); | - | ||||||||||||
| 384 | if (!U_SUCCESS(status))
| 0-417 | ||||||||||||
| 385 | return false; never executed: return false; | 0 | ||||||||||||
| 386 | - | |||||||||||||
| 387 | // Set the date to find the offset for | - | ||||||||||||
| 388 | status = U_ZERO_ERROR; | - | ||||||||||||
| 389 | ucal_setMillis(ucal, atMSecsSinceEpoch, &status); | - | ||||||||||||
| 390 | - | |||||||||||||
| 391 | bool result = false; | - | ||||||||||||
| 392 | if (U_SUCCESS(status)) {
| 0-417 | ||||||||||||
| 393 | status = U_ZERO_ERROR; | - | ||||||||||||
| 394 | result = ucal_inDaylightTime(ucal, &status); | - | ||||||||||||
| 395 | } executed 417 times by 1 test: end of blockExecuted by:
| 417 | ||||||||||||
| 396 | - | |||||||||||||
| 397 | ucal_close(ucal); | - | ||||||||||||
| 398 | return result; executed 417 times by 1 test: return result;Executed by:
| 417 | ||||||||||||
| 399 | } | - | ||||||||||||
| 400 | - | |||||||||||||
| 401 | QTimeZonePrivate::Data QIcuTimeZonePrivate::data(qint64 forMSecsSinceEpoch) const | - | ||||||||||||
| 402 | { | - | ||||||||||||
| 403 | // Available in ICU C++ api, and draft C api in v50 | - | ||||||||||||
| 404 | // TODO When v51 released see if api is stable | - | ||||||||||||
| 405 | QTimeZonePrivate::Data data = invalidData(); | - | ||||||||||||
| 406 | #if U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 407 | data = ucalTimeZoneTransition(m_ucal, UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE, | - | ||||||||||||
| 408 | forMSecsSinceEpoch); | - | ||||||||||||
| 409 | #else | - | ||||||||||||
| 410 | ucalOffsetsAtTime(m_ucal, forMSecsSinceEpoch, &data.standardTimeOffset, | - | ||||||||||||
| 411 | &data.daylightTimeOffset); | - | ||||||||||||
| 412 | data.offsetFromUtc = data.standardTimeOffset + data.daylightTimeOffset; | - | ||||||||||||
| 413 | data.abbreviation = abbreviation(forMSecsSinceEpoch); | - | ||||||||||||
| 414 | #endif // U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 415 | data.atMSecsSinceEpoch = forMSecsSinceEpoch; | - | ||||||||||||
| 416 | return data; executed 2 times by 1 test: return data;Executed by:
| 2 | ||||||||||||
| 417 | } | - | ||||||||||||
| 418 | - | |||||||||||||
| 419 | bool QIcuTimeZonePrivate::hasTransitions() const | - | ||||||||||||
| 420 | { | - | ||||||||||||
| 421 | // Available in ICU C++ api, and draft C api in v50 | - | ||||||||||||
| 422 | // TODO When v51 released see if api is stable | - | ||||||||||||
| 423 | #if U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 424 | return true; | - | ||||||||||||
| 425 | #else | - | ||||||||||||
| 426 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||
| 427 | #endif // U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 428 | } | - | ||||||||||||
| 429 | - | |||||||||||||
| 430 | QTimeZonePrivate::Data QIcuTimeZonePrivate::nextTransition(qint64 afterMSecsSinceEpoch) const | - | ||||||||||||
| 431 | { | - | ||||||||||||
| 432 | // Available in ICU C++ api, and draft C api in v50 | - | ||||||||||||
| 433 | // TODO When v51 released see if api is stable | - | ||||||||||||
| 434 | #if U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 435 | return ucalTimeZoneTransition(m_ucal, UCAL_TZ_TRANSITION_NEXT, afterMSecsSinceEpoch); | - | ||||||||||||
| 436 | #else | - | ||||||||||||
| 437 | Q_UNUSED(afterMSecsSinceEpoch) | - | ||||||||||||
| 438 | return invalidData(); never executed: return invalidData(); | 0 | ||||||||||||
| 439 | #endif // U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 440 | } | - | ||||||||||||
| 441 | - | |||||||||||||
| 442 | QTimeZonePrivate::Data QIcuTimeZonePrivate::previousTransition(qint64 beforeMSecsSinceEpoch) const | - | ||||||||||||
| 443 | { | - | ||||||||||||
| 444 | // Available in ICU C++ api, and draft C api in v50 | - | ||||||||||||
| 445 | // TODO When v51 released see if api is stable | - | ||||||||||||
| 446 | #if U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 447 | return ucalTimeZoneTransition(m_ucal, UCAL_TZ_TRANSITION_PREVIOUS, beforeMSecsSinceEpoch); | - | ||||||||||||
| 448 | #else | - | ||||||||||||
| 449 | Q_UNUSED(beforeMSecsSinceEpoch) | - | ||||||||||||
| 450 | return invalidData(); never executed: return invalidData(); | 0 | ||||||||||||
| 451 | #endif // U_ICU_VERSION_MAJOR_NUM == 50 | - | ||||||||||||
| 452 | } | - | ||||||||||||
| 453 | - | |||||||||||||
| 454 | QByteArray QIcuTimeZonePrivate::systemTimeZoneId() const | - | ||||||||||||
| 455 | { | - | ||||||||||||
| 456 | // No ICU C API to obtain sysem tz | - | ||||||||||||
| 457 | // TODO Assume default hasn't been changed and is the latests system | - | ||||||||||||
| 458 | return ucalDefaultTimeZoneId(); never executed: return ucalDefaultTimeZoneId(); | 0 | ||||||||||||
| 459 | } | - | ||||||||||||
| 460 | - | |||||||||||||
| 461 | QList<QByteArray> QIcuTimeZonePrivate::availableTimeZoneIds() const | - | ||||||||||||
| 462 | { | - | ||||||||||||
| 463 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 464 | UEnumeration *uenum = ucal_openTimeZones(&status); | - | ||||||||||||
| 465 | QList<QByteArray> result; | - | ||||||||||||
| 466 | if (U_SUCCESS(status))
| 0-423 | ||||||||||||
| 467 | result = uenumToIdList(uenum); executed 423 times by 1 test: result = uenumToIdList(uenum);Executed by:
| 423 | ||||||||||||
| 468 | uenum_close(uenum); | - | ||||||||||||
| 469 | return result; executed 423 times by 1 test: return result;Executed by:
| 423 | ||||||||||||
| 470 | } | - | ||||||||||||
| 471 | - | |||||||||||||
| 472 | QList<QByteArray> QIcuTimeZonePrivate::availableTimeZoneIds(QLocale::Country country) const | - | ||||||||||||
| 473 | { | - | ||||||||||||
| 474 | QByteArray regionCode = QLocalePrivate::countryToCode(country).toUtf8(); | - | ||||||||||||
| 475 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 476 | UEnumeration *uenum = ucal_openCountryTimeZones(regionCode, &status); | - | ||||||||||||
| 477 | QList<QByteArray> result; | - | ||||||||||||
| 478 | if (U_SUCCESS(status))
| 0 | ||||||||||||
| 479 | result = uenumToIdList(uenum); never executed: result = uenumToIdList(uenum); | 0 | ||||||||||||
| 480 | uenum_close(uenum); | - | ||||||||||||
| 481 | return result; never executed: return result; | 0 | ||||||||||||
| 482 | } | - | ||||||||||||
| 483 | - | |||||||||||||
| 484 | QList<QByteArray> QIcuTimeZonePrivate::availableTimeZoneIds(int offsetFromUtc) const | - | ||||||||||||
| 485 | { | - | ||||||||||||
| 486 | // TODO Available directly in C++ api but not C api, from 4.8 onwards new filter method works | - | ||||||||||||
| 487 | #if U_ICU_VERSION_MAJOR_NUM >= 49 || (U_ICU_VERSION_MAJOR_NUM == 4 && U_ICU_VERSION_MINOR_NUM == 8) | - | ||||||||||||
| 488 | UErrorCode status = U_ZERO_ERROR; | - | ||||||||||||
| 489 | UEnumeration *uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, 0, | - | ||||||||||||
| 490 | &offsetFromUtc, &status); | - | ||||||||||||
| 491 | QList<QByteArray> result; | - | ||||||||||||
| 492 | if (U_SUCCESS(status))
| 0 | ||||||||||||
| 493 | result = uenumToIdList(uenum); never executed: result = uenumToIdList(uenum); | 0 | ||||||||||||
| 494 | uenum_close(uenum); | - | ||||||||||||
| 495 | return result; never executed: return result; | 0 | ||||||||||||
| 496 | #else | - | ||||||||||||
| 497 | return QTimeZonePrivate::availableTimeZoneIds(offsetFromUtc); | - | ||||||||||||
| 498 | #endif | - | ||||||||||||
| 499 | } | - | ||||||||||||
| 500 | - | |||||||||||||
| 501 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |