[52/59] Refactor localtime ‘if’ in __tzfile_compute

Message ID 20250105055750.1668721-53-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): Negate the sense of another
‘if’ to simplify future changes.  Almost all of this is
indenting changes.
---
 time/tzfile.c | 158 +++++++++++++++++++++++++-------------------------
 1 file changed, 79 insertions(+), 79 deletions(-)
  

Patch

diff --git a/time/tzfile.c b/time/tzfile.c
index 37b194df16..783518cd6d 100644
--- a/time/tzfile.c
+++ b/time/tzfile.c
@@ -601,100 +601,100 @@  __tzfile_compute (__time64_t timer, int use_localtime,
   *leap_correct = corr;
   *leap_hit = hit;
 
-  if (use_localtime)
-    {
-      unsigned char ti;
+  if (!use_localtime)
+    return;
+
+  unsigned char ti;
 
-      if (__glibc_unlikely (timecnt == 0 ? tzspec == NULL
-			    : timer < transitions[0]))
+  if (__glibc_unlikely (timecnt == 0 ? tzspec == NULL
+			: timer < transitions[0]))
+    {
+      /* TIMER is before the first transition; or there are no
+	 transitions and no TZ string.  Use the first type.  */
+      ti = 0;
+    }
+  else if (timecnt == 0 || timer >= transitions[timecnt - 1])
+    {
+      /* 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))
 	{
-	  /* TIMER is before the first transition; or there are no
-	     transitions and no TZ string.  Use the first type.  */
-	  ti = 0;
+	  /* Use the rules from the TZ string to compute the change.  */
+	  __tz_compute (timer, tp);
+	  return;
 	}
-      else if (timecnt == 0 || timer >= transitions[timecnt - 1])
-	{
-	  /* 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
+      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)
 	{
-	  /* 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 - 1 - i;
+	  if (timer < transitions[i])
 	    {
-	      i = timecnt - 1 - i;
-	      if (timer < transitions[i])
-		{
-		  if (i < 10 || timer >= transitions[i - 10])
-		    {
-		      /* Linear search.  */
-		      while (timer < transitions[i - 1])
-			--i;
-		      goto found;
-		    }
-		  hi = i - 10;
-		}
-	      else
+	      if (i < 10 || timer >= transitions[i - 10])
 		{
-		  if (timecnt - i <= 10 || timer < transitions[i + 10])
-		    {
-		      /* Linear search.  */
-		      while (timer >= transitions[i])
-			++i;
-		      goto found;
-		    }
-		  lo = i + 10;
+		  /* Linear search.  */
+		  while (timer < transitions[i - 1])
+		    --i;
+		  goto found;
 		}
+	      hi = i - 10;
 	    }
-
-	  /* Binary search.  */
-	  /* assert (timer >= transitions[lo] && timer < transitions[hi]); */
-	  while (lo + 1 < hi)
+	  else
 	    {
-	      i = (lo >> 1) + (hi >> 1) + (lo & hi & 1);
-	      if (timer < transitions[i])
-		hi = i;
-	      else
-		lo = i;
+	      if (timecnt - i <= 10 || timer < transitions[i + 10])
+		{
+		  /* Linear search.  */
+		  while (timer >= transitions[i])
+		    ++i;
+		  goto found;
+		}
+	      lo = i + 10;
 	    }
-	  i = hi;
-
-	found:
-	  ti = type_idxs[i - 1];
 	}
 
-      struct ttinfo *info = &types[ti];
-      unsigned char dst = info->dst;
-      char *tm_zone = __tzstring (&zone_names[info->idx]);
-      tp->tm_isdst = dst;
-      tp->tm_gmtoff = info->utoff;
-      tp->tm_zone = tm_zone;
+      /* 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;
 
-      /* Set tzname[isdst] too; although not required by POSIX or documented,
-	 this is closer to what proleptic TZ does.  */
-      __tzname[dst] = tm_zone;
+    found:
+      ti = type_idxs[i - 1];
     }
+
+  struct ttinfo *info = &types[ti];
+  unsigned char dst = info->dst;
+  char *tm_zone = __tzstring (&zone_names[info->idx]);
+  tp->tm_isdst = dst;
+  tp->tm_gmtoff = info->utoff;
+  tp->tm_zone = tm_zone;
+
+  /* Set tzname[isdst] too; although not required by POSIX or documented,
+     this is closer to what proleptic TZ does.  */
+  __tzname[dst] = tm_zone;
 }
 
 weak_alias (transitions, __libc_tzfile_freemem_ptr)