1 /* Test of getdate() function.
2 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", \
36 __FILE__, __LINE__); \
44 #define LOG(str, now, res) \
45 printf ("string `%s' diff %d %d\n", \
46 str, res.tv_sec - now.tv_sec, res.tv_nsec - now.tv_nsec);
48 #define LOG(str, now, res) (void) 0
51 static const char* const day_table[] =
68 main (int argc, char **argv)
70 struct timespec result;
71 struct timespec result2;
76 set_program_name (argv[0]);
81 ASSERT (get_date (&result, p, &now));
83 ASSERT (now.tv_sec == result.tv_sec && now.tv_nsec == result.tv_nsec);
88 ASSERT (get_date (&result, p, &now));
90 ASSERT (now.tv_sec + 24 * 60 * 60 == result.tv_sec
91 && now.tv_nsec == result.tv_nsec);
96 ASSERT (get_date (&result, p, &now));
98 ASSERT (now.tv_sec - 24 * 60 * 60 == result.tv_sec
99 && now.tv_nsec == result.tv_nsec);
104 ASSERT (get_date (&result, p, &now));
105 LOG (p, now, result);
106 ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
107 && now.tv_nsec == result.tv_nsec);
109 /* test if timezone is not being ignored for day offset */
112 p = "UTC+400 +24 hours";
113 ASSERT (get_date (&result, p, &now));
114 LOG (p, now, result);
115 p = "UTC+400 +1 day";
116 ASSERT (get_date (&result2, p, &now));
117 LOG (p, now, result2);
118 ASSERT (result.tv_sec == result2.tv_sec
119 && result.tv_nsec == result2.tv_nsec);
121 /* test if several time zones formats are handled same way */
125 ASSERT (get_date (&result, p, &now));
126 LOG (p, now, result);
128 ASSERT (get_date (&result2, p, &now));
129 LOG (p, now, result2);
130 ASSERT (result.tv_sec == result2.tv_sec
131 && result.tv_nsec == result2.tv_nsec);
133 ASSERT (get_date (&result2, p, &now));
134 LOG (p, now, result2);
135 ASSERT (result.tv_sec == result2.tv_sec
136 && result.tv_nsec == result2.tv_nsec);
141 ASSERT (get_date (&result, p, &now));
142 LOG (p, now, result);
144 ASSERT (get_date (&result2, p, &now));
145 LOG (p, now, result2);
146 ASSERT (result.tv_sec == result2.tv_sec
147 && result.tv_nsec == result2.tv_nsec);
149 ASSERT (get_date (&result2, p, &now));
150 LOG (p, now, result2);
151 ASSERT (result.tv_sec == result2.tv_sec
152 && result.tv_nsec == result2.tv_nsec);
157 ASSERT (get_date (&result, p, &now));
158 LOG (p, now, result);
160 ASSERT (get_date (&result2, p, &now));
161 LOG (p, now, result2);
162 ASSERT (result.tv_sec == result2.tv_sec
163 && result.tv_nsec == result2.tv_nsec);
168 ASSERT (get_date (&result, p, &now));
169 LOG (p, now, result);
171 ASSERT (get_date (&result2, p, &now));
172 LOG (p, now, result2);
173 ASSERT (result.tv_sec == result2.tv_sec
174 && result.tv_nsec == result2.tv_nsec);
177 /* TZ out of range should cause get_date failure */
181 ASSERT (!get_date (&result, p, &now));
183 /* Check for several invalid countable dayshifts */
186 p = "UTC+4:00 +40 yesterday";
187 ASSERT (!get_date (&result, p, &now));
188 p = "UTC+4:00 next yesterday";
189 ASSERT (!get_date (&result, p, &now));
190 p = "UTC+4:00 tomorrow ago";
191 ASSERT (!get_date (&result, p, &now));
192 p = "UTC+4:00 40 now ago";
193 ASSERT (!get_date (&result, p, &now));
194 p = "UTC+4:00 last tomorrow";
195 ASSERT (!get_date (&result, p, &now));
196 p = "UTC+4:00 -4 today";
197 ASSERT (!get_date (&result, p, &now));
199 /* And check correct usage of dayshifts */
202 p = "UTC+400 tomorrow";
203 ASSERT (get_date (&result, p, &now));
204 LOG (p, now, result);
205 p = "UTC+400 +1 day";
206 ASSERT (get_date (&result2, p, &now));
207 LOG (p, now, result2);
208 ASSERT (result.tv_sec == result2.tv_sec
209 && result.tv_nsec == result2.tv_nsec);
212 p = "UTC+400 yesterday";
213 ASSERT (get_date (&result, p, &now));
214 LOG (p, now, result);
215 p = "UTC+400 1 day ago";
216 ASSERT (get_date (&result2, p, &now));
217 LOG (p, now, result2);
218 ASSERT (result.tv_sec == result2.tv_sec
219 && result.tv_nsec == result2.tv_nsec);
223 ASSERT (get_date (&result, p, &now));
224 LOG (p, now, result);
225 p = "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
226 ASSERT (get_date (&result2, p, &now));
227 LOG (p, now, result2);
228 ASSERT (result.tv_sec == result2.tv_sec
229 && result.tv_nsec == result2.tv_nsec);
231 /* Check that every 'last/next DAY' is in the past/future. */
232 for (i = 0; day_table[i]; i++)
235 sprintf (tmp, "NEXT %s", day_table[i]);
238 ASSERT (get_date (&result, tmp, &now));
239 LOG (tmp, now, result);
240 ASSERT (result.tv_sec > now.tv_sec
241 && result.tv_nsec == 0);
243 sprintf (tmp, "LAST %s", day_table[i]);
246 ASSERT (get_date (&result, tmp, &now));
247 LOG (tmp, now, result);
248 ASSERT (result.tv_sec < now.tv_sec
249 && result.tv_nsec == 0);
252 p = "THURSDAY UTC+00"; /* The epoch was on Thursday. */
255 ASSERT (get_date (&result, p, &now));
256 LOG (p, now, result);
257 ASSERT (result.tv_sec == now.tv_sec
258 && result.tv_nsec == now.tv_nsec);
263 ASSERT (get_date (&result, p, &now));
264 LOG (p, now, result);
265 ASSERT (result.tv_sec >= now.tv_sec
266 && result.tv_nsec == now.tv_nsec);