From eb17b871fefbaab7f82ab050c05f0b1d03d7afed Mon Sep 17 00:00:00 2001 From: roland Date: Sun, 8 Mar 1992 01:34:34 +0000 Subject: [PATCH] Formerly ../time/mktime.c.~5~ --- time/mktime.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/time/mktime.c b/time/mktime.c index 1961c05872..516d150ca9 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -71,17 +71,39 @@ DEFUN(mktime, (tp), register struct tm *tp) max.tm_mday = max.tm_mon = max.tm_year = INT_MAX; } - while (tp->tm_mon < 0) - { - --tp->tm_year; - tp->tm_mon += 12; - } - while (tp->tm_mon > 11) - { - ++tp->tm_year; - tp->tm_mon -= 12; + /* Make all the elements of TP that we pay attention to + be within the ranges of reasonable values for those things. */ +#define normalize(elt, min, max, nextelt) \ + while (tp->elt < min) \ + { \ + --tp->nextelt; \ + tp->elt += max + 1; \ + } \ + while (tp->elt > max) \ + { \ + ++tp->nextelt; \ + tp->elt -= max + 1; \ } + normalize (tm_sec, 0, 59, tm_min); + normalize (tm_min, 0, 59, tm_hour); + normalize (tm_hour, 0, 24, tm_mday); + + /* Normalize the month first so we can use + it to figure the range for the day. */ + normalize (tm_mon, 0, 11, tm_year); + normalize (tm_mday, 1, __mon_lengths[__isleap (tp->tm_year)][tp->tm_mon], + tm_mon); + + /* Normalize the month again, since normalizing + the day may have pushed it out of range. */ + normalize (tm_mon, 0, 11, tm_year); + + /* Normalize the day again, because normalizing + the month may have changed the range. */ + normalize (tm_mday, 1, __mon_lengths[__isleap (tp->tm_year)][tp->tm_mon], + tm_mon); + /* Check for out-of-range values. */ #define lowhigh(field, minmax, cmp) (tp->field cmp minmax.field) #define low(field) lowhigh(field, min, <) -- 2.11.0