mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
LibCore: Support DateTime string formatting of the form %z
This formats the time zone offset as "+hhmm" or "-hhmm".
This commit is contained in:
parent
6e38076b48
commit
d9ee218701
Notes:
sideshowbarker
2024-07-18 03:35:30 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/d9ee2187011 Pull-request: https://github.com/SerenityOS/serenity/pull/12137
1 changed files with 20 additions and 0 deletions
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue