Hello, This patch includes all your suggestions and new tests for test-getdate.c.
Giuseppe Bruno Haible <br...@clisp.org> writes: > Jim Meyering wrote: >> tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 >> + + 7 * (pc.day_ordinal >> + + (0 < pc.day_ordinal >> + && tm.tm_wday == pc.day_number) >> + - (0 < pc.day_ordinal))); > > This can be simplified to > > tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 > + 7 * (pc.day_ordinal > - (0 < pc.day_ordinal > && tm.tm_wday != pc.day_number))); > > Bruno
>From 75f9e5dcd077bd4faaebf6a51c8e89e1eb7d7e44 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano <gscriv...@gnu.org> Date: Fri, 1 May 2009 10:39:13 +0200 Subject: [PATCH] Add a week when the wday is the same as the current one. * lib/getdate.y (get_date): Correct the calculation of tm_mday so that e.g., "next tues" (when run on a tuesday) results in a date that is one week in the future, and not today's date. Reported by Tom Broadhurst http://savannah.gnu.org/bugs/?25406 and earlier by Martin Bernreuther. * tests/test-getdate.c (main): Check that "next DAY" is always in the future. --- lib/getdate.y | 4 +++- tests/test-getdate.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletions(-) diff --git a/lib/getdate.y b/lib/getdate.y index 877b264..aa375e5 100644 --- a/lib/getdate.y +++ b/lib/getdate.y @@ -1435,7 +1435,9 @@ get_date (struct timespec *result, char const *p, struct timespec const *now) if (pc.days_seen && ! pc.dates_seen) { tm.tm_mday += ((pc.day_number - tm.tm_wday + 7) % 7 - + 7 * (pc.day_ordinal - (0 < pc.day_ordinal))); + + 7 * (pc.day_ordinal + + (pc.day_ordinal && tm.tm_wday == pc.day_number) - + (0 < pc.day_ordinal))); tm.tm_isdst = -1; Start = mktime (&tm); if (Start == (time_t) -1) diff --git a/tests/test-getdate.c b/tests/test-getdate.c index 7dfb09e..d06fb98 100644 --- a/tests/test-getdate.c +++ b/tests/test-getdate.c @@ -48,6 +48,22 @@ #define LOG(str, now, res) (void) 0 #endif +static const char* day_table[] = +{ + "SUNDAY", + "MONDAY", + "TUESDAY", + "TUES", + "WEDNESDAY", + "WEDNES", + "THURSDAY", + "THUR", + "THURS", + "FRIDAY", + "SATURDAY", + 0 +}; + int main (int argc, char **argv) { @@ -55,6 +71,7 @@ main (int argc, char **argv) struct timespec result2; struct timespec now; const char *p; + int i; set_program_name (argv[0]); @@ -211,5 +228,19 @@ main (int argc, char **argv) ASSERT (result.tv_sec == result2.tv_sec && result.tv_nsec == result2.tv_nsec); + /* Check that every 'next DAY' is in the future. */ + for (i = 0; day_table[i]; i++) + { + char tmp[32]; + sprintf (tmp, "NEXT %s", day_table[i]); + now.tv_sec = 4711; + now.tv_nsec = 1267; + ASSERT (get_date (&result, tmp, &now)); + LOG (tmp, now, result); + ASSERT (result.tv_sec > now.tv_sec && + result.tv_nsec == 0); + } + + return 0; } -- 1.6.2.1