1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:07:35 +00:00

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.
This commit is contained in:
Timothy Flynn 2022-01-25 16:04:54 -05:00 committed by Linus Groh
parent d9ee218701
commit 9605be19bb

View file

@ -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('%');