]> Sergey Matveev's repositories - bfs.git/blob - tests/xtimegm.c
Formatting fixes
[bfs.git] / tests / xtimegm.c
1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
3
4 #include "../src/xtime.h"
5 #include "../src/config.h"
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <time.h>
10
11 static bool tm_equal(const struct tm *tma, const struct tm *tmb) {
12         if (tma->tm_year != tmb->tm_year) {
13                 return false;
14         }
15         if (tma->tm_mon != tmb->tm_mon) {
16                 return false;
17         }
18         if (tma->tm_mday != tmb->tm_mday) {
19                 return false;
20         }
21         if (tma->tm_hour != tmb->tm_hour) {
22                 return false;
23         }
24         if (tma->tm_min != tmb->tm_min) {
25                 return false;
26         }
27         if (tma->tm_sec != tmb->tm_sec) {
28                 return false;
29         }
30         if (tma->tm_wday != tmb->tm_wday) {
31                 return false;
32         }
33         if (tma->tm_yday != tmb->tm_yday) {
34                 return false;
35         }
36         if (tma->tm_isdst != tmb->tm_isdst) {
37                 return false;
38         }
39
40         return true;
41 }
42
43 static void tm_print(FILE *file, const struct tm *tm) {
44         fprintf(file, "Y%d M%d D%d  h%d m%d s%d  wd%d yd%d%s\n",
45                 tm->tm_year, tm->tm_mon, tm->tm_mday,
46                 tm->tm_hour, tm->tm_min, tm->tm_sec,
47                 tm->tm_wday, tm->tm_yday,
48                 tm->tm_isdst ? (tm->tm_isdst < 0 ? " (DST?)" : " (DST)") : "");
49 }
50
51 int main(void) {
52         if (setenv("TZ", "UTC0", true) != 0) {
53                 perror("setenv()");
54                 return EXIT_FAILURE;
55         }
56
57         struct tm tm = {
58                 .tm_isdst = -1,
59         };
60
61         for (tm.tm_year = 10; tm.tm_year <= 200; tm.tm_year += 10)
62         for (tm.tm_mon = -3; tm.tm_mon <= 15; tm.tm_mon += 3)
63         for (tm.tm_mday = -31; tm.tm_mday <= 61; tm.tm_mday += 4)
64         for (tm.tm_hour = -1; tm.tm_hour <= 24; tm.tm_hour += 5)
65         for (tm.tm_min = -1; tm.tm_min <= 60; tm.tm_min += 31)
66         for (tm.tm_sec = -60; tm.tm_sec <= 120; tm.tm_sec += 5) {
67                 struct tm tma = tm, tmb = tm;
68                 time_t ta, tb;
69                 ta = mktime(&tma);
70                 if (xtimegm(&tmb, &tb) != 0) {
71                         tb = -1;
72                 }
73
74                 bool fail = false;
75                 if (ta != tb) {
76                         printf("Mismatch:  %jd != %jd\n", (intmax_t)ta, (intmax_t)tb);
77                         fail = true;
78                 }
79                 if (ta != -1 && !tm_equal(&tma, &tmb)) {
80                         printf("mktime():  ");
81                         tm_print(stdout, &tma);
82                         printf("xtimegm(): ");
83                         tm_print(stdout, &tmb);
84                         fail = true;
85                 }
86                 if (fail) {
87                         printf("Input:     ");
88                         tm_print(stdout, &tm);
89                         return EXIT_FAILURE;
90                 }
91         }
92
93         return EXIT_SUCCESS;
94 }