Blame src/functional/time.c

nsz 73214b
#define _XOPEN_SOURCE 700
nsz 73214b
#include <stdlib.h>
nsz 73214b
#include <stdio.h>
nsz 73214b
#include <time.h>
Szabolcs Nagy cfa23c
#include <string.h>
Szabolcs Nagy cfa23c
#include <errno.h>
Szabolcs Nagy cfa23c
#include <limits.h>
nsz 73214b
#include "test.h"
nsz 73214b
nsz 73214b
/* We use this instead of memcmp because some broken C libraries
nsz 73214b
 * add additional nonstandard fields to struct tm... */
nsz 462b4f
nsz 73214b
int tm_cmp(struct tm tm1, struct tm tm2)
nsz 73214b
{
nsz 73214b
	return  tm1.tm_sec  != tm2.tm_sec  ||
nsz 73214b
		tm1.tm_min  != tm2.tm_min  ||
nsz 73214b
		tm1.tm_hour != tm2.tm_hour ||
nsz 73214b
		tm1.tm_mday != tm2.tm_mday ||
nsz 73214b
		tm1.tm_mon  != tm2.tm_mon  ||
nsz 73214b
		tm1.tm_year != tm2.tm_year ||
nsz 73214b
		tm1.tm_wday != tm2.tm_wday ||
nsz 73214b
		tm1.tm_yday != tm2.tm_yday ||
nsz 73214b
		tm1.tm_isdst!= tm2.tm_isdst;
nsz 73214b
}
nsz 73214b
nsz 73214b
char *tm_str(struct tm tm)
nsz 73214b
{
nsz 73214b
	static int i;
nsz 73214b
	static char b[4][64];
nsz 73214b
	i = (i+1)%4;
nsz 73214b
	snprintf(b[i], sizeof b[i],
nsz 73214b
		"s=%02d m=%02d h=%02d mday=%02d mon=%02d year=%04d wday=%d yday=%d isdst=%d",
nsz 73214b
		tm.tm_sec, tm.tm_min, tm.tm_hour,
nsz 73214b
		tm.tm_mday, tm.tm_mon, tm.tm_year,
nsz 73214b
		tm.tm_wday, tm.tm_yday, tm.tm_isdst);
nsz 73214b
	return b[i];
nsz 73214b
}
nsz 73214b
nsz 73214b
#define TM(ss,mm,hh,md,mo,yr,wd,yd,dst) (struct tm){ \
nsz 73214b
	.tm_sec = ss, .tm_min = mm, .tm_hour = hh,    \
nsz 73214b
	.tm_mday = md, .tm_mon = mo, .tm_year = yr,    \
nsz 73214b
	.tm_wday = wd, .tm_yday = yd, .tm_isdst = dst }
nsz 73214b
nsz 73214b
#define TM_EPOCH    TM(0,0,0,1,0,70,4,0,0)
nsz 73214b
#define TM_Y2038_1S TM(7,14,3,19,0,138,2,18,0)
nsz 73214b
#define TM_Y2038    TM(8,14,3,19,0,138,2,18,0)
nsz 73214b
Szabolcs Nagy cfa23c
static void sec2tm(time_t t, char *m)
Szabolcs Nagy cfa23c
{
Szabolcs Nagy cfa23c
	struct tm *tm;
Szabolcs Nagy cfa23c
	time_t r;
Szabolcs Nagy cfa23c
Szabolcs Nagy cfa23c
	errno = 0;
Szabolcs Nagy cfa23c
	tm = gmtime(&t);
Szabolcs Nagy cfa23c
	if (errno != 0)
Szabolcs Nagy cfa23c
		t_error("%s: gmtime((time_t)%lld) should not set errno, got %s\n",
Szabolcs Nagy cfa23c
			m, (long long)t, strerror(errno));
Szabolcs Nagy cfa23c
	errno = 0;
Szabolcs Nagy cfa23c
	r = mktime(tm);
Szabolcs Nagy cfa23c
	if (errno != 0)
Szabolcs Nagy cfa23c
		t_error("%s: mktime(%s) should not set errno, got %s\n",
Szabolcs Nagy cfa23c
			m, tm_str(*tm), strerror(errno));
Szabolcs Nagy cfa23c
	if (t != r)
Szabolcs Nagy cfa23c
		t_error("%s: mktime(gmtime(%lld)) roundtrip failed: got %lld (gmtime is %s)\n",
Szabolcs Nagy cfa23c
			m, (long long)t, (long long)r, tm_str(*tm));
Szabolcs Nagy cfa23c
}
nsz 73214b
Szabolcs Nagy cfa23c
static void tm2sec(struct tm *tm, int big, char *m)
Szabolcs Nagy cfa23c
{
Szabolcs Nagy cfa23c
	struct tm *r;
Szabolcs Nagy cfa23c
	time_t t;
Szabolcs Nagy cfa23c
	int overflow = big && (time_t)LLONG_MAX!=LLONG_MAX;
Szabolcs Nagy cfa23c
Szabolcs Nagy cfa23c
	errno = 0;
Szabolcs Nagy cfa23c
	t = mktime(tm);
Szabolcs Nagy cfa23c
	if (overflow && t != -1)
Szabolcs Nagy cfa23c
		t_error("%s: mktime(%s) expected -1, got (time_t)%ld\n",
Szabolcs Nagy cfa23c
			m, tm_str(*tm), (long)t);
Szabolcs Nagy cfa23c
	if (overflow && errno != EOVERFLOW)
Szabolcs Nagy cfa23c
		t_error("%s: mktime(%s) expected EOVERFLOW (%s), got (%s)\n",
Szabolcs Nagy cfa23c
			m, tm_str(*tm), strerror(EOVERFLOW), strerror(errno));
Szabolcs Nagy cfa23c
	if (!overflow && t == -1)
Szabolcs Nagy cfa23c
		t_error("%s: mktime(%s) expected success, got (time_t)-1\n",
Szabolcs Nagy cfa23c
			m, tm_str(*tm));
Szabolcs Nagy cfa23c
	if (!overflow && errno)
Szabolcs Nagy cfa23c
		t_error("%s: mktime(%s) expected no error, got (%s)\n",
Szabolcs Nagy cfa23c
			m, tm_str(*tm), strerror(errno));
Szabolcs Nagy cfa23c
	r = gmtime(&t);
Szabolcs Nagy cfa23c
	if (!overflow && tm_cmp(*r, *tm))
Szabolcs Nagy cfa23c
		t_error("%s: gmtime(mktime(%s)) roundtrip failed: got %s\n",
Szabolcs Nagy cfa23c
			m, tm_str(*tm), tm_str(*r));
Szabolcs Nagy cfa23c
}
nsz 73214b
nsz 462b4f
int main(void)
nsz 462b4f
{
nsz 73214b
	time_t t;
nsz 73214b
nsz 73214b
	putenv("TZ=GMT");
nsz 73214b
	tzset();
Szabolcs Nagy cfa23c
	tm2sec(&TM_EPOCH, 0, "gmtime(0)");
Szabolcs Nagy cfa23c
	tm2sec(&TM_Y2038_1S, 0, "2038-1s");
Szabolcs Nagy cfa23c
	tm2sec(&TM_Y2038, 1, "2038");
nsz 73214b
Szabolcs Nagy cfa23c
	sec2tm(0, "EPOCH");
Szabolcs Nagy cfa23c
	for (t = 1; t < 1000; t++)
Szabolcs Nagy cfa23c
		sec2tm(t*100003, "EPOCH+eps");
nsz 73214b
nsz 73214b
	/* FIXME: set a TZ var and check DST boundary conditions */
Szabolcs Nagy cfa23c
	return t_status;
nsz 73214b
}