[tz] [PATCH 3/8] Fix and simplify year-cycle reduction

Paul Eggert eggert at cs.ucla.edu
Wed Mar 24 03:58:48 UTC 2021


* zdump.c (delta_nonneg): Speed up code by using division
rather than repeated addition.  Based on a suggestion from
Arthur David Olson; this version avoids int overflow.
---
 zdump.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/zdump.c b/zdump.c
index 464a334..b006229 100644
--- a/zdump.c
+++ b/zdump.c
@@ -693,11 +693,11 @@ hunt(timezone_t tz, char *name, time_t lot, time_t hit)
 static intmax_t
 delta_nonneg(struct tm *newp, struct tm *oldp)
 {
-	register intmax_t	result;
-	register int		tmy;
-
-	result = 0;
-	for (tmy = oldp->tm_year; tmy < newp->tm_year; ++tmy)
+	intmax_t oldy = oldp->tm_year;
+	int cycles = (newp->tm_year - oldy) / YEARSPERREPEAT;
+	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 += newp->tm_yday - oldp->tm_yday;
 	result *= HOURSPERDAY;
-- 
2.27.0



More information about the tz mailing list