1 /* Test of getdate() function.
2 Copyright (C) 2008 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. */
28 #define ASSERT(expr) \
33 fprintf (stderr, "%s:%d: assertion failed\n", \
34 __FILE__, __LINE__); \
42 #define LOG(str, now, res) \
43 printf ("string `%s' diff %d %d\n", \
44 str, res.tv_sec - now.tv_sec, res.tv_nsec - now.tv_nsec);
46 #define LOG(str, now, res) 0
50 main (int argc, char **argv)
52 struct timespec result;
53 struct timespec result2;
60 ASSERT (get_date (&result, p, &now));
62 ASSERT (now.tv_sec == result.tv_sec && now.tv_nsec == result.tv_nsec);
67 ASSERT (get_date (&result, p, &now));
69 ASSERT (now.tv_sec + 24 * 60 * 60 == result.tv_sec
70 && now.tv_nsec == result.tv_nsec);
75 ASSERT (get_date (&result, p, &now));
77 ASSERT (now.tv_sec - 24 * 60 * 60 == result.tv_sec
78 && now.tv_nsec == result.tv_nsec);
83 ASSERT (get_date (&result, p, &now));
85 ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
86 && now.tv_nsec == result.tv_nsec);
88 /* test if timezone is not being ignored for day offset */
91 p = "UTC+400 +24 hours";
92 ASSERT (get_date (&result, p, &now));
95 ASSERT (get_date (&result2, p, &now));
96 LOG (p, now, result2);
97 ASSERT (result.tv_sec == result2.tv_sec
98 && result.tv_nsec == result2.tv_nsec);
100 /* test if several time zones formats are handled same way */
104 ASSERT (get_date (&result, p, &now));
105 LOG (p, now, result);
107 ASSERT (get_date (&result2, p, &now));
108 LOG (p, now, result2);
109 ASSERT (result.tv_sec == result2.tv_sec
110 && result.tv_nsec == result2.tv_nsec);
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);
120 ASSERT (get_date (&result, p, &now));
121 LOG (p, now, result);
123 ASSERT (get_date (&result2, p, &now));
124 LOG (p, now, result2);
125 ASSERT (result.tv_sec == result2.tv_sec
126 && result.tv_nsec == result2.tv_nsec);
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);
136 ASSERT (get_date (&result, p, &now));
137 LOG (p, now, result);
139 ASSERT (get_date (&result2, p, &now));
140 LOG (p, now, result2);
141 ASSERT (result.tv_sec == result2.tv_sec
142 && result.tv_nsec == result2.tv_nsec);
147 ASSERT (get_date (&result, p, &now));
148 LOG (p, now, result);
150 ASSERT (get_date (&result2, p, &now));
151 LOG (p, now, result2);
152 ASSERT (result.tv_sec == result2.tv_sec
153 && result.tv_nsec == result2.tv_nsec);
156 /* TZ out of range should cause get_date failure */
160 ASSERT (!get_date (&result, p, &now));
162 /* Check for several invalid countable dayshifts */
165 p = "UTC+4:00 +40 yesterday";
166 ASSERT (!get_date (&result, p, &now));
167 p = "UTC+4:00 next yesterday";
168 ASSERT (!get_date (&result, p, &now));
169 p = "UTC+4:00 tomorrow ago";
170 ASSERT (!get_date (&result, p, &now));
171 p = "UTC+4:00 40 now ago";
172 ASSERT (!get_date (&result, p, &now));
173 p = "UTC+4:00 last tomorrow";
174 ASSERT (!get_date (&result, p, &now));
175 p = "UTC+4:00 -4 today";
176 ASSERT (!get_date (&result, p, &now));
178 /* And check correct usage of dayshifts */
181 p = "UTC+400 tomorrow";
182 ASSERT (get_date (&result, p, &now));
183 LOG (p, now, result);
184 p = "UTC+400 +1 day";
185 ASSERT (get_date (&result2, p, &now));
186 LOG (p, now, result2);
187 ASSERT (result.tv_sec == result2.tv_sec
188 && result.tv_nsec == result2.tv_nsec);
191 p = "UTC+400 yesterday";
192 ASSERT (get_date (&result, p, &now));
193 LOG (p, now, result);
194 p = "UTC+400 1 day ago";
195 ASSERT (get_date (&result2, p, &now));
196 LOG (p, now, result2);
197 ASSERT (result.tv_sec == result2.tv_sec
198 && result.tv_nsec == result2.tv_nsec);
202 ASSERT (get_date (&result, p, &now));
203 LOG (p, now, result);
204 p = "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
205 ASSERT (get_date (&result2, p, &now));
206 LOG (p, now, result2);
207 ASSERT (result.tv_sec == result2.tv_sec
208 && result.tv_nsec == result2.tv_nsec);