1/*2output:3testing4test 05tm_sec=59, tm_min=30, tm_hour=12, tm_mday=28, tm_mon= 0, tm_year= 121, tm_wday=4, tm_yday= 27, tm_isdst=06Thu Jan 28 12:30:59 2021 CET7Thu Jan 28 13:30:59 2021 UTC8Thu Jan 28 12:30:59 2021 CET9Thu Jan 28 12:30:59 202110Thu Jan 28 12:30:59 202111done12end:13*/1415#include <assert.h>16#include <stdio.h>17#include <stdlib.h>18#include <string.h>19#include <time.h>2021#define NELEM(x) (sizeof(x)/sizeof((x)[0]))2223struct test {24 struct tm tm;25 char *tz;26} tests[] = {27 //sec,min,hour,mday,mon,year,wday,yday,isdst,tz,28 {{ 59, 30, 12, 28, 0, 121, 0, 0, 0},"CET01CEST"},29};3031char *faketz;3233#ifndef __unix__34#define putenv(x) (x)35/*36 * Ok, this is by definition undefined behaviour because37 * we are using the name of a library function. The reallity38 * is that if you use a static linker then this would39 * overload the libc getenv and it will work as expected,40 * but if you use a dynamic linker then the internal41 * references will be tied to the internal getenv function42 * and this would not work. For our main use case that is43 * testing scc libc this is good enough, and with the44 * fallback to putenv and unsetenv this will work in45 * the systems we are interested on46 */47char *48getenv(const char *name)49{50 if (strcmp(name, "TZ") != 0)51 return NULL;52 return faketz;53}54#endif5556int57test(void)58{59 time_t t;60 char buf[70], var[70], *tz;61 struct tm *tm;62 struct test *tp;6364 for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {65 printf("test %d\n", (int) (tp - tests));66 tz = tp->tz;67 if (!tz)68 tz = "UTC";69 faketz = tz;70 sprintf(var, "TZ=%s", tz);71 putenv(var);7273 t = mktime(&tp->tm);74 if (t == -1) {75 puts("failed");76 continue;77 }78 printf("tm_sec=%2d, "79 "tm_min=%2d, "80 "tm_hour=%2d, "81 "tm_mday=%2d, "82 "tm_mon=%2d, "83 "tm_year=%4d, "84 "tm_wday=%d, "85 "tm_yday=%3d, "86 "tm_isdst=%d\n",87 tp->tm.tm_sec,88 tp->tm.tm_min,89 tp->tm.tm_hour,90 tp->tm.tm_mday,91 tp->tm.tm_mon,92 tp->tm.tm_year,93 tp->tm.tm_wday,94 tp->tm.tm_yday,95 tp->tm.tm_isdst);96 strftime(buf, sizeof(buf), "%c %Z", &tp->tm);97 puts(buf);9899 tm = gmtime(&t);100 strftime(buf, sizeof(buf), "%c %Z", tm);101 puts(buf);102103 tm = localtime(&t);104 strftime(buf, sizeof(buf), "%c %Z", tm);105 puts(buf);106107 fputs(asctime(tm), stdout);108 fputs(ctime(&t), stdout);109 }110111 return 0;112}113114int115main(void)116{117#if !defined(__SCC_TZ_TIME__)118 puts("RESULT: SKIP");119 return 0;120#endif121 puts("testing");122 test();123 puts("done");124 return 0;125}