changeset 30240:622c3e797b4f

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".
author Ondřej Vašík <ovasik@redhat.com>
date Fri, 26 Sep 2008 15:28:49 +0200
parents aa4b5a65b7dd
children 865a79492d69
files ChangeLog lib/getdate.y tests/test-getdate.c
diffstat 3 files changed, 73 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Sep 29 21:50:01 2008 +0200
+++ b/ChangeLog	Fri Sep 26 15:28:49 2008 +0200
@@ -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.
--- a/lib/getdate.y	Mon Sep 29 21:50:01 2008 +0200
+++ b/lib/getdate.y	Fri Sep 26 15:28:49 2008 +0200
@@ -293,7 +293,7 @@
 %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 @@
 %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 @@
       { 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 @@
       { $$ = 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 @@
 /* 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 },
--- a/tests/test-getdate.c	Mon Sep 29 21:50:01 2008 +0200
+++ b/tests/test-getdate.c	Fri Sep 26 15:28:49 2008 +0200
@@ -160,5 +160,53 @@
   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;
 }