getdate.y: disallow countable dayshifts like "4 yesterday ago"
authorOndřej Vašík <ovasik@redhat.com>
Fri, 26 Sep 2008 13:28:49 +0000 (15:28 +0200)
committerJim Meyering <meyering@redhat.com>
Mon, 29 Sep 2008 21:24:06 +0000 (23:24 +0200)
* lib/getdate.y (relative_time_table) [tDAY_SHIFT]: New type for
exactly specified dayshifts.
(dayshift): New rule.
(rel): Add dayshift.
(relative_time_table) [tomorrow, yesterday, today, now]:
Use tDAY_SHIFT in place of tDAY_UNIT.
* tests/test-getdate.c: Add tests for now-disallowed countable
dayshifts, e.g., "4 yesterday ago".

ChangeLog
lib/getdate.y
tests/test-getdate.c

index 76c7363..efb0c0a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2008-09-29  Ondřej Vašík  <ovasik@redhat.com>
+
+       getdate.y: disallow countable dayshifts like "4 yesterday ago"
+       * lib/getdate.y (relative_time_table) [tDAY_SHIFT]: New type for
+       exactly specified dayshifts.
+       (dayshift): New rule.
+       (rel): Add dayshift.
+       (relative_time_table) [tomorrow, yesterday, today, now]:
+       Use tDAY_SHIFT in place of tDAY_UNIT.
+       * tests/test-getdate.c: Add tests for now-disallowed countable
+       dayshifts, e.g., "4 yesterday ago".
+
 2008-09-29  Bruno Haible  <bruno@clisp.org>
 
        * tests/test-posix_spawn1.c: Renamed from tests/test-posix_spawn.c.
index f9cd86c..877b264 100644 (file)
@@ -293,7 +293,7 @@ set_hhmmss (parser_control *pc, long int hour, long int minutes,
 %token tAGO tDST
 
 %token tYEAR_UNIT tMONTH_UNIT tHOUR_UNIT tMINUTE_UNIT tSEC_UNIT
-%token <intval> tDAY_UNIT
+%token <intval> tDAY_UNIT tDAY_SHIFT
 
 %token <intval> tDAY tDAYZONE tLOCAL_ZONE tMERIDIAN
 %token <intval> tMONTH tORDINAL tZONE
@@ -304,7 +304,7 @@ set_hhmmss (parser_control *pc, long int hour, long int minutes,
 %type <intval> o_colon_minutes o_merid
 %type <timespec> seconds signed_seconds unsigned_seconds
 
-%type <rel> relunit relunit_snumber
+%type <rel> relunit relunit_snumber dayshift
 
 %%
 
@@ -502,6 +502,8 @@ rel:
       { apply_relative_time (pc, $1, -1); }
   | relunit
       { apply_relative_time (pc, $1, 1); }
+  | dayshift
+      { apply_relative_time (pc, $1, 1); }
   ;
 
 relunit:
@@ -563,6 +565,11 @@ relunit_snumber:
       { $$ = RELATIVE_TIME_0; $$.seconds = $1.value; }
   ;
 
+dayshift:
+    tDAY_SHIFT
+      { $$ = RELATIVE_TIME_0; $$.day = $1; }
+  ;
+
 seconds: signed_seconds | unsigned_seconds;
 
 signed_seconds:
@@ -669,10 +676,10 @@ static table const time_units_table[] =
 /* Assorted relative-time words. */
 static table const relative_time_table[] =
 {
-  { "TOMORROW",        tDAY_UNIT,       1 },
-  { "YESTERDAY",tDAY_UNIT,     -1 },
-  { "TODAY",   tDAY_UNIT,       0 },
-  { "NOW",     tDAY_UNIT,       0 },
+  { "TOMORROW",        tDAY_SHIFT,      1 },
+  { "YESTERDAY",tDAY_SHIFT,    -1 },
+  { "TODAY",   tDAY_SHIFT,      0 },
+  { "NOW",     tDAY_SHIFT,      0 },
   { "LAST",    tORDINAL,       -1 },
   { "THIS",    tORDINAL,        0 },
   { "NEXT",    tORDINAL,        1 },
index adbcf3a..874d918 100644 (file)
@@ -160,5 +160,53 @@ main (int argc, char **argv)
   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;
 }