[15/59] Simplify __tz_compute

Message ID 20250105055750.1668721-16-eggert@cs.ucla.edu (mailing list archive)
State New
Headers
Series time: sync mktime from Gnulib |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

Paul Eggert Jan. 5, 2025, 5:56 a.m. UTC
  * time/tzset.c (__tz_compute): Omit last arg USE_LOCALTIME,
since this function should be called only when using localtime.
All callers changed.  Clarify isdst computation.
---
 time/tzfile.c |  2 +-
 time/tzset.c  | 35 ++++++++++++-----------------------
 time/tzset.h  |  2 +-
 3 files changed, 14 insertions(+), 25 deletions(-)
  

Patch

diff --git a/time/tzfile.c b/time/tzfile.c
index 7125f5a8e3..5a7514e466 100644
--- a/time/tzfile.c
+++ b/time/tzfile.c
@@ -636,7 +636,7 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 	    goto use_last;
 
 	  /* Use the rules from the TZ string to compute the change.  */
-	  __tz_compute (timer, tp, 1);
+	  __tz_compute (timer, tp);
 
 	  goto leap;
 	}
diff --git a/time/tzset.c b/time/tzset.c
index 6783067053..bd3509307e 100644
--- a/time/tzset.c
+++ b/time/tzset.c
@@ -525,32 +525,21 @@  compute_change (tz_rule *rule, int year)
 }
 
 
-/* Figure out the correct timezone for TM and set `__tzname',
-   `__timezone', and `__daylight' accordingly.  */
+/* Figure out the correct local timezone for TM.  */
 void
-__tz_compute (__time64_t timer, struct tm *tm, int use_localtime)
+__tz_compute (__time64_t timer, struct tm *tm)
 {
   compute_change (&tz_rules[0], 1900 + tm->tm_year);
   compute_change (&tz_rules[1], 1900 + tm->tm_year);
 
-  if (use_localtime)
-    {
-      int isdst;
-
-      /* We have to distinguish between northern and southern
-	 hemisphere.  For the latter the daylight saving time
-	 ends in the next year.  */
-      if (__builtin_expect (tz_rules[0].change
-			    > tz_rules[1].change, 0))
-	isdst = (timer < tz_rules[1].change
-		 || timer >= tz_rules[0].change);
-      else
-	isdst = (timer >= tz_rules[0].change
-		 && timer < tz_rules[1].change);
-      tm->tm_isdst = isdst;
-      tm->tm_zone = __tzname[isdst];
-      tm->tm_gmtoff = tz_rules[isdst].offset;
-    }
+  /* Distinguish between northern and southern hemisphere.
+     For the latter the daylight saving time ends in the next year.  */
+  bool isdst = (__glibc_likely (tz_rules[0].change <= tz_rules[1].change)
+		? tz_rules[0].change <= timer && timer < tz_rules[1].change
+		: ! (tz_rules[1].change <= timer && timer < tz_rules[0].change));
+  tm->tm_isdst = isdst;
+  tm->tm_zone = __tzname[isdst];
+  tm->tm_gmtoff = tz_rules[isdst].offset;
 }
 
 /* Reinterpret the TZ environment variable and set `tzname'.  */
@@ -599,8 +588,8 @@  __tz_convert (__time64_t timer, int use_localtime, struct tm *tp)
     {
       if (! __offtime (timer, 0, tp))
 	tp = NULL;
-      else
-	__tz_compute (timer, tp, use_localtime);
+      else if (use_localtime)
+	__tz_compute (timer, tp);
       leap_correction = 0;
       leap_extra_sec = false;
     }
diff --git a/time/tzset.h b/time/tzset.h
index d2d5853f0b..8c87ad6a24 100644
--- a/time/tzset.h
+++ b/time/tzset.h
@@ -15,7 +15,7 @@  extern void __tzfile_compute (__time64_t timer, int use_localtime,
 			      int *leap_correct, bool *leap_hit,
 			      struct tm *tp) attribute_hidden;
 extern void __tzset_parse_tz (const char *tz) attribute_hidden;
-extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime)
+extern void __tz_compute (__time64_t timer, struct tm *tm)
   __THROW attribute_hidden;
 
 __libc_lock_define (extern, __tzset_lock attribute_hidden);