From 9eb2711cd03b3825e36cff33e7e7fccbbfc504d1 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Ond=C5=99ej=20Va=C5=A1=C3=ADk?= Date: Fri, 27 Jun 2008 17:37:40 +0200 Subject: [PATCH] *lib/getdate.y: Factorize duplicate code for relative time offset and hhmmss times. Signed-off-by: Ondřej Vašík --- lib/getdate.y | 86 ++++++++++++++++++++++----------------------------------- 1 files changed, 33 insertions(+), 53 deletions(-) diff --git a/lib/getdate.y b/lib/getdate.y index 7b55c31..e4f2598 100644 --- a/lib/getdate.y +++ b/lib/getdate.y @@ -246,6 +246,30 @@ digits_to_date_time (parser_control *pc, textint text_int) } } +/* Extract relative time multiplied by factor into *pc */ +static void +extract_relative_time (parser_control *pc, relative_time rel, int factor) +{ + pc->rel.ns += factor*rel.ns; + pc->rel.seconds += factor*rel.seconds; + pc->rel.minutes += factor*rel.minutes; + pc->rel.hour += factor*rel.hour; + pc->rel.day += factor*rel.day; + pc->rel.month += factor*rel.month; + pc->rel.year += factor*rel.year; + pc->rels_seen = true; +} + +/* Extract hours, minutes, seconds and nanoseconds from arguments into *pc */ +static void +extract_hhmmss (parser_control *pc, long int ho, long int mi, time_t sec, long int nsec) +{ + pc->hour = ho; + pc->minutes = mi; + pc->seconds.tv_sec = sec; + pc->seconds.tv_nsec = nsec; +} + %} /* We want a reentrant parser, even if the TZ manipulation and the calls to @@ -313,7 +337,6 @@ item: | day { pc->days_seen++; } | rel - { pc->rels_seen = true; } | number | hybrid ; @@ -321,42 +344,29 @@ item: time: tUNUMBER tMERIDIAN { - pc->hour = $1.value; - pc->minutes = 0; - pc->seconds.tv_sec = 0; - pc->seconds.tv_nsec = 0; + extract_hhmmss (pc, $1.value, 0, 0, 0); pc->meridian = $2; } | tUNUMBER ':' tUNUMBER o_merid { - pc->hour = $1.value; - pc->minutes = $3.value; - pc->seconds.tv_sec = 0; - pc->seconds.tv_nsec = 0; + extract_hhmmss (pc, $1.value, $3.value, 0, 0); pc->meridian = $4; } | tUNUMBER ':' tUNUMBER tSNUMBER o_colon_minutes { - pc->hour = $1.value; - pc->minutes = $3.value; - pc->seconds.tv_sec = 0; - pc->seconds.tv_nsec = 0; + extract_hhmmss (pc, $1.value, $3.value, 0, 0); pc->meridian = MER24; pc->zones_seen++; pc->time_zone = time_zone_hhmm ($4, $5); } | tUNUMBER ':' tUNUMBER ':' unsigned_seconds o_merid { - pc->hour = $1.value; - pc->minutes = $3.value; - pc->seconds = $5; + extract_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec); pc->meridian = $6; } | tUNUMBER ':' tUNUMBER ':' unsigned_seconds tSNUMBER o_colon_minutes { - pc->hour = $1.value; - pc->minutes = $3.value; - pc->seconds = $5; + extract_hhmmss (pc, $1.value, $3.value, $5.tv_sec, $5.tv_nsec); pc->meridian = MER24; pc->zones_seen++; pc->time_zone = time_zone_hhmm ($6, $7); @@ -381,14 +391,7 @@ zone: { pc->time_zone = $1; } | tZONE relunit_snumber { pc->time_zone = $1; - pc->rel.ns += $2.ns; - pc->rel.seconds += $2.seconds; - pc->rel.minutes += $2.minutes; - pc->rel.hour += $2.hour; - pc->rel.day += $2.day; - pc->rel.month += $2.month; - pc->rel.year += $2.year; - pc->rels_seen = true; } + extract_relative_time (pc, $2, 1); } | tZONE tSNUMBER o_colon_minutes { pc->time_zone = $1 + time_zone_hhmm ($2, $3); } | tDAYZONE @@ -495,25 +498,9 @@ date: rel: relunit tAGO - { - pc->rel.ns -= $1.ns; - pc->rel.seconds -= $1.seconds; - pc->rel.minutes -= $1.minutes; - pc->rel.hour -= $1.hour; - pc->rel.day -= $1.day; - pc->rel.month -= $1.month; - pc->rel.year -= $1.year; - } + { extract_relative_time (pc, $1, -1); } | relunit - { - pc->rel.ns += $1.ns; - pc->rel.seconds += $1.seconds; - pc->rel.minutes += $1.minutes; - pc->rel.hour += $1.hour; - pc->rel.day += $1.day; - pc->rel.month += $1.month; - pc->rel.year += $1.year; - } + { extract_relative_time (pc, $1, 1); } ; relunit: @@ -600,14 +587,7 @@ hybrid: /* Hybrid all-digit and relative offset, so that we accept e.g., "YYYYMMDD +N days" as well as "YYYYMMDD N days". */ digits_to_date_time (pc, $1); - pc->rel.ns += $2.ns; - pc->rel.seconds += $2.seconds; - pc->rel.minutes += $2.minutes; - pc->rel.hour += $2.hour; - pc->rel.day += $2.day; - pc->rel.month += $2.month; - pc->rel.year += $2.year; - pc->rels_seen = true; + extract_relative_time (pc, $2, 1); } ; -- 1.5.2.2