scc

A fork of https://git.simple-cc.org/scc/ for Qute

git clone https://git.8pit.net/scc.git

  1/*
  2output:
  3testing
  4test 0
  5tm_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=0
  6Thu Jan 28 12:30:59 2021 CET
  7Thu Jan 28 13:30:59 2021 UTC
  8Thu Jan 28 12:30:59 2021 CET
  9Thu Jan 28 12:30:59 2021
 10Thu Jan 28 12:30:59 2021
 11done
 12end:
 13*/
 14
 15#include <assert.h>
 16#include <stdio.h>
 17#include <stdlib.h>
 18#include <string.h>
 19#include <time.h>
 20
 21#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
 22
 23struct 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};
 30
 31char *faketz;
 32
 33#ifndef __unix__
 34#define putenv(x) (x)
 35/*
 36 * Ok, this is by definition undefined behaviour because
 37 * we are using the name of a library function. The reallity
 38 * is that if you use a static linker then this would
 39 * overload the libc getenv and it will work as expected,
 40 * but if you use a dynamic linker then the internal
 41 * references will be tied to the internal getenv function
 42 * and this would not work. For our main use case that is
 43 * testing scc libc this is good enough, and with the
 44 * fallback to putenv and unsetenv this will work in
 45 * the systems we are interested on
 46 */
 47char *
 48getenv(const char *name)
 49{
 50	if (strcmp(name, "TZ") != 0)
 51		return NULL;
 52	return faketz;
 53}
 54#endif
 55
 56int
 57test(void)
 58{
 59	time_t t;
 60	char buf[70], var[70], *tz;
 61	struct tm *tm;
 62	struct test *tp;
 63
 64	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);
 72
 73		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);
 98
 99		tm = gmtime(&t);
100		strftime(buf, sizeof(buf), "%c %Z", tm);
101		puts(buf);
102
103		tm = localtime(&t);
104		strftime(buf, sizeof(buf), "%c %Z", tm);
105		puts(buf);
106
107		fputs(asctime(tm), stdout);
108		fputs(ctime(&t), stdout);
109	}
110
111	return 0;
112}
113
114int
115main(void)
116{
117#if !defined(__SCC_TZ_TIME__)
118	puts("RESULT: SKIP");
119	return 0;
120#endif
121	puts("testing");
122	test();
123	puts("done");
124	return 0;
125}