[llvm-branch-commits] [libcxx] [libc++][TZDB] Improves time zone format specifiers. (PR #85797)

Louis Dionne via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Apr 16 08:43:33 PDT 2024


================
@@ -170,10 +171,45 @@ _LIBCPP_HIDE_FROM_ABI void __format_century(basic_stringstream<_CharT>& __sstr,
   __sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "{:02}"), __century);
 }
 
+// Implements the %z format specifier according to [tab:time.format.spec], where
+// '__modifier' signals %Oz or %Ez were used. (Both modifiers behave the same,
+// so there is no need to distinguish between them.)
+template <class _CharT>
+_LIBCPP_HIDE_FROM_ABI void
+__format_zone_offset(basic_stringstream<_CharT>& __sstr, chrono::seconds __offset, bool __modifier) {
+  if (__offset < 0s) {
+    __sstr << _CharT('-');
+    __offset = -__offset;
+  } else
+    __sstr << _CharT('+');
+
+  chrono::hh_mm_ss __hms{__offset};
+  std::ostreambuf_iterator<_CharT> __out_it{__sstr};
+  if (__modifier)
+    std::format_to(__out_it, _LIBCPP_STATICALLY_WIDEN(_CharT, "{:%H:%M}"), __hms);
+  else
+    std::format_to(__out_it, _LIBCPP_STATICALLY_WIDEN(_CharT, "{:%H%M}"), __hms);
+}
+
+// Helper to store the time zone information needed for formatting.
+struct _LIBCPP_HIDE_FROM_ABI __time_zone {
+  // Typically these abbreviations as short and fit in the string's internal
+  // buffer.
+  string __abbrev{"UTC"};
+  chrono::seconds __offset{0};
----------------
ldionne wrote:

It feels weird to me to have these as default initializers. Instead I would not provide any defaults here, but I would initialize the result in `__convert_to_time_zone` below using `{"UTC", 0}`.

https://github.com/llvm/llvm-project/pull/85797


More information about the llvm-branch-commits mailing list