From 18439a449fdf7e60878329a42fdc55beca14b4c0 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 15 Jul 2022 15:50:33 +0200 Subject: Support serializing time-zone fields in date-times more flexibly [ChangeLog][QtCore][QDateTime] The 't' format character in a toString() template can now be repeated to get alternatives to the (unparseable) zone abbreviation. Thus 'tt' now gets the zone offset without colon, [+-]hhmm; 'ttt' gets the same with colon, [+-]hh:mm, and 'tttt' gets the zone name. Previously, each 't' was replaced by another copy of the abbreviation. Task-number: QTBUG-95966 Change-Id: Iccccd11f06fa732ed27c0e5d4e40a3d4b5f79f8d Reviewed-by: Thiago Macieira --- src/corelib/text/qlocale.cpp | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'src/corelib/text/qlocale.cpp') diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index e8ef690186..ba97d8cbb4 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -31,6 +31,9 @@ QT_WARNING_DISABLE_GCC("-Wfree-nonheap-object") // false positive tracking #include "qvariant.h" #include "qvarlengtharray.h" #include "qstringbuilder.h" +#if QT_CONFIG(timezone) +# include "qtimezone.h" +#endif #include "private/qnumeric_p.h" #include "private/qtools_p.h" #include @@ -3520,13 +3523,36 @@ QString QCalendarBackend::dateTimeToString(QStringView format, const QDateTime & } break; - case 't': + case 't': { used = true; - repeat = 1; - // If we have a QDateTime use the time spec otherwise use the current system tzname - result.append(formatDate ? datetime.timeZoneAbbreviation() - : QDateTime::currentDateTime().timeZoneAbbreviation()); + repeat = qMin(repeat, 4); + // If we don't have a date-time, use the current system time: + const QDateTime when = formatDate ? datetime : QDateTime::currentDateTime(); + QString text; + switch (repeat) { +#if QT_CONFIG(timezone) + case 4: + text = when.timeZone().displayName(when, QTimeZone::LongName); + break; +#endif // timezone + case 3: + case 2: + text = when.toOffsetFromUtc(when.offsetFromUtc()).timeZoneAbbreviation(); + // If the offset is UTC that'll be a Qt::UTC, otherwise Qt::OffsetFromUTC. + Q_ASSERT(text.startsWith("UTC"_L1)); + // The Qt::UTC case omits the zero offset, which we want: + text = text.size() == 3 ? u"+00:00"_s : text.sliced(3); + if (repeat == 2) // +hhmm format, rather than +hh:mm format + text = text.remove(u':'); + break; + default: + text = when.timeZoneAbbreviation(); + break; + } + if (!text.isEmpty()) + result.append(text); break; + } default: break; -- cgit v1.2.3