diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-01-25 15:46:49 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-25 23:21:30 +0000 |
commit | d9ee2187011ab1328bf7161e7a751325c4a41208 (patch) | |
tree | 39cd0864c7d7f705055ddfb72576ab1f10e1dfe4 /Userland/Libraries/LibCore | |
parent | 6e38076b48164ddebf4f83ac99a11ca9385b8e6e (diff) | |
download | serenity-d9ee2187011ab1328bf7161e7a751325c4a41208.zip |
LibCore: Support DateTime string formatting of the form %z
This formats the time zone offset as "+hhmm" or "-hhmm".
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r-- | Userland/Libraries/LibCore/DateTime.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/DateTime.cpp b/Userland/Libraries/LibCore/DateTime.cpp index cc433e0aba..1d0bfa0c40 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; |