[49/59] Remove leap-second goto from __tzfile_compute

Message ID 20250105055750.1668721-50-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:57 a.m. UTC
  * time/tzfile.c (__tzfile_compute): Do leap second computations
first, to avoid the need for a goto and a label.
---
 time/tzfile.c | 40 +++++++++++++++++++++-------------------
 1 file changed, 21 insertions(+), 19 deletions(-)
  

Patch

diff --git a/time/tzfile.c b/time/tzfile.c
index f86df0c8d5..e491df7d9f 100644
--- a/time/tzfile.c
+++ b/time/tzfile.c
@@ -581,6 +581,26 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 {
   tzidx i;
 
+  /* Find the last leap second correction transition time before TIMER.  */
+  int corr = 0;
+  bool hit = false;
+  for (i = leapcnt; 0 < i; )
+    {
+      i--;
+      if (leaps[i].occur <= timer)
+	{
+	  /* Apply its correction.  */
+	  corr = leaps[i].corr;
+
+	  /* A hit if exactly at a positive leap second.  */
+	  hit = (timer == leaps[i].occur
+		 && (i == 0 ? 0 : leaps[i - 1].corr) < leaps[i].corr);
+	  break;
+	}
+    }
+  *leap_correct = corr;
+  *leap_hit = hit;
+
   if (use_localtime)
     {
       unsigned char ti;
@@ -609,7 +629,7 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 	  /* Use the rules from the TZ string to compute the change.  */
 	  __tz_compute (timer, tp);
 
-	  goto leap;
+	  return;
 	}
       else
 	{
@@ -679,24 +699,6 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 	 this is closer to what proleptic TZ does.  */
       __tzname[dst] = tm_zone;
     }
-
- leap:
-  *leap_correct = 0;
-  *leap_hit = false;
-
-  /* Find the last leap second correction transition time before TIMER.  */
-  i = leapcnt;
-  do
-    if (i-- == 0)
-      return;
-  while (timer < leaps[i].occur);
-
-  /* Apply its correction.  */
-  *leap_correct = leaps[i].corr;
-
-  if (timer == leaps[i].occur /* Exactly at the transition time.  */
-      && (leaps[i].corr > (i == 0 ? 0 : leaps[i - 1].corr)))
-    *leap_hit = true;
 }
 
 weak_alias (transitions, __libc_tzfile_freemem_ptr)