mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:07:35 +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
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…
Add table
Add a link
Reference in a new issue