summaryrefslogtreecommitdiffstats
path: root/tests/xtime.c
blob: 4a7d55c5aa0b62b495f5cb9b66c16943929c9d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

#include "tests.h"
#include "../src/xtime.h"
#include "../src/bfstd.h"
#include "../src/config.h"
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static bool tm_equal(const struct tm *tma, const struct tm *tmb) {
	if (tma->tm_year != tmb->tm_year) {
		return false;
	}
	if (tma->tm_mon != tmb->tm_mon) {
		return false;
	}
	if (tma->tm_mday != tmb->tm_mday) {
		return false;
	}
	if (tma->tm_hour != tmb->tm_hour) {
		return false;
	}
	if (tma->tm_min != tmb->tm_min) {
		return false;
	}
	if (tma->tm_sec != tmb->tm_sec) {
		return false;
	}
	if (tma->tm_wday != tmb->tm_wday) {
		return false;
	}
	if (tma->tm_yday != tmb->tm_yday) {
		return false;
	}
	if (tma->tm_isdst != tmb->tm_isdst) {
		return false;
	}

	return true;
}

static void tm_print(FILE *file, const struct tm *tm) {
	fprintf(file, "Y%d M%d D%d  h%d m%d s%d  wd%d yd%d%s\n",
		tm->tm_year, tm->tm_mon, tm->tm_mday,
		tm->tm_hour, tm->tm_min, tm->tm_sec,
		tm->tm_wday, tm->tm_yday,
		tm->tm_isdst ? (tm->tm_isdst < 0 ? " (DST?)" : " (DST)") : "");
}

/** Check one xgetdate() result. */
static bool compare_xgetdate(const char *str, int error, time_t expected) {
	struct timespec ts;
	int ret = xgetdate(str, &ts);

	if (error) {
		if (ret != -1 || errno != error) {
			fprintf(stderr, "xgetdate('%s'): %s\n", str, xstrerror(errno));
			return false;
		}
	} else if (ret != 0) {
		fprintf(stderr, "xgetdate('%s'): %s\n", str, xstrerror(errno));
		return false;
	} else if (ts.tv_sec != expected || ts.tv_nsec != 0) {
		fprintf(stderr, "xgetdate('%s'): %jd.%09jd != %jd\n", str, (intmax_t)ts.tv_sec, (intmax_t)ts.tv_nsec, (intmax_t)expected);
		return false;
	}

	return true;
}

/** xgetdate() tests. */
static bool check_xgetdate(void) {
	return compare_xgetdate("", EINVAL, 0)
		& compare_xgetdate("????", EINVAL, 0)
		& compare_xgetdate("1991", EINVAL, 0)
		& compare_xgetdate("1991-??", EINVAL, 0)
		& compare_xgetdate("1991-12", EINVAL, 0)
		& compare_xgetdate("1991-12-", EINVAL, 0)
		& compare_xgetdate("1991-12-??", EINVAL, 0)
		& compare_xgetdate("1991-12-14", 0, 692668800)
		& compare_xgetdate("1991-12-14-", EINVAL, 0)
		& compare_xgetdate("1991-12-14T", EINVAL, 0)
		& compare_xgetdate("1991-12-14T??", EINVAL, 0)
		& compare_xgetdate("1991-12-14T10", 0, 692704800)
		& compare_xgetdate("1991-12-14T10:??", EINVAL, 0)
		& compare_xgetdate("1991-12-14T10:11", 0, 692705460)
		& compare_xgetdate("1991-12-14T10:11:??", EINVAL, 0)
		& compare_xgetdate("1991-12-14T10:11:12", 0, 692705472)
		& compare_xgetdate("1991-12-14T10Z", 0, 692704800)
		& compare_xgetdate("1991-12-14T10:11Z", 0, 692705460)
		& compare_xgetdate("1991-12-14T10:11:12Z", 0, 692705472)
		& compare_xgetdate("1991-12-14T10:11:12?", EINVAL, 0)
		& compare_xgetdate("1991-12-14T03-07", 0, 692704800)
		& compare_xgetdate("1991-12-14T06:41-03:30", 0, 692705460)
		& compare_xgetdate("1991-12-14T03:11:12-07:00", 0, 692705472)
		& compare_xgetdate("19911214 031112-0700", 0, 692705472);
}

/** xmktime() tests. */
static bool check_xmktime(void) {
	for (time_t time = -10; time <= 10; ++time) {
		struct tm tm;
		if (xlocaltime(&time, &tm) != 0) {
			perror("xlocaltime()");
			return false;
		}

		time_t made;
		if (xmktime(&tm, &made) != 0) {
			perror("xmktime()");
			return false;
		}

		if (time != made) {
			fprintf(stderr, "Mismatch: %jd != %jd\n", (intmax_t)time, (intmax_t)made);
			tm_print(stderr, &tm);
			return false;
		}
	}

	return true;
}

/** xtimegm() tests. */
static bool check_xtimegm(void) {
	struct tm tm = {
		.tm_isdst = -1,
	};

	for (tm.tm_year =  10; tm.tm_year <= 200; tm.tm_year += 10)
	for (tm.tm_mon  =  -3; tm.tm_mon  <=  15; tm.tm_mon  +=  3)
	for (tm.tm_mday = -31; tm.tm_mday <=  61; tm.tm_mday +=  4)
	for (tm.tm_hour =  -1; tm.tm_hour <=  24; tm.tm_hour +=  5)
	for (tm.tm_min  =  -1; tm.tm_min  <=  60; tm.tm_min  += 31)
	for (tm.tm_sec  = -60; tm.tm_sec  <= 120; tm.tm_sec  +=  5) {
		struct tm tma = tm, tmb = tm;
		time_t ta, tb;
		ta = mktime(&tma);
		if (xtimegm(&tmb, &tb) != 0) {
			tb = -1;
		}

		bool fail = false;
		if (ta != tb) {
			printf("Mismatch:  %jd != %jd\n", (intmax_t)ta, (intmax_t)tb);
			fail = true;
		}
		if (ta != -1 && !tm_equal(&tma, &tmb)) {
			printf("mktime():  ");
			tm_print(stdout, &tma);
			printf("xtimegm(): ");
			tm_print(stdout, &tmb);
			fail = true;
		}
		if (fail) {
			printf("Input:     ");
			tm_print(stdout, &tm);
			return false;
		}
	}

	return true;
}

bool check_xtime(void) {
	if (setenv("TZ", "UTC0", true) != 0) {
		perror("setenv()");
		return false;
	}
	tzset();

	return check_xgetdate()
		& check_xmktime()
		& check_xtimegm();
}