From b4b6fb6d2a0ac08ce3138239b1442413de27cfa0 Mon Sep 17 00:00:00 2001 From: no92 Date: Sat, 6 Jul 2024 18:11:15 +0200 Subject: [PATCH] tests/ansi: add tests for various new strftime modifiers --- options/ansi/generic/time-stubs.cpp | 6 ++++-- tests/ansi/strftime.c | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/options/ansi/generic/time-stubs.cpp b/options/ansi/generic/time-stubs.cpp index 27a838c642..790e82f7f3 100644 --- a/options/ansi/generic/time-stubs.cpp +++ b/options/ansi/generic/time-stubs.cpp @@ -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; @@ -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(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) diff --git a/tests/ansi/strftime.c b/tests/ansi/strftime.c index 69ec6427dc..e30df21cb6 100644 --- a/tests/ansi/strftime.c +++ b/tests/ansi/strftime.c @@ -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; @@ -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"));