summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qlocale.cpp
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2022-07-15 15:50:33 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2022-09-29 14:42:56 +0200
commit18439a449fdf7e60878329a42fdc55beca14b4c0 (patch)
tree9cca8400b7e8b7d47d435566d32fb09c88912645 /src/corelib/text/qlocale.cpp
parent0bc92c01e364b89250b7d23ec3078563548609f8 (diff)
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qlocale.cpp')
-rw-r--r--src/corelib/text/qlocale.cpp36
1 files changed, 31 insertions, 5 deletions
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 <cmath>
@@ -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;