1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright © 2021 Keith Packard
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #define _DEFAULT_SOURCE
37 #include <time.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40
41 #define NUM_TEST 1024
42
43 const struct _test {
44 struct tm tm;
45 time_t time;
46 } tests[NUM_TEST] = {
47 #ifndef GENERATE
48 #include "timegm.h"
49 #endif
50 };
51
main(void)52 int main(void)
53 {
54 unsigned i;
55 int ret = 0;
56
57 #ifdef GENERATE
58 for (i = 0; i < NUM_TEST; i++) {
59 tests[i].time = random();
60 gmtime_r(&tests[i].time, &tests[i].tm);
61 printf (" {\n");
62 printf (" .time = %ld,\n", tests[i].time);
63 printf (" .tm = {\n");
64 printf (" .tm_sec = %d,\n", tests[i].tm.tm_sec); /* Seconds (0-60) */
65 printf (" .tm_min = %d,\n", tests[i].tm.tm_min); /* Minutes (0-59) */
66 printf (" .tm_hour = %d,\n", tests[i].tm.tm_hour); /* Hours (0-23) */
67 printf (" .tm_mday = %d,\n", tests[i].tm.tm_mday); /* Day of the month (1-31) */
68 printf (" .tm_mon = %d,\n", tests[i].tm.tm_mon); /* Month (0-11) */
69 printf (" .tm_year = %d,\n", tests[i].tm.tm_year); /* Year - 1900 */
70 printf (" .tm_wday = %d,\n", tests[i].tm.tm_wday); /* Day of the week (0-6, Sunday = 0) */
71 printf (" .tm_yday = %d,\n", tests[i].tm.tm_yday); /* Day in the year (0-365, 1 Jan = 0) */
72 printf (" .tm_isdst = %d,\n", tests[i].tm.tm_isdst); /* Daylight saving time */
73 printf (" },\n");
74 printf (" },\n");
75 }
76 #endif
77 for (i = 0; i < NUM_TEST; i++) {
78 struct tm *ptm;
79 time_t time;
80
81 /* gmtime */
82 time = tests[i].time;
83 ptm = gmtime(&time);
84 if (ptm->tm_sec != tests[i].tm.tm_sec) {
85 printf ("tm_sec: got %d want %d,\n", ptm->tm_sec, tests[i].tm.tm_sec); ret++;
86 }
87 if (ptm->tm_min != tests[i].tm.tm_min) {
88 printf ("tm_min: got %d want %d,\n", ptm->tm_min, tests[i].tm.tm_min);
89 ret++;
90 }
91 if (ptm->tm_hour != tests[i].tm.tm_hour) {
92 printf ("tm_hour: got %d want %d,\n", ptm->tm_hour, tests[i].tm.tm_hour);
93 ret++;
94 }
95 if (ptm->tm_mday != tests[i].tm.tm_mday) {
96 printf ("tm_mday: got %d want %d,\n", ptm->tm_mday, tests[i].tm.tm_mday);
97 ret++;
98 }
99 if (ptm->tm_mon != tests[i].tm.tm_mon) {
100 printf ("tm_mon: got %d want %d,\n", ptm->tm_mon, tests[i].tm.tm_mon);
101 ret++;
102 }
103 if (ptm->tm_year != tests[i].tm.tm_year) {
104 printf ("tm_year: got %d want %d,\n", ptm->tm_year, tests[i].tm.tm_year);
105 ret++;
106 }
107 if (ptm->tm_wday != tests[i].tm.tm_wday) {
108 printf ("tm_wday: got %d want %d,\n", ptm->tm_wday, tests[i].tm.tm_wday);
109 ret++;
110 }
111 if (ptm->tm_yday != tests[i].tm.tm_yday) {
112 printf ("tm_yday: got %d want %d,\n", ptm->tm_yday, tests[i].tm.tm_yday);
113 ret++;
114 }
115 if (ptm->tm_isdst != tests[i].tm.tm_isdst) {
116 printf ("tm_isdst: got %d want %d,\n", ptm->tm_isdst, tests[i].tm.tm_isdst);
117 ret++;
118 }
119
120 struct tm tmp = tests[i].tm;
121
122 /* timegm */
123 time = timegm(&tmp);
124 if (time != tests[i].time) {
125 printf("time: got %ld want %ld\n", (long) time, (long) tests[i].time);
126 ret++;
127 }
128 }
129 return ret;
130 }
131