mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:17:34 +00:00
Everywhere: Deduplicate day/month name constants
Day and month name constants are defined in numerous places. This pulls them together into a single place and eliminates the duplication. It also ensures they are `constexpr`.
This commit is contained in:
parent
759857b597
commit
4c5e9f5633
7 changed files with 79 additions and 134 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/DateConstants.h>
|
||||
#include <langinfo.h>
|
||||
|
||||
// Values taken from glibc's en_US locale files.
|
||||
|
@ -25,81 +26,47 @@ static const char* __nl_langinfo(nl_item item)
|
|||
case PM_STR:
|
||||
return "PM";
|
||||
case DAY_1:
|
||||
return "Sunday";
|
||||
case DAY_2:
|
||||
return "Monday";
|
||||
case DAY_3:
|
||||
return "Tuesday";
|
||||
case DAY_4:
|
||||
return "Wednesday";
|
||||
case DAY_5:
|
||||
return "Thursday";
|
||||
case DAY_6:
|
||||
return "Friday";
|
||||
case DAY_7:
|
||||
return "Saturday";
|
||||
return long_day_names[item - DAY_1].characters_without_null_termination();
|
||||
case ABDAY_1:
|
||||
return "Sun";
|
||||
case ABDAY_2:
|
||||
return "Mon";
|
||||
case ABDAY_3:
|
||||
return "Tue";
|
||||
case ABDAY_4:
|
||||
return "Wed";
|
||||
case ABDAY_5:
|
||||
return "Thu";
|
||||
case ABDAY_6:
|
||||
return "Fri";
|
||||
case ABDAY_7:
|
||||
return "Sat";
|
||||
return short_day_names[item - ABDAY_1].characters_without_null_termination();
|
||||
case MON_1:
|
||||
return "January";
|
||||
case MON_2:
|
||||
return "February";
|
||||
case MON_3:
|
||||
return "March";
|
||||
case MON_4:
|
||||
return "April";
|
||||
case MON_5:
|
||||
return "May";
|
||||
case MON_6:
|
||||
return "June";
|
||||
case MON_7:
|
||||
return "July";
|
||||
case MON_8:
|
||||
return "August";
|
||||
case MON_9:
|
||||
return "September";
|
||||
case MON_10:
|
||||
return "October";
|
||||
case MON_11:
|
||||
return "November";
|
||||
case MON_12:
|
||||
return "December";
|
||||
return long_month_names[item - MON_1].characters_without_null_termination();
|
||||
case ABMON_1:
|
||||
return "Jan";
|
||||
case ABMON_2:
|
||||
return "Feb";
|
||||
case ABMON_3:
|
||||
return "Mar";
|
||||
case ABMON_4:
|
||||
return "Apr";
|
||||
case ABMON_5:
|
||||
return "May";
|
||||
case ABMON_6:
|
||||
return "Jun";
|
||||
case ABMON_7:
|
||||
return "Jul";
|
||||
case ABMON_8:
|
||||
return "Aug";
|
||||
case ABMON_9:
|
||||
return "Sep";
|
||||
case ABMON_10:
|
||||
return "Oct";
|
||||
case ABMON_11:
|
||||
return "Nov";
|
||||
case ABMON_12:
|
||||
return "Dec";
|
||||
return short_month_names[item - ABMON_1].characters_without_null_termination();
|
||||
case RADIXCHAR:
|
||||
return ".";
|
||||
case THOUSEP:
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/DateConstants.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Time.h>
|
||||
|
@ -208,21 +209,6 @@ size_t strftime(char* destination, size_t max_size, const char* format, const st
|
|||
{
|
||||
tzset();
|
||||
|
||||
const char wday_short_names[7][4] = {
|
||||
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
||||
};
|
||||
const char wday_long_names[7][10] = {
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
|
||||
};
|
||||
const char mon_short_names[12][4] = {
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
const char mon_long_names[12][10] = {
|
||||
"January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December"
|
||||
};
|
||||
|
||||
StringBuilder builder { max_size };
|
||||
|
||||
const int format_len = strlen(format);
|
||||
|
@ -235,16 +221,16 @@ size_t strftime(char* destination, size_t max_size, const char* format, const st
|
|||
|
||||
switch (format[i]) {
|
||||
case 'a':
|
||||
builder.append(wday_short_names[tm->tm_wday]);
|
||||
builder.append(short_day_names[tm->tm_wday]);
|
||||
break;
|
||||
case 'A':
|
||||
builder.append(wday_long_names[tm->tm_wday]);
|
||||
builder.append(long_day_names[tm->tm_wday]);
|
||||
break;
|
||||
case 'b':
|
||||
builder.append(mon_short_names[tm->tm_mon]);
|
||||
builder.append(short_month_names[tm->tm_mon]);
|
||||
break;
|
||||
case 'B':
|
||||
builder.append(mon_long_names[tm->tm_mon]);
|
||||
builder.append(long_month_names[tm->tm_mon]);
|
||||
break;
|
||||
case 'C':
|
||||
builder.appendff("{:02}", (tm->tm_year + 1900) / 100);
|
||||
|
@ -259,7 +245,7 @@ size_t strftime(char* destination, size_t max_size, const char* format, const st
|
|||
builder.appendff("{:2}", tm->tm_mday);
|
||||
break;
|
||||
case 'h':
|
||||
builder.append(mon_short_names[tm->tm_mon]);
|
||||
builder.append(short_month_names[tm->tm_mon]);
|
||||
break;
|
||||
case 'H':
|
||||
builder.appendff("{:02}", tm->tm_hour);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue