Refactor to coalesce multiple ways of figuring out how many days
are in a year.
* localtime.c (year_lengths), zic.c (len_years): Remove.
All uses replaced with year_days.
* private.h (year_days): New macro.
(isleap_sum): Remove.  All uses redone to use ...
(year_sum_days): ... this new macro.
---
 localtime.c | 16 ++++++----------
 private.h   |  3 ++-
 strftime.c  |  9 +++------
 zdump.c     |  4 ++--
 zic.c       |  6 +-----
 5 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/localtime.c b/localtime.c
index 436a4b80..20e88c35 100644
--- a/localtime.c
+++ b/localtime.c
@@ -1334,10 +1334,6 @@ static const int mon_lengths[2][MONSPERYEAR] = {
        { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 };
 
-static const int       year_lengths[2] = {
-       DAYSPERNYEAR, DAYSPERLYEAR
-};
-
 /* Is C an ASCII digit?  */
 static bool
 is_digit(char c)
@@ -1723,7 +1719,7 @@ tzparse(const char *name, struct state *sp, struct state 
const *basep)
 
                do {
                  int_fast32_t yearsecs
-                   = year_lengths[isleap(yearbeg - 1)] * SECSPERDAY;
+                   = year_days(yearbeg - 1) * SECSPERDAY;
                  time_t janfirst1 = janfirst;
                  yearbeg--;
                  if (increment_overflow_time(&janfirst1, -yearsecs)) {
@@ -1736,7 +1732,7 @@ tzparse(const char *name, struct state *sp, struct state 
const *basep)
 
                while (true) {
                  int_fast32_t yearsecs
-                   = year_lengths[isleap(yearbeg)] * SECSPERDAY;
+                   = year_days(yearbeg) * SECSPERDAY;
                  int yearbeg1 = yearbeg;
                  time_t janfirst1 = janfirst;
                  if (increment_overflow_time(&janfirst1, yearsecs)
@@ -1754,7 +1750,7 @@ tzparse(const char *name, struct state *sp, struct state 
const *basep)
                  int_fast32_t
                    starttime = transtime(year, &start, stdoffset),
                    endtime = transtime(year, &end, dstoffset),
-                   yearsecs = year_lengths[isleap(year)] * SECSPERDAY;
+                   yearsecs = year_days(year) * SECSPERDAY;
                  bool reversed = endtime < starttime;
                  if (reversed) {
                    int_fast32_t swap = starttime;
@@ -2383,7 +2379,7 @@ timesub(const time_t *timep, int_fast32_t offset,
        idays += dayoff % DAYSPERREPEAT + 2 * DAYSPERREPEAT;
        idays %= DAYSPERREPEAT;
        /* Increase Y and decrease IDAYS until IDAYS is in range for Y.  */
-       while (year_lengths[isleap(y)] <= idays) {
+       while (year_days(y) <= idays) {
                int tdelta = idays / DAYSPERLYEAR;
                int_fast32_t ydelta = tdelta + !tdelta;
                time_t newy = y + ydelta;
@@ -2651,12 +2647,12 @@ time2sub(struct tm *const tmp,
 
        while (mday <= 0) {
          iinntt li = y - (yourtm.tm_mon <= 1);
-         mday += year_lengths[isleap(li)];
+         mday += year_days(li);
          y--;
        }
        while (DAYSPERLYEAR < mday) {
          iinntt li = y + (1 < yourtm.tm_mon);
-         mday -= year_lengths[isleap(li)];
+         mday -= year_days(li);
          y++;
        }
        yourtm.tm_mday = mday;
diff --git a/private.h b/private.h
index 8ef03daf..2e782435 100644
--- a/private.h
+++ b/private.h
@@ -1176,6 +1176,7 @@ enum {
 };
 
 #define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
+#define year_days(y) (DAYSPERNYEAR + isleap(y))
 
 /*
 ** Since everything in isleap is modulo 400 (or a factor of 400), we know that
@@ -1189,6 +1190,6 @@ enum {
 ** We use this to avoid addition overflow problems.
 */
 
-#define isleap_sum(a, b)       isleap((a) % 400 + (b) % 400)
+#define year_sum_days(a, b) (DAYSPERNYEAR + isleap((a) % 400 + (b) % 400))
 
 #endif /* !defined PRIVATE_H */
diff --git a/strftime.c b/strftime.c
index c2490105..a7d48849 100644
--- a/strftime.c
+++ b/strftime.c
@@ -464,9 +464,7 @@ label:
                                                int     bot;
                                                int     top;
 
-                                               len = isleap_sum(year, base) ?
-                                                       DAYSPERLYEAR :
-                                                       DAYSPERNYEAR;
+                                               len = year_sum_days(year, base);
                                                /*
                                                ** What yday (-3 ... 3) does
                                                ** the ISO year begin on?
@@ -493,9 +491,8 @@ label:
                                                        break;
                                                }
                                                --base;
-                                               yday += isleap_sum(year, base) ?
-                                                       DAYSPERLYEAR :
-                                                       DAYSPERNYEAR;
+                                               yday += year_sum_days(year,
+                                                                     base);
                                        }
 #ifdef XPG4_1994_04_09
                                        if ((w == 52 &&
diff --git a/zdump.c b/zdump.c
index 54ef63c0..c627f8bd 100644
--- a/zdump.c
+++ b/zdump.c
@@ -719,7 +719,7 @@ delta_nonneg(struct tm *newp, struct tm *oldp)
        intmax_t sec = SECSPERREPEAT, result = cycles * sec;
        int tmy = oldp->tm_year + cycles * YEARSPERREPEAT;
        for ( ; tmy < newp->tm_year; ++tmy)
-               result += DAYSPERNYEAR + isleap_sum(tmy, TM_YEAR_BASE);
+         result += year_sum_days(tmy, TM_YEAR_BASE);
        result += newp->tm_yday - oldp->tm_yday;
        result *= HOURSPERDAY;
        result += newp->tm_hour - oldp->tm_hour;
@@ -746,7 +746,7 @@ adjusted_yday(struct tm const *a, struct tm const *b)
 {
   int yday = a->tm_yday;
   if (b->tm_year < a->tm_year)
-    yday += 365 + isleap_sum(b->tm_year, TM_YEAR_BASE);
+    yday += year_sum_days(b->tm_year, TM_YEAR_BASE);
   return yday;
 }
 #endif
diff --git a/zic.c b/zic.c
index 761f9e0d..b643d6c7 100644
--- a/zic.c
+++ b/zic.c
@@ -528,10 +528,6 @@ static const int   len_months[2][MONSPERYEAR] = {
        { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 };
 
-static const int       len_years[2] = {
-       DAYSPERNYEAR, DAYSPERLYEAR
-};
-
 static struct attype {
        zic_t           at;
        bool            dontmerge;
@@ -4153,7 +4149,7 @@ rpytime(const struct rule *rp, zic_t wantedy)
        wantedy = y + (yrem + 2 * YEARSPERREPEAT) % YEARSPERREPEAT;
 
        while (wantedy != y) {
-               i = len_years[isleap(y)];
+               i = year_days(y);
                dayoff = oadd(dayoff, i);
                y++;
        }
-- 
2.55.0

Reply via email to