LibC: Use `nl_langinfo` for strings in `strftime`

This commit is contained in:
Bananymous 2025-04-19 20:03:56 +03:00
parent 407eed212a
commit e62cc17797
1 changed files with 25 additions and 27 deletions

View File

@ -3,6 +3,7 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <langinfo.h>
#include <string.h> #include <string.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <time.h> #include <time.h>
@ -201,20 +202,6 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
char modifier = '\0'; char modifier = '\0';
}; };
static constexpr const char* abbr_wday[] {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static constexpr const char* full_wday[] {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
static constexpr const char* abbr_mon[] {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static constexpr const char* full_mon[] {
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};
const auto append_string = const auto append_string =
[&s, &len, &maxsize](const char* string) -> bool [&s, &len, &maxsize](const char* string) -> bool
{ {
@ -226,12 +213,23 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
return true; return true;
}; };
const auto append_string_from_list = const auto append_month =
[&append_string]<size_t LIST_SIZE>(int index, const char* const (&list)[LIST_SIZE]) -> bool [&append_string](int index, bool abbreviated)
{ {
const nl_item base = abbreviated ? ABMON_1 : MON_1;
const char* string = "INVALID"; const char* string = "INVALID";
if (index >= 0 && index < (int)LIST_SIZE) if (index >= 0 && index < 12)
string = list[index]; string = nl_langinfo(base + index);
return append_string(string);
};
const auto append_weekday =
[&append_string](int index, bool abbreviated)
{
const nl_item base = abbreviated ? ABDAY_1 : DAY_1;
const char* string = "INVALID";
if (index >= 0 && index < 7)
string = nl_langinfo(base + index);
return append_string(string); return append_string(string);
}; };
@ -308,24 +306,24 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
switch (*format) switch (*format)
{ {
case 'a': case 'a':
if (!append_string_from_list(timeptr->tm_wday, abbr_wday)) if (!append_weekday(timeptr->tm_wday, true))
return 0; return 0;
break; break;
case 'A': case 'A':
if (!append_string_from_list(timeptr->tm_wday, full_wday)) if (!append_weekday(timeptr->tm_wday, false))
return 0; return 0;
break; break;
case 'b': case 'b':
case 'h': case 'h':
if (!append_string_from_list(timeptr->tm_mon, abbr_mon)) if (!append_month(timeptr->tm_mon, true))
return 0; return 0;
break; break;
case 'B': case 'B':
if (!append_string_from_list(timeptr->tm_mon, full_mon)) if (!append_month(timeptr->tm_mon, false))
return 0; return 0;
break; break;
case 'c': case 'c':
if (size_t ret = strftime(s + len, maxsize - len, "%a %b %e %H:%M:%S %Y", timeptr)) if (size_t ret = strftime(s + len, maxsize - len, nl_langinfo(D_T_FMT), timeptr))
len += ret; len += ret;
else return 0; else return 0;
break; break;
@ -401,11 +399,11 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
s[len++] = '\n'; s[len++] = '\n';
break; break;
case 'p': case 'p':
if (!append_string(timeptr->tm_hour < 12 ? "AM" : "PM")) if (!append_string(timeptr->tm_hour < 12 ? nl_langinfo(AM_STR) : nl_langinfo(PM_STR)))
return 0; return 0;
break; break;
case 'r': case 'r':
if (size_t ret = strftime(s + len, maxsize - len, "%I:%M:%S %p", timeptr)) if (size_t ret = strftime(s + len, maxsize - len, nl_langinfo(T_FMT_AMPM), timeptr))
len += ret; len += ret;
else return 0; else return 0;
break; break;
@ -501,12 +499,12 @@ size_t strftime(char* __restrict s, size_t maxsize, const char* __restrict forma
return 0; return 0;
break; break;
case 'x': case 'x':
if (size_t ret = strftime(s + len, maxsize - len, "%m/%d/%y", timeptr)) if (size_t ret = strftime(s + len, maxsize - len, nl_langinfo(D_FMT), timeptr))
len += ret; len += ret;
else return 0; else return 0;
break; break;
case 'X': case 'X':
if (size_t ret = strftime(s + len, maxsize - len, "%H:%M:%S", timeptr)) if (size_t ret = strftime(s + len, maxsize - len, nl_langinfo(T_FMT), timeptr))
len += ret; len += ret;
else return 0; else return 0;
break; break;