From c06e0b4ede84b52e4aa90ed35ff3061e792be6ea Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Sat, 10 Dec 2022 15:23:49 +0500 Subject: [PATCH] Extract timezone offset from timezone conversion functions Signed-off-by: Vladislav Shchapov --- include/fmt/chrono.h | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index dd0869db..b23bb1af 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -427,33 +427,14 @@ inline void do_write(buffer& buf, const std::tm& time, os.imbue(loc); using iterator = std::ostreambuf_iterator; const auto& facet = std::use_facet>(loc); - // std::time_put may not support '%Ez', '%Oz'. - bool modified_z = false; - if (format == 'z' && modifier != '\0') { - modified_z = true; - modifier = '\0'; - } auto end = facet.put(os, os, Char(' '), &time, format, modifier); if (end.failed()) FMT_THROW(format_error("failed to format time")); - if (modified_z) { - // Insert ':' into ISO 8601 formatted timezone - auto size = buf.size(); - buf.push_back(*(buf.end() - 1)); - Char* p = buf.data(); - p[size - 1] = p[size - 2]; - p[size - 2] = Char(':'); - } } template ::value)> auto write(OutputIt out, const std::tm& time, const std::locale& loc, char format, char modifier = 0) -> OutputIt { - if (format == 'z' && modifier != '\0') { - auto&& buf = basic_memory_buffer(); - do_write(buf, time, loc, format, modifier); - return copy_str(buf.begin(), buf.end(), out); - } auto&& buf = get_buffer(out); do_write(buf, time, loc, format, modifier); return get_iterator(buf, out); @@ -1274,8 +1255,15 @@ class tm_writer { } write_utc_offset(-offset, ns); #else - ignore_unused(tm); - format_localized('z', ns == numeric_system::standard ? '\0' : 'E'); + if (ns == numeric_system::standard) return format_localized('z'); + + // Extract timezone offset from timezone conversion functions. + std::tm gtm = tm; + std::time_t gt = std::mktime(>m); + std::tm ltm = gmtime(gt); + std::time_t lt = std::mktime(<m); + long offset = gt - lt; + write_utc_offset(offset, ns); #endif }