From 4d2e18fb07eba8130fccf1d0e7c8b62e76deae88 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 21 Apr 2022 14:31:38 +0100 Subject: [PATCH] LibCore: Output invalid DateTime::to_string() specifiers as literals While working on #13764 I noticed that DateTime::to_string() would just return an empty String if the format included an invalid specifier (eg `%Q`). This seems to be a mistake. POSIX date(1), which I believe we are basing our implementation on, only replaces valid specifiers, and any invalid ones get included as literals in the output. For example, on Linux `date "+%Quiz"` returns "%Quiz", but we were returning "". --- Userland/Libraries/LibCore/DateTime.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibCore/DateTime.cpp b/Userland/Libraries/LibCore/DateTime.cpp index 0f3530f375..3ffaa79d62 100644 --- a/Userland/Libraries/LibCore/DateTime.cpp +++ b/Userland/Libraries/LibCore/DateTime.cpp @@ -239,11 +239,15 @@ String DateTime::to_string(StringView format) const format_time_zone_offset(false); break; case ':': - if (++i == format_len) - return String::empty(); - if (format[i] != 'z') - return String::empty(); - + if (++i == format_len) { + builder.append("%:"); + break; + } + if (format[i] != 'z') { + builder.append("%:"); + builder.append(format[i]); + break; + } format_time_zone_offset(true); break; case 'Z': @@ -253,7 +257,9 @@ String DateTime::to_string(StringView format) const builder.append('%'); break; default: - return String(); + builder.append('%'); + builder.append(format[i]); + break; } } }