mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +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:
parent
d9ee218701
commit
9605be19bb
1 changed files with 12 additions and 3 deletions
|
@ -105,7 +105,7 @@ String DateTime::to_string(const String& format) const
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
const int format_len = format.length();
|
const int format_len = format.length();
|
||||||
|
|
||||||
auto format_time_zone_offset = [&]() {
|
auto format_time_zone_offset = [&](bool with_separator) {
|
||||||
auto offset_seconds = -timezone;
|
auto offset_seconds = -timezone;
|
||||||
StringView offset_sign;
|
StringView offset_sign;
|
||||||
|
|
||||||
|
@ -118,8 +118,9 @@ String DateTime::to_string(const String& format) const
|
||||||
|
|
||||||
auto offset_hours = offset_seconds / 3600;
|
auto offset_hours = offset_seconds / 3600;
|
||||||
auto offset_minutes = (offset_seconds % 3600) / 60;
|
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) {
|
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);
|
builder.appendff("{}", tm.tm_year + 1900);
|
||||||
break;
|
break;
|
||||||
case 'z':
|
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;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
builder.append('%');
|
builder.append('%');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue