Set cut_dir properly, and add mode line for Emacs.
[gnulib.git] / tests / test-getdate.c
1 /* Test of getdate() function.
2    Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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.  */
17
18 /* Written by Simon Josefsson <simon@josefsson.org>, 2008.  */
19
20 #include <config.h>
21
22 #include "getdate.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "progname.h"
29 #include "macros.h"
30
31 #ifdef DEBUG
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);
35 #else
36 #define LOG(str, now, res) (void) 0
37 #endif
38
39 static const char* const day_table[] =
40 {
41   "SUNDAY",
42   "MONDAY",
43   "TUESDAY",
44   "WEDNESDAY",
45   "THURSDAY",
46   "FRIDAY",
47   "SATURDAY",
48   NULL
49 };
50
51 int
52 main (int argc _GL_UNUSED, char **argv)
53 {
54   struct timespec result;
55   struct timespec result2;
56   struct timespec now;
57   const char *p;
58   int i;
59
60   set_program_name (argv[0]);
61
62   now.tv_sec = 4711;
63   now.tv_nsec = 1267;
64   p = "now";
65   ASSERT (get_date (&result, p, &now));
66   LOG (p, now, result);
67   ASSERT (now.tv_sec == result.tv_sec && now.tv_nsec == result.tv_nsec);
68
69   now.tv_sec = 4711;
70   now.tv_nsec = 1267;
71   p = "tomorrow";
72   ASSERT (get_date (&result, p, &now));
73   LOG (p, now, result);
74   ASSERT (now.tv_sec + 24 * 60 * 60 == result.tv_sec
75           && now.tv_nsec == result.tv_nsec);
76
77   now.tv_sec = 4711;
78   now.tv_nsec = 1267;
79   p = "yesterday";
80   ASSERT (get_date (&result, p, &now));
81   LOG (p, now, result);
82   ASSERT (now.tv_sec - 24 * 60 * 60 == result.tv_sec
83           && now.tv_nsec == result.tv_nsec);
84
85   now.tv_sec = 4711;
86   now.tv_nsec = 1267;
87   p = "4 hours";
88   ASSERT (get_date (&result, p, &now));
89   LOG (p, now, result);
90   ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
91           && now.tv_nsec == result.tv_nsec);
92
93   /* test if timezone is not being ignored for day offset */
94   now.tv_sec = 4711;
95   now.tv_nsec = 1267;
96   p = "UTC+400 +24 hours";
97   ASSERT (get_date (&result, p, &now));
98   LOG (p, now, result);
99   p = "UTC+400 +1 day";
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);
104
105   /* test if several time zones formats are handled same way */
106   now.tv_sec = 4711;
107   now.tv_nsec = 1267;
108   p = "UTC+14:00";
109   ASSERT (get_date (&result, p, &now));
110   LOG (p, now, result);
111   p = "UTC+14";
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);
116   p = "UTC+1400";
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);
121
122   now.tv_sec = 4711;
123   now.tv_nsec = 1267;
124   p = "UTC-14:00";
125   ASSERT (get_date (&result, p, &now));
126   LOG (p, now, result);
127   p = "UTC-14";
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);
132   p = "UTC-1400";
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);
137
138   now.tv_sec = 4711;
139   now.tv_nsec = 1267;
140   p = "UTC+0:15";
141   ASSERT (get_date (&result, p, &now));
142   LOG (p, now, result);
143   p = "UTC+0015";
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);
148
149   now.tv_sec = 4711;
150   now.tv_nsec = 1267;
151   p = "UTC-1:30";
152   ASSERT (get_date (&result, p, &now));
153   LOG (p, now, result);
154   p = "UTC-130";
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);
159
160
161   /* TZ out of range should cause get_date failure */
162   now.tv_sec = 4711;
163   now.tv_nsec = 1267;
164   p = "UTC+25:00";
165   ASSERT (!get_date (&result, p, &now));
166
167         /* Check for several invalid countable dayshifts */
168   now.tv_sec = 4711;
169   now.tv_nsec = 1267;
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));
182
183   /* And check correct usage of dayshifts */
184   now.tv_sec = 4711;
185   now.tv_nsec = 1267;
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);
194   now.tv_sec = 4711;
195   now.tv_nsec = 1267;
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);
204   now.tv_sec = 4711;
205   now.tv_nsec = 1267;
206   p = "UTC+400 now";
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);
214
215   /* Check that some "next Monday", "last Wednesday", etc. are correct.  */
216   setenv ("TZ", "UTC0", 1);
217   for (i = 0; day_table[i]; i++)
218     {
219       unsigned int thur2 = 7 * 24 * 3600; /* 2nd thursday */
220       char tmp[32];
221       sprintf (tmp, "NEXT %s", day_table[i]);
222       now.tv_sec = thur2 + 4711;
223       now.tv_nsec = 1267;
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);
228
229       sprintf (tmp, "LAST %s", day_table[i]);
230       now.tv_sec = thur2 + 4711;
231       now.tv_nsec = 1267;
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);
236     }
237
238   p = "THURSDAY UTC+00";  /* The epoch was on Thursday.  */
239   now.tv_sec = 0;
240   now.tv_nsec = 0;
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);
245
246   p = "FRIDAY UTC+00";
247   now.tv_sec = 0;
248   now.tv_nsec = 0;
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);
253
254   return 0;
255 }