1 /* Test of parse_datetime() function.
2 Copyright (C) 2008-2021 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, see <https://www.gnu.org/licenses/>. */
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
21 #include "parse-datetime.h"
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
[] =
53 /* Shift A right by B bits portably, by dividing A by 2**B and
54 truncating towards minus infinity. A and B should be free of side
55 effects, and B should be in the range 0 <= B <= INT_BITS - 2, where
56 INT_BITS is the number of useful bits in an int. GNU code can
57 assume that INT_BITS is at least 32.
59 ISO C99 says that A >> B is implementation-defined if A < 0. Some
60 implementations (e.g., UNICOS 9.0 on a Cray Y-MP EL) don't shift
61 right in the usual way when A < 0, so SHR falls back on division if
62 ordinary A >> B doesn't seem to be the usual signed shift. */
66 : (a) / (1 << (b)) - ((a) % (1 << (b)) < 0))
68 #define TM_YEAR_BASE 1900
70 /* Yield the difference between *A and *B,
71 measured in seconds, ignoring leap seconds.
72 The body of this function is taken directly from the GNU C Library;
73 see src/strftime.c. */
75 tm_diff (struct tm
const *a
, struct tm
const *b
)
77 /* Compute intervening leap days correctly even if year is negative.
78 Take care to avoid int overflow in leap day calculations. */
79 int a4
= SHR (a
->tm_year
, 2) + SHR (TM_YEAR_BASE
, 2) - ! (a
->tm_year
& 3);
80 int b4
= SHR (b
->tm_year
, 2) + SHR (TM_YEAR_BASE
, 2) - ! (b
->tm_year
& 3);
81 int a100
= a4
/ 25 - (a4
% 25 < 0);
82 int b100
= b4
/ 25 - (b4
% 25 < 0);
83 int a400
= SHR (a100
, 2);
84 int b400
= SHR (b100
, 2);
85 int intervening_leap_days
= (a4
- b4
) - (a100
- b100
) + (a400
- b400
);
86 long int ayear
= a
->tm_year
;
87 long int years
= ayear
- b
->tm_year
;
88 long int days
= (365 * years
+ intervening_leap_days
89 + (a
->tm_yday
- b
->tm_yday
));
90 return (60 * (60 * (24 * days
+ (a
->tm_hour
- b
->tm_hour
))
91 + (a
->tm_min
- b
->tm_min
))
92 + (a
->tm_sec
- b
->tm_sec
));
94 #endif /* ! HAVE_TM_GMTOFF */
102 struct tm tm_local
= *localtime (&s
);
103 struct tm tm_gmt
= *gmtime (&s
);
105 gmtoff
= tm_diff (&tm_local
, &tm_gmt
);
107 gmtoff
= localtime (&s
)->tm_gmtoff
;
114 main (int argc _GL_UNUSED
, char **argv
)
116 struct timespec result
;
117 struct timespec result2
;
118 struct timespec expected
;
123 time_t ref_time
= 1304250918;
125 /* Set the time zone to US Eastern time with the 2012 rules. This
126 should disable any leap second support. Otherwise, there will be
127 a problem with glibc on sites that default to leap seconds; see
128 <https://bugs.gnu.org/12206>. */
129 setenv ("TZ", "EST5EDT,M3.2.0,M11.1.0", 1);
131 gmtoff
= gmt_offset (ref_time
);
134 /* ISO 8601 extended date and time of day representation,
135 'T' separator, local time zone */
136 p
= "2011-05-01T11:55:18";
137 expected
.tv_sec
= ref_time
- gmtoff
;
138 expected
.tv_nsec
= 0;
139 ASSERT (parse_datetime (&result
, p
, 0));
140 LOG (p
, expected
, result
);
141 ASSERT (expected
.tv_sec
== result
.tv_sec
142 && expected
.tv_nsec
== result
.tv_nsec
);
144 /* ISO 8601 extended date and time of day representation,
145 ' ' separator, local time zone */
146 p
= "2011-05-01 11:55:18";
147 expected
.tv_sec
= ref_time
- gmtoff
;
148 expected
.tv_nsec
= 0;
149 ASSERT (parse_datetime (&result
, p
, 0));
150 LOG (p
, expected
, result
);
151 ASSERT (expected
.tv_sec
== result
.tv_sec
152 && expected
.tv_nsec
== result
.tv_nsec
);
155 /* ISO 8601, extended date and time of day representation,
156 'T' separator, UTC */
157 p
= "2011-05-01T11:55:18Z";
158 expected
.tv_sec
= ref_time
;
159 expected
.tv_nsec
= 0;
160 ASSERT (parse_datetime (&result
, p
, 0));
161 LOG (p
, expected
, result
);
162 ASSERT (expected
.tv_sec
== result
.tv_sec
163 && expected
.tv_nsec
== result
.tv_nsec
);
165 /* ISO 8601, extended date and time of day representation,
166 ' ' separator, UTC */
167 p
= "2011-05-01 11:55:18Z";
168 expected
.tv_sec
= ref_time
;
169 expected
.tv_nsec
= 0;
170 ASSERT (parse_datetime (&result
, p
, 0));
171 LOG (p
, expected
, result
);
172 ASSERT (expected
.tv_sec
== result
.tv_sec
173 && expected
.tv_nsec
== result
.tv_nsec
);
176 /* ISO 8601 extended date and time of day representation,
177 'T' separator, w/UTC offset */
178 p
= "2011-05-01T11:55:18-07:00";
179 expected
.tv_sec
= 1304276118;
180 expected
.tv_nsec
= 0;
181 ASSERT (parse_datetime (&result
, p
, 0));
182 LOG (p
, expected
, result
);
183 ASSERT (expected
.tv_sec
== result
.tv_sec
184 && expected
.tv_nsec
== result
.tv_nsec
);
186 /* ISO 8601 extended date and time of day representation,
187 ' ' separator, w/UTC offset */
188 p
= "2011-05-01 11:55:18-07:00";
189 expected
.tv_sec
= 1304276118;
190 expected
.tv_nsec
= 0;
191 ASSERT (parse_datetime (&result
, p
, 0));
192 LOG (p
, expected
, result
);
193 ASSERT (expected
.tv_sec
== result
.tv_sec
194 && expected
.tv_nsec
== result
.tv_nsec
);
197 /* ISO 8601 extended date and time of day representation,
198 'T' separator, w/hour only UTC offset */
199 p
= "2011-05-01T11:55:18-07";
200 expected
.tv_sec
= 1304276118;
201 expected
.tv_nsec
= 0;
202 ASSERT (parse_datetime (&result
, p
, 0));
203 LOG (p
, expected
, result
);
204 ASSERT (expected
.tv_sec
== result
.tv_sec
205 && expected
.tv_nsec
== result
.tv_nsec
);
207 /* ISO 8601 extended date and time of day representation,
208 ' ' separator, w/hour only UTC offset */
209 p
= "2011-05-01 11:55:18-07";
210 expected
.tv_sec
= 1304276118;
211 expected
.tv_nsec
= 0;
212 ASSERT (parse_datetime (&result
, p
, 0));
213 LOG (p
, expected
, result
);
214 ASSERT (expected
.tv_sec
== result
.tv_sec
215 && expected
.tv_nsec
== result
.tv_nsec
);
221 ASSERT (parse_datetime (&result
, p
, &now
));
222 LOG (p
, now
, result
);
223 ASSERT (now
.tv_sec
== result
.tv_sec
&& now
.tv_nsec
== result
.tv_nsec
);
228 ASSERT (parse_datetime (&result
, p
, &now
));
229 LOG (p
, now
, result
);
230 ASSERT (now
.tv_sec
+ 24 * 60 * 60 == result
.tv_sec
231 && now
.tv_nsec
== result
.tv_nsec
);
236 ASSERT (parse_datetime (&result
, p
, &now
));
237 LOG (p
, now
, result
);
238 ASSERT (now
.tv_sec
- 24 * 60 * 60 == result
.tv_sec
239 && now
.tv_nsec
== result
.tv_nsec
);
244 ASSERT (parse_datetime (&result
, p
, &now
));
245 LOG (p
, now
, result
);
246 ASSERT (now
.tv_sec
+ 4 * 60 * 60 == result
.tv_sec
247 && now
.tv_nsec
== result
.tv_nsec
);
249 /* test if timezone is not being ignored for day offset */
252 p
= "UTC+400 +24 hours";
253 ASSERT (parse_datetime (&result
, p
, &now
));
254 LOG (p
, now
, result
);
255 p
= "UTC+400 +1 day";
256 ASSERT (parse_datetime (&result2
, p
, &now
));
257 LOG (p
, now
, result2
);
258 ASSERT (result
.tv_sec
== result2
.tv_sec
259 && result
.tv_nsec
== result2
.tv_nsec
);
261 /* test if several time zones formats are handled same way */
265 ASSERT (parse_datetime (&result
, p
, &now
));
266 LOG (p
, now
, result
);
268 ASSERT (parse_datetime (&result2
, p
, &now
));
269 LOG (p
, now
, result2
);
270 ASSERT (result
.tv_sec
== result2
.tv_sec
271 && result
.tv_nsec
== result2
.tv_nsec
);
273 ASSERT (parse_datetime (&result2
, p
, &now
));
274 LOG (p
, now
, result2
);
275 ASSERT (result
.tv_sec
== result2
.tv_sec
276 && result
.tv_nsec
== result2
.tv_nsec
);
281 ASSERT (parse_datetime (&result
, p
, &now
));
282 LOG (p
, now
, result
);
284 ASSERT (parse_datetime (&result2
, p
, &now
));
285 LOG (p
, now
, result2
);
286 ASSERT (result
.tv_sec
== result2
.tv_sec
287 && result
.tv_nsec
== result2
.tv_nsec
);
289 ASSERT (parse_datetime (&result2
, p
, &now
));
290 LOG (p
, now
, result2
);
291 ASSERT (result
.tv_sec
== result2
.tv_sec
292 && result
.tv_nsec
== result2
.tv_nsec
);
297 ASSERT (parse_datetime (&result
, p
, &now
));
298 LOG (p
, now
, result
);
300 ASSERT (parse_datetime (&result2
, p
, &now
));
301 LOG (p
, now
, result2
);
302 ASSERT (result
.tv_sec
== result2
.tv_sec
303 && result
.tv_nsec
== result2
.tv_nsec
);
308 ASSERT (parse_datetime (&result
, p
, &now
));
309 LOG (p
, now
, result
);
311 ASSERT (parse_datetime (&result2
, p
, &now
));
312 LOG (p
, now
, result2
);
313 ASSERT (result
.tv_sec
== result2
.tv_sec
314 && result
.tv_nsec
== result2
.tv_nsec
);
317 /* TZ out of range should cause parse_datetime failure */
321 ASSERT (!parse_datetime (&result
, p
, &now
));
323 /* Check for several invalid countable dayshifts */
326 p
= "UTC+4:00 +40 yesterday";
327 ASSERT (!parse_datetime (&result
, p
, &now
));
328 p
= "UTC+4:00 next yesterday";
329 ASSERT (!parse_datetime (&result
, p
, &now
));
330 p
= "UTC+4:00 tomorrow ago";
331 ASSERT (!parse_datetime (&result
, p
, &now
));
332 p
= "UTC+4:00 tomorrow hence";
333 ASSERT (!parse_datetime (&result
, p
, &now
));
334 p
= "UTC+4:00 40 now ago";
335 ASSERT (!parse_datetime (&result
, p
, &now
));
336 p
= "UTC+4:00 last tomorrow";
337 ASSERT (!parse_datetime (&result
, p
, &now
));
338 p
= "UTC+4:00 -4 today";
339 ASSERT (!parse_datetime (&result
, p
, &now
));
341 /* And check correct usage of dayshifts */
344 p
= "UTC+400 tomorrow";
345 ASSERT (parse_datetime (&result
, p
, &now
));
346 LOG (p
, now
, result
);
347 p
= "UTC+400 +1 day";
348 ASSERT (parse_datetime (&result2
, p
, &now
));
349 LOG (p
, now
, result2
);
350 ASSERT (result
.tv_sec
== result2
.tv_sec
351 && result
.tv_nsec
== result2
.tv_nsec
);
352 p
= "UTC+400 1 day hence";
353 ASSERT (parse_datetime (&result2
, p
, &now
));
354 LOG (p
, now
, result2
);
355 ASSERT (result
.tv_sec
== result2
.tv_sec
356 && result
.tv_nsec
== result2
.tv_nsec
);
359 p
= "UTC+400 yesterday";
360 ASSERT (parse_datetime (&result
, p
, &now
));
361 LOG (p
, now
, result
);
362 p
= "UTC+400 1 day ago";
363 ASSERT (parse_datetime (&result2
, p
, &now
));
364 LOG (p
, now
, result2
);
365 ASSERT (result
.tv_sec
== result2
.tv_sec
366 && result
.tv_nsec
== result2
.tv_nsec
);
370 ASSERT (parse_datetime (&result
, p
, &now
));
371 LOG (p
, now
, result
);
372 p
= "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
373 ASSERT (parse_datetime (&result2
, p
, &now
));
374 LOG (p
, now
, result2
);
375 ASSERT (result
.tv_sec
== result2
.tv_sec
376 && result
.tv_nsec
== result2
.tv_nsec
);
378 /* Check that some "next Monday", "last Wednesday", etc. are correct. */
379 setenv ("TZ", "UTC0", 1);
380 for (i
= 0; day_table
[i
]; i
++)
382 unsigned int thur2
= 7 * 24 * 3600; /* 2nd thursday */
384 sprintf (tmp
, "NEXT %s", day_table
[i
]);
385 now
.tv_sec
= thur2
+ 4711;
387 ASSERT (parse_datetime (&result
, tmp
, &now
));
388 LOG (tmp
, now
, result
);
389 ASSERT (result
.tv_nsec
== 0);
390 ASSERT (result
.tv_sec
== thur2
+ (i
== 4 ? 7 : (i
+ 3) % 7) * 24 * 3600);
392 sprintf (tmp
, "LAST %s", day_table
[i
]);
393 now
.tv_sec
= thur2
+ 4711;
395 ASSERT (parse_datetime (&result
, tmp
, &now
));
396 LOG (tmp
, now
, result
);
397 ASSERT (result
.tv_nsec
== 0);
398 ASSERT (result
.tv_sec
== thur2
+ ((i
+ 3) % 7 - 7) * 24 * 3600);
401 p
= "THURSDAY UTC+00"; /* The epoch was on Thursday. */
404 ASSERT (parse_datetime (&result
, p
, &now
));
405 LOG (p
, now
, result
);
406 ASSERT (result
.tv_sec
== now
.tv_sec
407 && result
.tv_nsec
== now
.tv_nsec
);
412 ASSERT (parse_datetime (&result
, p
, &now
));
413 LOG (p
, now
, result
);
414 ASSERT (result
.tv_sec
== 24 * 3600
415 && result
.tv_nsec
== now
.tv_nsec
);
417 /* Exercise a sign-extension bug. Before July 2012, an input
418 starting with a high-bit-set byte would be treated like "0". */
419 ASSERT ( ! parse_datetime (&result
, "\xb0", &now
));
421 /* Exercise TZ="" parsing code. */
422 /* These two would infloop or segfault before Feb 2014. */
423 ASSERT ( ! parse_datetime (&result
, "TZ=\"\"\"", &now
));
424 ASSERT ( ! parse_datetime (&result
, "TZ=\"\" \"", &now
));
425 /* Exercise invalid patterns. */
426 ASSERT ( ! parse_datetime (&result
, "TZ=\"", &now
));
427 ASSERT ( ! parse_datetime (&result
, "TZ=\"\\\"", &now
));
428 ASSERT ( ! parse_datetime (&result
, "TZ=\"\\n", &now
));
429 ASSERT ( ! parse_datetime (&result
, "TZ=\"\\n\"", &now
));
430 /* Exercise valid patterns. */
431 ASSERT ( parse_datetime (&result
, "TZ=\"\"", &now
));
432 ASSERT ( parse_datetime (&result
, "TZ=\"\" ", &now
));
433 ASSERT ( parse_datetime (&result
, " TZ=\"\"", &now
));
434 ASSERT ( parse_datetime (&result
, "TZ=\"\\\\\"", &now
));
435 ASSERT ( parse_datetime (&result
, "TZ=\"\\\"\"", &now
));
437 /* Outlandishly-long time zone abbreviations should not cause problems. */
439 static char const bufprefix
[] = "TZ=\"";
440 long int tzname_max
= -1;
442 #ifdef _SC_TZNAME_MAX
443 tzname_max
= sysconf (_SC_TZNAME_MAX
);
445 enum { tzname_alloc
= 2000 };
447 tzname_max
= errno
? 6 : tzname_alloc
;
448 int tzname_len
= tzname_alloc
< tzname_max
? tzname_alloc
: tzname_max
;
449 static char const bufsuffix
[] = "0\" 1970-01-01 01:02:03.123456789";
450 enum { bufsize
= sizeof bufprefix
- 1 + tzname_alloc
+ sizeof bufsuffix
};
452 memcpy (buf
, bufprefix
, sizeof bufprefix
- 1);
453 memset (buf
+ sizeof bufprefix
- 1, 'X', tzname_len
);
454 strcpy (buf
+ sizeof bufprefix
- 1 + tzname_len
, bufsuffix
);
455 ASSERT (parse_datetime (&result
, buf
, &now
));
456 LOG (buf
, now
, result
);
457 ASSERT (result
.tv_sec
== 1 * 60 * 60 + 2 * 60 + 3
458 && result
.tv_nsec
== 123456789);