[53/59] Refactor post-transition-table ‘if’

Message ID 20250105055750.1668721-54-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): Reorder code and ‘if’
to remove a goto, for clarity.  Again, mostly indenting changes.
---
 time/tzfile.c | 112 +++++++++++++++++++++++++-------------------------
 1 file changed, 57 insertions(+), 55 deletions(-)
  

Patch

diff --git a/time/tzfile.c b/time/tzfile.c
index 783518cd6d..cde8d74b6f 100644
--- a/time/tzfile.c
+++ b/time/tzfile.c
@@ -613,73 +613,75 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 	 transitions and no TZ string.  Use the first type.  */
       ti = 0;
     }
-  else if (timecnt == 0 || timer >= transitions[timecnt - 1])
+  else
     {
-      /* TIMER is after the last transition.  Use the TZ string if
-	 it is present and we can convert to the broken down structure.  */
-      if (__glibc_likely (tzspec != NULL)
-	  && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL))
+      if (timecnt == 0 || timer >= transitions[timecnt - 1])
 	{
-	  /* Use the rules from the TZ string to compute the change.  */
-	  __tz_compute (timer, tp);
-	  return;
-	}
+	  /* TIMER is after the last transition.  Use the TZ string if
+	     it is present and we can convert to the broken down structure.  */
+	  if (__glibc_likely (tzspec != NULL)
+	      && __glibc_likely (__offtime (timer, 0, 0, tp) != NULL))
+	    {
+	      /* Use the rules from the TZ string to compute the change.  */
+	      __tz_compute (timer, tp);
+	      return;
+	    }
 
-      i = timecnt;
-      goto found;
-    }
-  else
-    {
-      /* Find the first transition after TIMER, and
-	 then pick the type of the transition before it.  */
-      tzidx lo = 0;
-      tzidx hi = timecnt - 1;
-      /* Assume that DST is changing twice a year and guess
-	 initial search spot from it.  Half of a gregorian year
-	 has on average 365.2425 * 86400 / 2 = 15778476 seconds.
-	 Although i's value can be wrong if overflow occurs,
-	 this is harmless because it is just a guess.  */
-      __time64_t tdiff;
-      ckd_sub (&tdiff, transitions[timecnt - 1], timer);
-      ckd_add (&i, tdiff / 15778476, 0);
-      if (i < timecnt)
+	  i = timecnt;
+	}
+      else
 	{
-	  i = timecnt - 1 - i;
-	  if (timer < transitions[i])
+	  /* Find the first transition after TIMER, and
+	     then pick the type of the transition before it.  */
+	  tzidx lo = 0;
+	  tzidx hi = timecnt - 1;
+	  /* Assume that DST is changing twice a year and guess
+	     initial search spot from it.  Half of a gregorian year
+	     has on average 365.2425 * 86400 / 2 = 15778476 seconds.
+	     Although i's value can be wrong if overflow occurs,
+	     this is harmless because it is just a guess.  */
+	  __time64_t tdiff;
+	  ckd_sub (&tdiff, transitions[timecnt - 1], timer);
+	  ckd_add (&i, tdiff / 15778476, 0);
+	  if (i < timecnt)
 	    {
-	      if (i < 10 || timer >= transitions[i - 10])
+	      i = timecnt - 1 - i;
+	      if (timer < transitions[i])
 		{
-		  /* Linear search.  */
-		  while (timer < transitions[i - 1])
-		    --i;
-		  goto found;
+		  if (i < 10 || timer >= transitions[i - 10])
+		    {
+		      /* Linear search.  */
+		      while (timer < transitions[i - 1])
+			--i;
+		      goto found;
+		    }
+		  hi = i - 10;
 		}
-	      hi = i - 10;
-	    }
-	  else
-	    {
-	      if (timecnt - i <= 10 || timer < transitions[i + 10])
+	      else
 		{
-		  /* Linear search.  */
-		  while (timer >= transitions[i])
-		    ++i;
-		  goto found;
+		  if (timecnt - i <= 10 || timer < transitions[i + 10])
+		    {
+		      /* Linear search.  */
+		      while (timer >= transitions[i])
+			++i;
+		      goto found;
+		    }
+		  lo = i + 10;
 		}
-	      lo = i + 10;
 	    }
-	}
 
-      /* Binary search.  */
-      /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
-      while (lo + 1 < hi)
-	{
-	  i = (lo >> 1) + (hi >> 1) + (lo & hi & 1);
-	  if (timer < transitions[i])
-	    hi = i;
-	  else
-	    lo = i;
+	  /* Binary search.  */
+	  /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
+	  while (lo + 1 < hi)
+	    {
+	      i = (lo >> 1) + (hi >> 1) + (lo & hi & 1);
+	      if (timer < transitions[i])
+		hi = i;
+	      else
+		lo = i;
+	    }
+	  i = hi;
 	}
-      i = hi;
 
     found:
       ti = type_idxs[i - 1];