glibcextract.py: Add compile_c_snippet
[glibc.git] / time / tst-getdate.c
blob7d4b3f46357125266eb1b357ff06d4a7f7522990
1 /* Test for getdate.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <support/check.h>
25 #include <support/temp_file.h>
26 #include <support/xunistd.h>
27 #include <time.h>
29 static const struct
31 const char *str;
32 const char *tz;
33 struct tm tm;
34 bool time64;
35 } tests [] =
37 {"21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
38 false },
39 {"21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
40 false },
41 {" 21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
42 false },
43 {"21:01:10 1999-1-31 ", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
44 false },
45 {" 21:01:10 1999-1-31 ", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
46 false },
47 {"21:01:10 1999-2-28", "Universal", {10, 1, 21, 28, 1, 99, 0, 0, 0},
48 false },
49 {"16:30:46 2000-2-29", "Universal", {46, 30,16, 29, 1, 100, 0, 0, 0},
50 false },
51 {"01-08-2000 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 100, 0, 0, 0},
52 false },
54 /* 64 bit time_t tests. */
55 {"21:01:10 2038-1-31", "Universal", {10, 1, 21, 31, 0, 138, 0, 0, 0},
56 true },
57 {"22:01:10 2048-5-20", "Universal", {10, 1, 22, 20, 4, 148, 0, 0, 0},
58 true },
59 {"01-08-2038 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 138, 0, 0, 0},
60 true },
61 {"20-03-2050 21:30:08", "Europe/Berlin", {8, 30, 21, 20, 2, 150, 0, 0, 0},
62 true }
65 static const char *
66 report_date_error (void)
68 switch (getdate_err)
70 case 1:
71 return "The environment variable DATEMSK is not defined or null.";
72 case 2:
73 return "The template file denoted by the DATEMSK environment variable "
74 "cannot be opened.";
75 case 3:
76 return "Information about the template file cannot retrieved.";
77 case 4:
78 return "The template file is not a regular file.\n";
79 case 5:
80 return "An I/O error occurred while reading the template file.";
81 case 6:
82 return "Not enough memory available to execute the function.";
83 case 7:
84 return "The template file contains no matching template.";
85 case 8:
86 return "The input date is invalid, but would match a template "
87 "otherwise.";
88 default:
89 return "Unknown error code.";
93 static char *datemsk;
94 static const char datemskstr[] =
95 "%H:%M:%S %F\n"
96 "%d-%m-%Y %T\n";
98 static void
99 do_prepare (int argc, char **argv)
101 int fd = create_temp_file ("tst-chk1.", &datemsk);
102 xwrite (fd, datemskstr, sizeof (datemskstr) - 1);
104 setenv ("DATEMSK", datemsk, 1);
106 #define PREPARE do_prepare
108 static int
109 do_test (void)
111 struct tm *tm;
113 for (int i = 0; i < array_length (tests); ++i)
115 setenv ("TZ", tests[i].tz, 1);
117 tm = getdate (tests[i].str);
118 TEST_COMPARE (getdate_err, 0);
119 if (getdate_err != 0)
121 support_record_failure ();
122 printf ("%s\n", report_date_error ());
124 else
126 TEST_COMPARE (tests[i].tm.tm_mon, tm->tm_mon);
127 TEST_COMPARE (tests[i].tm.tm_year, tm->tm_year);
128 TEST_COMPARE (tests[i].tm.tm_mday, tm->tm_mday);
129 TEST_COMPARE (tests[i].tm.tm_hour, tm->tm_hour);
130 TEST_COMPARE (tests[i].tm.tm_min, tm->tm_min);
131 TEST_COMPARE (tests[i].tm.tm_sec, tm->tm_sec);
134 struct tm tms;
135 TEST_COMPARE (getdate_r (tests[i].str, &tms), 0);
136 if (getdate_err == 0)
138 TEST_COMPARE (tests[i].tm.tm_mon, tms.tm_mon);
139 TEST_COMPARE (tests[i].tm.tm_year, tms.tm_year);
140 TEST_COMPARE (tests[i].tm.tm_mday, tms.tm_mday);
141 TEST_COMPARE (tests[i].tm.tm_hour, tms.tm_hour);
142 TEST_COMPARE (tests[i].tm.tm_min, tms.tm_min);
143 TEST_COMPARE (tests[i].tm.tm_sec, tms.tm_sec);
147 return 0;
150 #include <support/test-driver.c>