LibC: Support a few other formats in strftime

This commit is contained in:
Lucas Chollet authored and Nico Weber committed 2026-09-21 20:12:39 -04:00
1 parent 3fcd7ac87b
commit df91038f4a
2 files changed
+48

No files matched your search

+21
View File
@@ -35,6 +35,9 @@ TEST_CASE(strftime)
EXPECT(strftime(str, sizeof str, "The full name of the month is %B.", &t));
EXPECT_EQ("The full name of the month is October."sv, str);
EXPECT(strftime(str, sizeof str, "The date and time is %c.", &t));
EXPECT_EQ("The date and time is Wed Oct 10 11:53:07 2018."sv, str);
EXPECT(strftime(str, sizeof str, "The century is %C.", &t));
EXPECT_EQ("The century is 20."sv, str);
@@ -47,6 +50,15 @@ TEST_CASE(strftime)
EXPECT(strftime(str, sizeof str, "The day of the month is %e.", &t));
EXPECT_EQ("The day of the month is 10."sv, str);
EXPECT(strftime(str, sizeof str, "The date is %F.", &t));
EXPECT_EQ("The date is 2018-10-10."sv, str);
EXPECT(strftime(str, sizeof str, "The two last digits of the week based year is %g.", &t));
EXPECT_EQ("The two last digits of the week based year is 18."sv, str);
EXPECT(strftime(str, sizeof str, "The week based year is %G.", &t));
EXPECT_EQ("The week based year is 2018."sv, str);
EXPECT(strftime(str, sizeof str, "The short name of the month is %h.", &t));
EXPECT_EQ("The short name of the month is Oct."sv, str);
@@ -101,6 +113,12 @@ TEST_CASE(strftime)
EXPECT(strftime(str, sizeof str, "The week of the year is %W.", &t));
EXPECT_EQ("The week of the year is 41."sv, str);
EXPECT(strftime(str, sizeof str, "The locale's appropriate date representation is %x.", &t));
EXPECT_EQ("The locale's appropriate date representation is 10/10/18."sv, str);
EXPECT(strftime(str, sizeof str, "The locale's appropriate time representation is %X.", &t));
EXPECT_EQ("The locale's appropriate time representation is 11:53:07."sv, str);
EXPECT(strftime(str, sizeof str, "The year is %y.", &t));
EXPECT_EQ("The year is 18."sv, str);
@@ -122,6 +140,9 @@ TEST_CASE(strftime_with_modifiers)
t.tm_yday = 282;
t.tm_isdst = -1;
EXPECT(strftime(str, sizeof str, "The date and time in the alternate representation is %Ec.", &t));
EXPECT_EQ("The date and time in the alternate representation is Wed Oct 10 11:53:07 2018."sv, str);
EXPECT(strftime(str, sizeof str, "The century in the alternate representation is %EC.", &t));
EXPECT_EQ("The century in the alternate representation is 20."sv, str);
+27
View File
@@ -302,6 +302,16 @@ size_t strftime(char* destination, size_t max_size, char const* format, const st
case 'B':
builder.append(long_month_names[tm->tm_mon]);
break;
case 'c':
builder.appendff("{} {} {:2} {:02}:{:02}:{:02} {}",
short_day_names[tm->tm_wday],
short_month_names[tm->tm_mon],
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
tm->tm_year + 1900);
break;
case 'C':
builder.appendff("{:02}", (tm->tm_year + 1900) / 100);
break;
@@ -319,6 +329,17 @@ size_t strftime(char* destination, size_t max_size, char const* format, const st
return 0;
has_e_modifier = true;
goto continue_reading_operator;
case 'F':
builder.appendff("{}-{:02}-{:02}", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
break;
case 'G':
dbgln("FIXME: Compute the week-based year instead of using the normal year.");
builder.appendff("{}", tm->tm_year + 1900);
break;
case 'g':
dbgln("FIXME: Compute the week-based year instead of using the normal year.");
builder.appendff("{:02}", (tm->tm_year + 1900) % 100);
break;
case 'h':
builder.append(short_month_names[tm->tm_mon]);
break;
@@ -410,6 +431,12 @@ size_t strftime(char* destination, size_t max_size, char const* format, const st
builder.appendff("{:02}", week_number);
break;
}
case 'x':
builder.appendff("{:02}/{:02}/{:02}", tm->tm_mon + 1, tm->tm_mday, (tm->tm_year + 1900) % 100);
break;
case 'X':
builder.appendff("{:02}:{:02}:{:02}", tm->tm_hour, tm->tm_min, tm->tm_sec);
break;
case 'y':
builder.appendff("{:02}", (tm->tm_year + 1900) % 100);
break;