summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-25 16:04:54 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-25 23:21:30 +0000
commit9605be19bb41f89ec23706cfb8ea14773f49351b (patch)
tree07062f11cc4ba6b2403bd30bb179244cb5981ae9 /Userland/Libraries/LibCore
parentd9ee2187011ab1328bf7161e7a751325c4a41208 (diff)
downloadserenity-9605be19bb41f89ec23706cfb8ea14773f49351b.zip
LibCore: Support DateTime string formatting of the form %:z
This formats the time zone offset as "+hh:mm" or "-hh:mm". This doesn't appear to be strictly POSIX, but it is how Linux implements this format.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r--Userland/Libraries/LibCore/DateTime.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/Userland/Libraries/LibCore/DateTime.cpp b/Userland/Libraries/LibCore/DateTime.cpp
index 1d0bfa0c40..490a27d2f6 100644
--- a/Userland/Libraries/LibCore/DateTime.cpp
+++ b/Userland/Libraries/LibCore/DateTime.cpp
@@ -105,7 +105,7 @@ String DateTime::to_string(const String& format) const
StringBuilder builder;
const int format_len = format.length();
- auto format_time_zone_offset = [&]() {
+ auto format_time_zone_offset = [&](bool with_separator) {
auto offset_seconds = -timezone;
StringView offset_sign;
@@ -118,8 +118,9 @@ String DateTime::to_string(const String& format) const
auto offset_hours = offset_seconds / 3600;
auto offset_minutes = (offset_seconds % 3600) / 60;
+ auto separator = with_separator ? ":"sv : ""sv;
- builder.appendff("{}{:02}{:02}", offset_sign, offset_hours, offset_minutes);
+ builder.appendff("{}{:02}{}{:02}", offset_sign, offset_hours, separator, offset_minutes);
};
for (int i = 0; i < format_len; ++i) {
@@ -235,7 +236,15 @@ String DateTime::to_string(const String& format) const
builder.appendff("{}", tm.tm_year + 1900);
break;
case 'z':
- format_time_zone_offset();
+ format_time_zone_offset(false);
+ break;
+ case ':':
+ if (++i == format_len)
+ return String::empty();
+ if (format[i] != 'z')
+ return String::empty();
+
+ format_time_zone_offset(true);
break;
case '%':
builder.append('%');