LibCore: Support DateTime string formatting of the form %z

This formats the time zone offset as "+hhmm" or "-hhmm".
This commit is contained in:
Timothy Flynn 2022-01-25 15:46:49 -05:00 committed by Linus Groh
parent 6e38076b48
commit d9ee218701
Notes: sideshowbarker 2024-07-18 03:35:30 +09:00

View file

@ -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;