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
52 main (int argc, char **argv)
54 struct timespec result;
55 struct timespec result2;
59 set_program_name (argv[0]);
64 ASSERT (get_date (&result, p, &now));
66 ASSERT (now.tv_sec == result.tv_sec && now.tv_nsec == result.tv_nsec);
71 ASSERT (get_date (&result, p, &now));
73 ASSERT (now.tv_sec + 24 * 60 * 60 == result.tv_sec
74 && now.tv_nsec == result.tv_nsec);
79 ASSERT (get_date (&result, p, &now));
81 ASSERT (now.tv_sec - 24 * 60 * 60 == result.tv_sec
82 && now.tv_nsec == result.tv_nsec);
87 ASSERT (get_date (&result, p, &now));
89 ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
90 && now.tv_nsec == result.tv_nsec);
92 /* test if timezone is not being ignored for day offset */
95 p = "UTC+400 +24 hours";
96 ASSERT (get_date (&result, p, &now));
99 ASSERT (get_date (&result2, p, &now));
100 LOG (p, now, result2);
101 ASSERT (result.tv_sec == result2.tv_sec
102 && result.tv_nsec == result2.tv_nsec);
104 /* test if several time zones formats are handled same way */
108 ASSERT (get_date (&result, p, &now));
109 LOG (p, now, result);
111 ASSERT (get_date (&result2, p, &now));
112 LOG (p, now, result2);
113 ASSERT (result.tv_sec == result2.tv_sec
114 && result.tv_nsec == result2.tv_nsec);
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);
124 ASSERT (get_date (&result, p, &now));
125 LOG (p, now, result);
127 ASSERT (get_date (&result2, p, &now));
128 LOG (p, now, result2);
129 ASSERT (result.tv_sec == result2.tv_sec
130 && result.tv_nsec == result2.tv_nsec);
132 ASSERT (get_date (&result2, p, &now));
133 LOG (p, now, result2);
134 ASSERT (result.tv_sec == result2.tv_sec
135 && result.tv_nsec == result2.tv_nsec);
140 ASSERT (get_date (&result, p, &now));
141 LOG (p, now, result);
143 ASSERT (get_date (&result2, p, &now));
144 LOG (p, now, result2);
145 ASSERT (result.tv_sec == result2.tv_sec
146 && result.tv_nsec == result2.tv_nsec);
151 ASSERT (get_date (&result, p, &now));
152 LOG (p, now, result);
154 ASSERT (get_date (&result2, p, &now));
155 LOG (p, now, result2);
156 ASSERT (result.tv_sec == result2.tv_sec
157 && result.tv_nsec == result2.tv_nsec);
160 /* TZ out of range should cause get_date failure */
164 ASSERT (!get_date (&result, p, &now));
166 /* Check for several invalid countable dayshifts */
169 p = "UTC+4:00 +40 yesterday";
170 ASSERT (!get_date (&result, p, &now));
171 p = "UTC+4:00 next yesterday";
172 ASSERT (!get_date (&result, p, &now));
173 p = "UTC+4:00 tomorrow ago";
174 ASSERT (!get_date (&result, p, &now));
175 p = "UTC+4:00 40 now ago";
176 ASSERT (!get_date (&result, p, &now));
177 p = "UTC+4:00 last tomorrow";
178 ASSERT (!get_date (&result, p, &now));
179 p = "UTC+4:00 -4 today";
180 ASSERT (!get_date (&result, p, &now));
182 /* And check correct usage of dayshifts */
185 p = "UTC+400 tomorrow";
186 ASSERT (get_date (&result, p, &now));
187 LOG (p, now, result);
188 p = "UTC+400 +1 day";
189 ASSERT (get_date (&result2, p, &now));
190 LOG (p, now, result2);
191 ASSERT (result.tv_sec == result2.tv_sec
192 && result.tv_nsec == result2.tv_nsec);
195 p = "UTC+400 yesterday";
196 ASSERT (get_date (&result, p, &now));
197 LOG (p, now, result);
198 p = "UTC+400 1 day ago";
199 ASSERT (get_date (&result2, p, &now));
200 LOG (p, now, result2);
201 ASSERT (result.tv_sec == result2.tv_sec
202 && result.tv_nsec == result2.tv_nsec);
206 ASSERT (get_date (&result, p, &now));
207 LOG (p, now, result);
208 p = "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
209 ASSERT (get_date (&result2, p, &now));
210 LOG (p, now, result2);
211 ASSERT (result.tv_sec == result2.tv_sec
212 && result.tv_nsec == result2.tv_nsec);