1 /* Test of getdate() function.
2 Copyright (C) 2008, 2009, 2010 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. */
32 #define LOG(str, now, res) \
33 printf ("string `%s' diff %d %d\n", \
34 str, res.tv_sec - now.tv_sec, res.tv_nsec - now.tv_nsec);
36 #define LOG(str, now, res) (void) 0
39 static const char* const day_table[] =
52 main (int argc _GL_UNUSED, char **argv)
54 struct timespec result;
55 struct timespec result2;
60 set_program_name (argv[0]);
65 ASSERT (get_date (&result, p, &now));
67 ASSERT (now.tv_sec == result.tv_sec && now.tv_nsec == result.tv_nsec);
72 ASSERT (get_date (&result, p, &now));
74 ASSERT (now.tv_sec + 24 * 60 * 60 == result.tv_sec
75 && now.tv_nsec == result.tv_nsec);
80 ASSERT (get_date (&result, p, &now));
82 ASSERT (now.tv_sec - 24 * 60 * 60 == result.tv_sec
83 && now.tv_nsec == result.tv_nsec);
88 ASSERT (get_date (&result, p, &now));
90 ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
91 && now.tv_nsec == result.tv_nsec);
93 /* test if timezone is not being ignored for day offset */
96 p = "UTC+400 +24 hours";
97 ASSERT (get_date (&result, p, &now));
100 ASSERT (get_date (&result2, p, &now));
101 LOG (p, now, result2);
102 ASSERT (result.tv_sec == result2.tv_sec
103 && result.tv_nsec == result2.tv_nsec);
105 /* test if several time zones formats are handled same way */
109 ASSERT (get_date (&result, p, &now));
110 LOG (p, now, result);
112 ASSERT (get_date (&result2, p, &now));
113 LOG (p, now, result2);
114 ASSERT (result.tv_sec == result2.tv_sec
115 && result.tv_nsec == result2.tv_nsec);
117 ASSERT (get_date (&result2, p, &now));
118 LOG (p, now, result2);
119 ASSERT (result.tv_sec == result2.tv_sec
120 && result.tv_nsec == result2.tv_nsec);
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);
152 ASSERT (get_date (&result, p, &now));
153 LOG (p, now, result);
155 ASSERT (get_date (&result2, p, &now));
156 LOG (p, now, result2);
157 ASSERT (result.tv_sec == result2.tv_sec
158 && result.tv_nsec == result2.tv_nsec);
161 /* TZ out of range should cause get_date failure */
165 ASSERT (!get_date (&result, p, &now));
167 /* Check for several invalid countable dayshifts */
170 p = "UTC+4:00 +40 yesterday";
171 ASSERT (!get_date (&result, p, &now));
172 p = "UTC+4:00 next yesterday";
173 ASSERT (!get_date (&result, p, &now));
174 p = "UTC+4:00 tomorrow ago";
175 ASSERT (!get_date (&result, p, &now));
176 p = "UTC+4:00 40 now ago";
177 ASSERT (!get_date (&result, p, &now));
178 p = "UTC+4:00 last tomorrow";
179 ASSERT (!get_date (&result, p, &now));
180 p = "UTC+4:00 -4 today";
181 ASSERT (!get_date (&result, p, &now));
183 /* And check correct usage of dayshifts */
186 p = "UTC+400 tomorrow";
187 ASSERT (get_date (&result, p, &now));
188 LOG (p, now, result);
189 p = "UTC+400 +1 day";
190 ASSERT (get_date (&result2, p, &now));
191 LOG (p, now, result2);
192 ASSERT (result.tv_sec == result2.tv_sec
193 && result.tv_nsec == result2.tv_nsec);
196 p = "UTC+400 yesterday";
197 ASSERT (get_date (&result, p, &now));
198 LOG (p, now, result);
199 p = "UTC+400 1 day ago";
200 ASSERT (get_date (&result2, p, &now));
201 LOG (p, now, result2);
202 ASSERT (result.tv_sec == result2.tv_sec
203 && result.tv_nsec == result2.tv_nsec);
207 ASSERT (get_date (&result, p, &now));
208 LOG (p, now, result);
209 p = "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
210 ASSERT (get_date (&result2, p, &now));
211 LOG (p, now, result2);
212 ASSERT (result.tv_sec == result2.tv_sec
213 && result.tv_nsec == result2.tv_nsec);
215 /* Check that some "next Monday", "last Wednesday", etc. are correct. */
216 setenv ("TZ", "UTC0", 1);
217 for (i = 0; day_table[i]; i++)
219 unsigned int thur2 = 7 * 24 * 3600; /* 2nd thursday */
221 sprintf (tmp, "NEXT %s", day_table[i]);
222 now.tv_sec = thur2 + 4711;
224 ASSERT (get_date (&result, tmp, &now));
225 LOG (tmp, now, result);
226 ASSERT (result.tv_nsec == 0);
227 ASSERT (result.tv_sec == thur2 + (i == 4 ? 7 : (i + 3) % 7) * 24 * 3600);
229 sprintf (tmp, "LAST %s", day_table[i]);
230 now.tv_sec = thur2 + 4711;
232 ASSERT (get_date (&result, tmp, &now));
233 LOG (tmp, now, result);
234 ASSERT (result.tv_nsec == 0);
235 ASSERT (result.tv_sec == thur2 + ((i + 3) % 7 - 7) * 24 * 3600);
238 p = "THURSDAY UTC+00"; /* The epoch was on Thursday. */
241 ASSERT (get_date (&result, p, &now));
242 LOG (p, now, result);
243 ASSERT (result.tv_sec == now.tv_sec
244 && result.tv_nsec == now.tv_nsec);
249 ASSERT (get_date (&result, p, &now));
250 LOG (p, now, result);
251 ASSERT (result.tv_sec == 24 * 3600
252 && result.tv_nsec == now.tv_nsec);