LibC+LibCore: Properly format 12-hour formatted hours

This fixes a small formatting issue where midnight and noon would
display as 00 when they should display as 12.
This commit is contained in:
cflip 2022-04-12 17:53:04 -06:00 committed by Tim Flynn
parent 31ca48ebb2
commit 0500d49acb
Notes: sideshowbarker 2024-07-18 05:37:06 +09:00
2 changed files with 24 additions and 8 deletions

View file

@ -250,9 +250,13 @@ size_t strftime(char* destination, size_t max_size, char const* format, const st
case 'H':
builder.appendff("{:02}", tm->tm_hour);
break;
case 'I':
builder.appendff("{:02}", tm->tm_hour % 12);
case 'I': {
int display_hour = tm->tm_hour % 12;
if (display_hour == 0)
display_hour = 12;
builder.appendff("{:02}", display_hour);
break;
}
case 'j':
builder.appendff("{:03}", tm->tm_yday + 1);
break;
@ -268,9 +272,13 @@ size_t strftime(char* destination, size_t max_size, char const* format, const st
case 'p':
builder.append(tm->tm_hour < 12 ? "a.m." : "p.m.");
break;
case 'r':
builder.appendff("{:02}:{:02}:{:02} {}", tm->tm_hour % 12, tm->tm_min, tm->tm_sec, tm->tm_hour < 12 ? "a.m." : "p.m.");
case 'r': {
int display_hour = tm->tm_hour % 12;
if (display_hour == 0)
display_hour = 12;
builder.appendff("{:02}:{:02}:{:02} {}", display_hour, tm->tm_min, tm->tm_sec, tm->tm_hour < 12 ? "a.m." : "p.m.");
break;
}
case 'R':
builder.appendff("{:02}:{:02}", tm->tm_hour, tm->tm_min);
break;

View file

@ -153,9 +153,13 @@ String DateTime::to_string(StringView format) const
case 'H':
builder.appendff("{:02}", tm.tm_hour);
break;
case 'I':
builder.appendff("{:02}", tm.tm_hour % 12);
case 'I': {
int display_hour = tm.tm_hour % 12;
if (display_hour == 0)
display_hour = 12;
builder.appendff("{:02}", display_hour);
break;
}
case 'j':
builder.appendff("{:03}", tm.tm_yday + 1);
break;
@ -171,9 +175,13 @@ String DateTime::to_string(StringView format) const
case 'p':
builder.append(tm.tm_hour < 12 ? "a.m." : "p.m.");
break;
case 'r':
builder.appendff("{:02}:{:02}:{:02} {}", tm.tm_hour % 12, tm.tm_min, tm.tm_sec, tm.tm_hour < 12 ? "a.m." : "p.m.");
case 'r': {
int display_hour = tm.tm_hour % 12;
if (display_hour == 0)
display_hour = 12;
builder.appendff("{:02}:{:02}:{:02} {}", display_hour, tm.tm_min, tm.tm_sec, tm.tm_hour < 12 ? "a.m." : "p.m.");
break;
}
case 'R':
builder.appendff("{:02}:{:02}", tm.tm_hour, tm.tm_min);
break;