Skip to content

Commit

Permalink
tests/ansi: add tests for various new strftime modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
no92 committed Jul 6, 2024
1 parent 4ea04b2 commit b4b6fb6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions options/ansi/generic/time-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ size_t strftime(char *__restrict dest, size_t max_size,
if(mon < 0 || mon > 11)
__ensure(!"Month not in bounds.");

chunk = snprintf(p, space, "%s %s %2d %.2i:%.2i:%.2d %d", mlibc::nl_langinfo(ABDAY_1 + day),
chunk = snprintf(p, space, "%s %s %2d %.2i:%.2i:%.2d %d", mlibc::nl_langinfo(ABDAY_1 + day),
mlibc::nl_langinfo(ABMON_1 + mon), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, 1900 + tm->tm_year);
if(chunk >= space)
return 0;
Expand Down Expand Up @@ -329,8 +329,10 @@ size_t strftime(char *__restrict dest, size_t max_size,
}
case 'P': {
char *str = mlibc::nl_langinfo((tm->tm_hour < 12) ? AM_STR : PM_STR);
char *str_lower = reinterpret_cast<char *>(getAllocator().allocate(strlen(str) + 1));
for(size_t i = 0; str[i]; i++)
str[i] = tolower(str[i]);
str_lower[i] = tolower(str[i]);
str_lower[strlen(str)] = '\0';

chunk = snprintf(p, space, "%s", str);
if(chunk >= space)
Expand Down
22 changes: 20 additions & 2 deletions tests/ansi/strftime.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ int main() {
fputs("strftime testcase could not set locale, errors may be expected!", stderr);
}

char timebuf[16];
char result[16] = " 8";
char timebuf[32];
char result[32] = " 8";
struct tm tm;
tm.tm_sec = 0;
tm.tm_min = 17;
Expand All @@ -33,6 +33,24 @@ int main() {
strftime(timebuf, sizeof(timebuf), "%X", &tm);
assert(!strcmp(timebuf, "17:17:00"));

memset(timebuf, 0, sizeof(timebuf));
strftime(timebuf, sizeof(timebuf), "%D", &tm);
assert(!strcmp(timebuf, "03/08/21"));

memset(timebuf, 0, sizeof(timebuf));
strftime(timebuf, sizeof(timebuf), "%OB %Ob", &tm);
assert(!strcmp(timebuf, "March Mar"));

memset(timebuf, 0, sizeof(timebuf));
strftime(timebuf, sizeof(timebuf), "%c", &tm);
memset(result, 0, sizeof(result));
strftime(result, sizeof(result), "%a %b %e %H:%M:%S %Y", &tm);
assert(!strcmp(timebuf, result));

memset(timebuf, 0, sizeof(timebuf));
strftime(timebuf, sizeof(timebuf), "%k %p %P%n", &tm);
assert(!strcmp(timebuf, "17 PM pm\n"));

memset(timebuf, 0, sizeof(timebuf));
strftime(timebuf, sizeof(timebuf), "%a %A", &tm);
assert(!strcmp(timebuf, "Tue Tuesday"));
Expand Down

0 comments on commit b4b6fb6

Please sign in to comment.