scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 769d229c5a8a0b0ce9da52d181c592fbba272583
parent 8d5c42a0606b6ba6528c8fc4740fa75f4793051e
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Tue, 21 Apr 2026 10:15:01 +0200

tests/libc: Add 0069-mktime

This test checks the mktime and friends in the scope of a
UTC timezone, that is the minimum that we can expect from
an implementation. Following tests will check for more
complex timezone scenarios, but they will not be portable
by definition, so we will have to implement some kind of
skip mechanism.

Diffstat:
Mtests/libc/execute/.gitignore | 1+
Atests/libc/execute/0069-mktime.c | 233+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/libc/execute/libc-tests.lst | 1+
3 files changed, 235 insertions(+), 0 deletions(-)

diff --git a/tests/libc/execute/.gitignore b/tests/libc/execute/.gitignore @@ -67,3 +67,4 @@ test.log 0066-wctob 0067-wcwidth 0068-fseek +0069-mktime diff --git a/tests/libc/execute/0069-mktime.c b/tests/libc/execute/0069-mktime.c @@ -0,0 +1,233 @@ +/* +output: +testing +test 0 +tm_sec= 0, tm_min= 3, tm_hour= 0, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 +Fri Jan 1 00:03:00 2021 +test 1 +tm_sec=50, tm_min=30, tm_hour=23, tm_mday=31, tm_mon=11, tm_year= 120, tm_wday=4, tm_yday=365, tm_isdst=0 +Thu Dec 31 23:30:50 2020 UTC +Thu Dec 31 23:30:50 2020 UTC +Thu Dec 31 23:30:50 2020 UTC +Thu Dec 31 23:30:50 2020 +Thu Dec 31 23:30:50 2020 +test 2 +tm_sec=59, tm_min=30, tm_hour= 0, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 00:30:59 2021 UTC +Fri Jan 1 00:30:59 2021 UTC +Fri Jan 1 00:30:59 2021 UTC +Fri Jan 1 00:30:59 2021 +Fri Jan 1 00:30:59 2021 +test 3 +tm_sec=59, tm_min=30, tm_hour=12, tm_mday=30, tm_mon=11, tm_year= 120, tm_wday=3, tm_yday=364, tm_isdst=0 +Wed Dec 30 12:30:59 2020 UTC +Wed Dec 30 12:30:59 2020 UTC +Wed Dec 30 12:30:59 2020 UTC +Wed Dec 30 12:30:59 2020 +Wed Dec 30 12:30:59 2020 +test 4 +tm_sec=59, tm_min=30, tm_hour=12, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 12:30:59 2021 UTC +Fri Jan 1 12:30:59 2021 UTC +Fri Jan 1 12:30:59 2021 UTC +Fri Jan 1 12:30:59 2021 +Fri Jan 1 12:30:59 2021 +test 5 +tm_sec=59, tm_min=59, tm_hour=23, tm_mday=31, tm_mon=11, tm_year= 120, tm_wday=4, tm_yday=365, tm_isdst=0 +Thu Dec 31 23:59:59 2020 UTC +Thu Dec 31 23:59:59 2020 UTC +Thu Dec 31 23:59:59 2020 UTC +Thu Dec 31 23:59:59 2020 +Thu Dec 31 23:59:59 2020 +test 6 +tm_sec= 0, tm_min= 3, tm_hour= 0, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 +Fri Jan 1 00:03:00 2021 +test 7 +tm_sec=59, tm_min=30, tm_hour=12, tm_mday=30, tm_mon=11, tm_year= 120, tm_wday=3, tm_yday=364, tm_isdst=0 +Wed Dec 30 12:30:59 2020 UTC +Wed Dec 30 12:30:59 2020 UTC +Wed Dec 30 12:30:59 2020 UTC +Wed Dec 30 12:30:59 2020 +Wed Dec 30 12:30:59 2020 +test 8 +tm_sec=59, tm_min=30, tm_hour=12, tm_mday=20, tm_mon= 0, tm_year= 121, tm_wday=3, tm_yday= 19, tm_isdst=0 +Wed Jan 20 12:30:59 2021 UTC +Wed Jan 20 12:30:59 2021 UTC +Wed Jan 20 12:30:59 2021 UTC +Wed Jan 20 12:30:59 2021 +Wed Jan 20 12:30:59 2021 +test 9 +tm_sec=59, tm_min=59, tm_hour=23, tm_mday=31, tm_mon=11, tm_year= 120, tm_wday=4, tm_yday=365, tm_isdst=0 +Thu Dec 31 23:59:59 2020 UTC +Thu Dec 31 23:59:59 2020 UTC +Thu Dec 31 23:59:59 2020 UTC +Thu Dec 31 23:59:59 2020 +Thu Dec 31 23:59:59 2020 +test 10 +tm_sec= 0, tm_min= 0, tm_hour= 0, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 00:00:00 2021 UTC +Fri Jan 1 00:00:00 2021 UTC +Fri Jan 1 00:00:00 2021 UTC +Fri Jan 1 00:00:00 2021 +Fri Jan 1 00:00:00 2021 +test 11 +tm_sec=59, tm_min=38, tm_hour=12, tm_mday=10, tm_mon=11, tm_year= 33, tm_wday=0, tm_yday=343, tm_isdst=0 +Sun Dec 10 12:38:59 1933 UTC +Sun Dec 10 12:38:59 1933 UTC +Sun Dec 10 12:38:59 1933 UTC +Sun Dec 10 12:38:59 1933 +Sun Dec 10 12:38:59 1933 +test 12 +tm_sec=59, tm_min=38, tm_hour=12, tm_mday=12, tm_mon=11, tm_year= 33, tm_wday=2, tm_yday=345, tm_isdst=0 +Tue Dec 12 12:38:59 1933 UTC +Tue Dec 12 12:38:59 1933 UTC +Tue Dec 12 12:38:59 1933 UTC +Tue Dec 12 12:38:59 1933 +Tue Dec 12 12:38:59 1933 +test 13 +tm_wday=200, tm_yday=500 +failed +test 14 +tm_sec= 0, tm_min= 3, tm_hour= 0, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 +Fri Jan 1 00:03:00 2021 +test 15 +tm_sec= 0, tm_min= 3, tm_hour= 0, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 +Fri Jan 1 00:03:00 2021 +test 16 +tm_sec= 0, tm_min= 3, tm_hour= 0, tm_mday= 1, tm_mon= 0, tm_year= 121, tm_wday=5, tm_yday= 0, tm_isdst=0 +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 UTC +Fri Jan 1 00:03:00 2021 +Fri Jan 1 00:03:00 2021 +done +end: +*/ + +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +#define NELEM(x) (sizeof(x)/sizeof((x)[0])) + +struct test { + struct tm tm; + int fail; +} tests[] = { + //sec,min,hour,mday,mon,year,wday,yday,isdst,fail + {{ 60, 62, 23, 31, 11, 120, 200, 500}}, + {{ 50, 30, -1, 1, 0, 121}}, + {{ 59, 30, 24, 31, 11, 120}}, + {{ 59, 30, 12, -1, 0, 121}}, + {{ 59, 30, 12, 32, 11, 120}}, + {{ 59, -1, 0, 1, 0, 121}}, + {{ 60, 62, 23, 31, 11, 120}}, + {{ 59, 30, 12, 30, -1, 121}}, + {{ 59, 30, 12, 20, 12, 120}}, + {{ -1, 0, 0, 1, 0, 121}}, + {{ 60, 59, 23, 31, 11, 120}}, + {{ 59, 38, 12, 10, 11, 33}}, + {{ 59, 38, 12, 12, 11, 33}}, + {{ 72, 38, 1, 31, 15,INT_MAX, 200, 500}, 1}, + {{ 60, 62, 23, 31, 11, 120, 0, 0, 1}}, + {{ 60, 62, 23, 31, 11, 120, 0, 0, 0}}, + {{ 60, 62, 23, 31, 11, 120, 0, 0, -1}}, +}; + +#ifndef __unix__ +#define putenv(x) (x) +/* + * Ok, this is by definition undefined behaviour because + * we are using the name of a library function. The reallity + * is that if you use a static linker then this would + * overload the libc getenv and it will work as expected, + * but if you use a dynamic linker then the internal + * references will be tied to the internal getenv function + * and this would not work. For our main use case that is + * testing scc libc this is good enough, and with the + * fallback to putenv this will work in the systems we are + * interested on + */ +char * +getenv(const char *name) +{ + if (strcmp(name, "TZ") != 0) + return NULL; + return "UTC"; +} +#endif + +int +main(void) +{ + time_t t; + char buf[70]; + struct tm *tm; + struct test *tp; + + puts("testing"); + putenv("TZ=UTC"); + for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) { + printf("test %d\n", (int) (tp - tests)); + t = mktime(&tp->tm); + if (tp->fail) { + printf("tm_wday=%d, tm_yday=%d\n", + tp->tm.tm_wday, tp->tm.tm_yday); + puts(t == (time_t) -1 ? "failed" : "not failed"); + continue; + } + printf("tm_sec=%2d, " + "tm_min=%2d, " + "tm_hour=%2d, " + "tm_mday=%2d, " + "tm_mon=%2d, " + "tm_year=%4d, " + "tm_wday=%d, " + "tm_yday=%3d, " + "tm_isdst=%d\n", + tp->tm.tm_sec, + tp->tm.tm_min, + tp->tm.tm_hour, + tp->tm.tm_mday, + tp->tm.tm_mon, + tp->tm.tm_year, + tp->tm.tm_wday, + tp->tm.tm_yday, + tp->tm.tm_isdst); + strftime(buf, sizeof(buf), "%c %Z", &tp->tm); + puts(buf); + + tm = gmtime(&t); + strftime(buf, sizeof(buf), "%c %Z", tm); + puts(buf); + + tm = localtime(&t); + strftime(buf, sizeof(buf), "%c %Z", tm); + puts(buf); + + fputs(asctime(tm), stdout); + fputs(ctime(&t), stdout); + } + puts("done"); + + return 0; +} + diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst @@ -65,3 +65,4 @@ 0066-wctob 0067-wcwidth [TODO] 0068-fseek +0069-mktime