warnings: Support non-literal arguments, and let the user choose the variable
[gnulib.git] / tests / test-getdate.c
index 5bf3728..3433c09 100644 (file)
@@ -49,8 +49,8 @@
 int
 main (int argc, char **argv)
 {
-  bool ret;
   struct timespec result;
+  struct timespec result2;
   struct timespec now;
   const char *p;
 
@@ -85,5 +85,127 @@ main (int argc, char **argv)
   ASSERT (now.tv_sec + 4 * 60 * 60 == result.tv_sec
          && now.tv_nsec == result.tv_nsec);
 
+  /* test if timezone is not being ignored for day offset */
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+400 +24 hours";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC+400 +1 day";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+
+  /* test if several time zones formats are handled same way */
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+14:00";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC+14";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+  p = "UTC+1400";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC-14:00";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC-14";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+  p = "UTC-1400";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+0:15";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC+0015";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC-1:30";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC-130";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+
+
+  /* TZ out of range should cause get_date failure */
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+25:00";
+  ASSERT (!get_date (&result, p, &now));
+
+       /* Check for several invalid countable dayshifts */
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+4:00 +40 yesterday";
+  ASSERT (!get_date (&result, p, &now));
+  p = "UTC+4:00 next yesterday";
+  ASSERT (!get_date (&result, p, &now));
+  p = "UTC+4:00 tomorrow ago";
+  ASSERT (!get_date (&result, p, &now));
+  p = "UTC+4:00 40 now ago";
+  ASSERT (!get_date (&result, p, &now));
+  p = "UTC+4:00 last tomorrow";
+  ASSERT (!get_date (&result, p, &now));
+  p = "UTC+4:00 -4 today";
+  ASSERT (!get_date (&result, p, &now));
+
+  /* And check correct usage of dayshifts */
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+400 tomorrow";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC+400 +1 day";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+400 yesterday";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC+400 1 day ago";
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+  now.tv_sec = 4711;
+  now.tv_nsec = 1267;
+  p = "UTC+400 now";
+  ASSERT (get_date (&result, p, &now));
+  LOG (p, now, result);
+  p = "UTC+400 +0 minutes"; /* silly, but simple "UTC+400" is different*/
+  ASSERT (get_date (&result2, p, &now));
+  LOG (p, now, result2);
+  ASSERT (result.tv_sec == result2.tv_sec
+         && result.tv_nsec == result2.tv_nsec);
+
   return 0;
 }