From d9ee2187011ab1328bf7161e7a751325c4a41208 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 25 Jan 2022 15:46:49 -0500 Subject: [PATCH] LibCore: Support DateTime string formatting of the form %z This formats the time zone offset as "+hhmm" or "-hhmm". --- Userland/Libraries/LibCore/DateTime.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Userland/Libraries/LibCore/DateTime.cpp b/Userland/Libraries/LibCore/DateTime.cpp index cc433e0aba9..1d0bfa0c409 100644 --- a/Userland/Libraries/LibCore/DateTime.cpp +++ b/Userland/Libraries/LibCore/DateTime.cpp @@ -105,6 +105,23 @@ String DateTime::to_string(const String& format) const StringBuilder builder; const int format_len = format.length(); + auto format_time_zone_offset = [&]() { + auto offset_seconds = -timezone; + StringView offset_sign; + + if (offset_seconds >= 0) { + offset_sign = "+"sv; + } else { + offset_sign = "-"sv; + offset_seconds *= -1; + } + + auto offset_hours = offset_seconds / 3600; + auto offset_minutes = (offset_seconds % 3600) / 60; + + builder.appendff("{}{:02}{:02}", offset_sign, offset_hours, offset_minutes); + }; + for (int i = 0; i < format_len; ++i) { if (format[i] != '%') { builder.append(format[i]); @@ -217,6 +234,9 @@ String DateTime::to_string(const String& format) const case 'Y': builder.appendff("{}", tm.tm_year + 1900); break; + case 'z': + format_time_zone_offset(); + break; case '%': builder.append('%'); break;