[18/59] Match RFC 9636 names

Message ID 20250105055750.1668721-19-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
  Refactor to change spelling of identifiers to match RFC 9636.
This eases conformance review.
* time/tzfile.c (struct ttinfo, struct leap, __tzfile_read)
(__tzfile_default, __tzfile_compute): Change member names and
locals to match the RFC.  All use changed.
(timecnt, typecnt, leapcnt): Rename from num_transitions,
num_types, num_leaps.  All uses changed.
---
 time/tzfile.c | 217 +++++++++++++++++++++++++-------------------------
 1 file changed, 109 insertions(+), 108 deletions(-)
  

Patch

diff --git a/time/tzfile.c b/time/tzfile.c
index 5a7514e466..dcd2bcc96d 100644
--- a/time/tzfile.c
+++ b/time/tzfile.c
@@ -40,17 +40,17 @@  static __time64_t tzfile_mtime;
 
 struct ttinfo
   {
-    int offset;			/* Seconds east of GMT.  */
-    unsigned char isdst;	/* Used to set tm_isdst.  */
+    int utoff;			/* Seconds east of UT.  */
+    unsigned char dst;		/* Used to set tm_isdst.  */
     unsigned char idx;		/* Index into `zone_names'.  */
     unsigned char isstd;	/* Transition times are in standard time.  */
-    unsigned char isgmt;	/* Transition times are in GMT.  */
+    unsigned char isutc;	/* Transition times are in UT.  */
   };
 
 struct leap
   {
-    __time64_t transition;	/* Time the transition takes effect.  */
-    int change;			/* Seconds of correction to apply.  */
+    __time64_t occur;		/* Time the transition takes effect.  */
+    int corr;			/* Seconds of correction to apply.  */
   };
 
 /* Internal index derived from a count taken from a TZif file.
@@ -59,14 +59,14 @@  struct leap
    code ordinarily should not assume it is unsigned.  */
 typedef unsigned int tzidx;
 
-static tzidx num_transitions;
+static tzidx timecnt;
 static __time64_t *transitions;
 static unsigned char *type_idxs;
-static tzidx num_types;
+static tzidx typecnt;
 static struct ttinfo *types;
 static char *zone_names;
 static long int rule_stdoff;
-static tzidx num_leaps;
+static tzidx leapcnt;
 static struct leap *leaps;
 static char *tzspec;
 
@@ -142,10 +142,10 @@  void
 __tzfile_read (const char *file)
 {
   static const char default_tzdir[] = TZDIR;
-  tzidx num_isstd, num_isgmt;
+  tzidx isstdcnt, isutcnt;
   FILE *f;
   union { struct tzhead tzhead; tzidx tzidx_aligned; } u;
-  tzidx chars;
+  tzidx charcnt;
   tzidx i;
   int was_using_tzfile = __use_tzfile;
   int trans_width = 4;
@@ -227,15 +227,15 @@  __tzfile_read (const char *file)
     goto lose;
   f_offset += sizeof u.tzhead;
 
-  num_transitions = tzidx_decode (u.tzhead.tzh_timecnt);
-  num_types = tzidx_decode (u.tzhead.tzh_typecnt);
-  chars = tzidx_decode (u.tzhead.tzh_charcnt);
-  num_leaps = tzidx_decode (u.tzhead.tzh_leapcnt);
-  num_isstd = tzidx_decode (u.tzhead.tzh_ttisstdcnt);
-  num_isgmt = tzidx_decode (u.tzhead.tzh_ttisutcnt);
+  timecnt = tzidx_decode (u.tzhead.tzh_timecnt);
+  typecnt = tzidx_decode (u.tzhead.tzh_typecnt);
+  charcnt = tzidx_decode (u.tzhead.tzh_charcnt);
+  leapcnt = tzidx_decode (u.tzhead.tzh_leapcnt);
+  isstdcnt = tzidx_decode (u.tzhead.tzh_ttisstdcnt);
+  isutcnt = tzidx_decode (u.tzhead.tzh_ttisutcnt);
 
-  if (__glibc_unlikely (num_isstd > num_types || num_isgmt > num_types
-			|| !num_types))
+  if (__glibc_unlikely (isstdcnt > typecnt || isutcnt > typecnt
+			|| !typecnt))
     goto lose;
 
   if (trans_width == 4 && u.tzhead.tzh_version[0] != '\0')
@@ -246,14 +246,14 @@  __tzfile_read (const char *file)
       /* Position the stream before the second header.  */
       off_t to_skip, product;
       bool v = false;
-      v |= ckd_mul (&to_skip, num_transitions, 4 + 1);
-      v |= ckd_mul (&product, num_types, 6);
+      v |= ckd_mul (&to_skip, timecnt, 4 + 1);
+      v |= ckd_mul (&product, typecnt, 6);
       v |= ckd_add (&to_skip, to_skip, product);
-      v |= ckd_add (&to_skip, to_skip, chars);
-      v |= ckd_mul (&product, num_leaps, 8);
+      v |= ckd_add (&to_skip, to_skip, charcnt);
+      v |= ckd_mul (&product, leapcnt, 8);
       v |= ckd_add (&to_skip, to_skip, product);
-      v |= ckd_add (&to_skip, to_skip, num_isstd);
-      v |= ckd_add (&to_skip, to_skip, num_isgmt);
+      v |= ckd_add (&to_skip, to_skip, isstdcnt);
+      v |= ckd_add (&to_skip, to_skip, isutcnt);
       if (v)
 	goto lose;
 
@@ -271,15 +271,15 @@  __tzfile_read (const char *file)
     {
       off_t rem = st.st_size - f_offset, product;
       bool v = false;
-      v |= ckd_mul (&product, num_transitions, 8 + 1);
+      v |= ckd_mul (&product, timecnt, 8 + 1);
       v |= ckd_sub (&rem, rem, product);
-      v |= ckd_mul (&product, num_types, 6);
+      v |= ckd_mul (&product, typecnt, 6);
       v |= ckd_sub (&rem, rem, product);
-      v |= ckd_sub (&rem, rem, chars);
-      v |= ckd_mul (&product, num_leaps, 12);
+      v |= ckd_sub (&rem, rem, charcnt);
+      v |= ckd_mul (&product, leapcnt, 12);
       v |= ckd_sub (&rem, rem, product);
-      v |= ckd_sub (&rem, rem, num_isstd);
-      v |= ckd_sub (&rem, rem, num_isgmt);
+      v |= ckd_sub (&rem, rem, isstdcnt);
+      v |= ckd_sub (&rem, rem, isutcnt);
       v |= rem <= 1;
       v |= 3 <= rem && rem < sizeof "XYZ0\n";
       if (v)
@@ -292,11 +292,11 @@  __tzfile_read (const char *file)
   /* The file is parsed into a single heap allocation, comprising of
      the following arrays:
 
-     __time64_t transitions[num_transitions];
-     struct leap leaps[num_leaps];
-     struct ttinfo types[num_types];
-     unsigned char type_idxs[num_types];
-     char zone_names[chars];
+     __time64_t transitions[timecnt];
+     struct leap leaps[leapcnt];
+     struct ttinfo types[typecnt];
+     unsigned char type_idxs[typecnt];
+     char zone_names[charcnt];
      char tzspec[tzspec_size];
 
      The piece-wise allocations from buf below verify that no
@@ -314,13 +314,13 @@  __tzfile_read (const char *file)
   {
     size_t total_size, product;
     bool v = false;
-    v |= ckd_mul (&total_size, num_transitions, sizeof (__time64_t));
-    v |= ckd_mul (&product, num_leaps, sizeof (struct leap));
+    v |= ckd_mul (&total_size, timecnt, sizeof (__time64_t));
+    v |= ckd_mul (&product, leapcnt, sizeof (struct leap));
     v |= ckd_add (&total_size, total_size, product);
-    v |= ckd_mul (&product, num_types, sizeof (struct ttinfo));
+    v |= ckd_mul (&product, typecnt, sizeof (struct ttinfo));
     v |= ckd_add (&total_size, total_size, product);
-    v |= ckd_add (&total_size, total_size, num_transitions); /* type_idxs */
-    v |= ckd_add (&total_size, total_size, chars); /* zone_names */
+    v |= ckd_add (&total_size, total_size, timecnt); /* type_idxs */
+    v |= ckd_add (&total_size, total_size, charcnt); /* zone_names */
     v |= ckd_add (&total_size, total_size, tzspec_size);
     v |= ckd_add (&total_size, total_size, alignment_slop);
     if (v)
@@ -333,11 +333,11 @@  __tzfile_read (const char *file)
 
   /* The address of the first allocation is already stored in the
      pointer transitions.  */
-  (void) alloc_buffer_alloc_array (&buf, __time64_t, num_transitions);
-  leaps = alloc_buffer_alloc_array (&buf, struct leap, num_leaps);
-  types = alloc_buffer_alloc_array (&buf, struct ttinfo, num_types);
-  type_idxs = alloc_buffer_alloc_array (&buf, unsigned char, num_transitions);
-  zone_names = alloc_buffer_alloc_array (&buf, char, chars);
+  (void) alloc_buffer_alloc_array (&buf, __time64_t, timecnt);
+  leaps = alloc_buffer_alloc_array (&buf, struct leap, leapcnt);
+  types = alloc_buffer_alloc_array (&buf, struct ttinfo, typecnt);
+  type_idxs = alloc_buffer_alloc_array (&buf, unsigned char, timecnt);
+  zone_names = alloc_buffer_alloc_array (&buf, char, charcnt);
   if (trans_width == 8)
     tzspec = alloc_buffer_alloc_array (&buf, char, tzspec_size);
   else
@@ -347,7 +347,7 @@  __tzfile_read (const char *file)
 
   if (trans_width == 4)
     {
-      for (i = 0; i < num_transitions; i++)
+      for (i = 0; i < timecnt; i++)
 	{
 	  union { unsigned char c[4]; tzidx tzidx_aligned; } x;
 	  if (__fread_unlocked (&x, 1, 4, f) != 4)
@@ -359,10 +359,10 @@  __tzfile_read (const char *file)
     }
   else if (sizeof (int64_t) == 8)
     {
-      if (__fread_unlocked (transitions, 8, num_transitions, f)
-	  != num_transitions)
+      if (__fread_unlocked (transitions, 8, timecnt, f)
+	  != timecnt)
 	goto lose;
-      for (i = 0; i < num_transitions; i++)
+      for (i = 0; i < timecnt; i++)
 	{
 	  if (BYTE_ORDER != BIG_ENDIAN)
 	    transitions[i] = decode64 (&transitions[i]);
@@ -372,7 +372,7 @@  __tzfile_read (const char *file)
     }
   else
     {
-      for (i = 0; i < num_transitions; i++)
+      for (i = 0; i < timecnt; i++)
 	{
 	  union { unsigned char c[8]; int64_t int64_t_aligned; } x;
 	  if (__fread_unlocked (&x, 1, 8, f) != 8)
@@ -383,18 +383,18 @@  __tzfile_read (const char *file)
 	}
     }
 
-  if (__glibc_unlikely (__fread_unlocked (type_idxs, 1, num_transitions, f)
-			!= num_transitions))
+  if (__glibc_unlikely (__fread_unlocked (type_idxs, 1, timecnt, f)
+			!= timecnt))
     goto lose;
 
   /* Check for bogus indices in the data file, so we can hereafter
      safely use type_idxs[T] as indices into `types' and never crash.  */
-  for (i = 0; i < num_transitions; ++i)
-    if (__glibc_unlikely (type_idxs[i] >= num_types))
+  for (i = 0; i < timecnt; ++i)
+    if (__glibc_unlikely (type_idxs[i] >= typecnt))
       goto lose;
 
   unsigned char max_idx = 0;
-  for (i = 0; i < num_types; ++i)
+  for (i = 0; i < typecnt; ++i)
     {
       union { unsigned char c[4]; tzidx tzidx_aligned; } x;
       int c;
@@ -403,32 +403,33 @@  __tzfile_read (const char *file)
       c = __getc_unlocked (f);
       if (__glibc_unlikely (! (0 <= c && c <= 1)))
 	goto lose;
-      types[i].isdst = c;
+      types[i].dst = c;
       c = __getc_unlocked (f);
-      if (__glibc_unlikely (! (0 <= c && c < chars)))
+      if (__glibc_unlikely (! (0 <= c && c < charcnt)))
 	/* Bogus index in data file.  */
 	goto lose;
       if (max_idx < c)
 	max_idx = c;
       types[i].idx = c;
-      types[i].offset = decode (&x);
+      types[i].utoff = decode (&x);
 
       /* If long int is only 32 bits, reject offsets that cannot be negated.
          RFC 9636 section 3.2 allows this.  */
       long int negated_offset;
-      if (ckd_sub (&negated_offset, 0, types[i].offset))
+      if (ckd_sub (&negated_offset, 0, types[i].utoff))
 	goto lose;
     }
 
-  if (__glibc_unlikely (__fread_unlocked (zone_names, 1, chars, f) != chars))
+  if (__glibc_unlikely (__fread_unlocked (zone_names, 1, charcnt, f)
+			!= charcnt))
     goto lose;
-  if (__strnlen (zone_names + max_idx, chars - max_idx) == chars - max_idx)
+  if (__strnlen (zone_names + max_idx, charcnt - max_idx) == charcnt - max_idx)
     goto lose;
 
   int minimum_leap_gap = 2419199; /* See RFC 9636 section 3.2.  */
   __time64_t prevtransition = -minimum_leap_gap;
   int prevchange = 0;
-  for (i = 0; i < num_leaps; ++i)
+  for (i = 0; i < leapcnt; ++i)
     {
       {
 	union { char c[8]; tzidx tzidx_aligned; int64_t int64_t_aligned; } x;
@@ -436,45 +437,45 @@  __tzfile_read (const char *file)
 			       != trans_width),
 			      0))
 	  goto lose;
-	leaps[i].transition = trans_width == 4 ? decode (&x) : decode64 (&x);
-	if (leaps[i].transition < 0
-	    || leaps[i].transition - minimum_leap_gap < prevtransition)
+	leaps[i].occur = trans_width == 4 ? decode (&x) : decode64 (&x);
+	if (leaps[i].occur < 0
+	    || leaps[i].occur - minimum_leap_gap < prevtransition)
 	  goto lose;
-	prevtransition = leaps[i].transition;
+	prevtransition = leaps[i].occur;
       }
 
       {
 	union { unsigned char c[4]; tzidx tzidx_aligned; } x;
 	if (__glibc_unlikely (__fread_unlocked (&x, 1, 4, f) != 4))
 	  goto lose;
-	leaps[i].change = decode (&x);
+	leaps[i].corr = decode (&x);
 	int delta_change;
-	if (ckd_sub (&delta_change, leaps[i].change, prevchange)
+	if (ckd_sub (&delta_change, leaps[i].corr, prevchange)
 	    || ! (delta_change == 1 || delta_change == -1))
 	  goto lose;
-	prevchange = leaps[i].change;
+	prevchange = leaps[i].corr;
       }
     }
 
-  for (i = 0; i < num_isstd; ++i)
+  for (i = 0; i < isstdcnt; i++)
     {
       int c = __getc_unlocked (f);
       if (__glibc_unlikely (c == EOF))
 	goto lose;
       types[i].isstd = c != 0;
     }
-  while (i < num_types)
+  while (i < typecnt)
     types[i++].isstd = 0;
 
-  for (i = 0; i < num_isgmt; ++i)
+  for (i = 0; i < isutcnt; i++)
     {
       int c = __getc_unlocked (f);
       if (__glibc_unlikely (c == EOF))
 	goto lose;
-      types[i].isgmt = c != 0;
+      types[i].isutc = c != 0;
     }
-  while (i < num_types)
-    types[i++].isgmt = 0;
+  while (i < typecnt)
+    types[i++].isutc = 0;
 
   /* Read the POSIX TZ-style information if possible.  */
   if (tzspec != NULL)
@@ -498,7 +499,7 @@  __tzfile_read (const char *file)
   fclose (f);
 
   /* First "register" all time zone abbreviations.  */
-  for (i = 0; i < num_types; ++i)
+  for (i = 0; i < typecnt; ++i)
     if (__tzstring (&zone_names[types[i].idx]) == NULL)
       goto ret_free_transitions;
 
@@ -507,10 +508,10 @@  __tzfile_read (const char *file)
      transitioned to earliest in time.  */
   __tzname[0] = NULL;
   __tzname[1] = NULL;
-  for (i = num_transitions; i > 0; )
+  for (i = timecnt; i > 0; )
     {
       int type = type_idxs[--i];
-      int dst = types[type].isdst;
+      int dst = types[type].dst;
 
       if (__tzname[dst] == NULL)
 	{
@@ -533,20 +534,20 @@  __tzfile_read (const char *file)
     __tzname[1] = __tzname[0];
 
   daylight_saved = 0;
-  if (num_transitions == 0)
+  if (timecnt == 0)
     /* Use the first rule (which should also be the only one).  */
-    rule_stdoff = types[0].offset;
+    rule_stdoff = types[0].utoff;
   else
     {
       rule_stdoff = 0;
 
       /* Search for the last rule with a standard time offset.  This
 	 will be used for the global timezone variable.  */
-      i = num_transitions - 1;
+      i = timecnt - 1;
       do
-	if (!types[type_idxs[i]].isdst)
+	if (!types[type_idxs[i]].dst)
 	  {
-	    rule_stdoff = types[type_idxs[i]].offset;
+	    rule_stdoff = types[type_idxs[i]].utoff;
 	    break;
 	  }
 	else
@@ -557,7 +558,7 @@  __tzfile_read (const char *file)
 	 information will be used to set the global daylight
 	 variable.  */
       while (i-- > 0 && !daylight_saved)
-	daylight_saved = types[type_idxs[i]].isdst;
+	daylight_saved = types[type_idxs[i]].dst;
     }
 
   __daylight = daylight_saved;
@@ -588,13 +589,13 @@  __tzfile_compute (__time64_t timer, int use_localtime,
       __tzname[0] = NULL;
       __tzname[1] = NULL;
 
-      if (__glibc_unlikely (num_transitions == 0 || timer < transitions[0]))
+      if (__glibc_unlikely (timecnt == 0 || timer < transitions[0]))
 	{
 	  /* TIMER is before any transition (or there are no transitions).
 	     Choose the first non-DST type
 	     (or the first if they're all DST types).  */
 	  i = 0;
-	  while (i < num_types && types[i].isdst)
+	  while (i < typecnt && types[i].dst)
 	    {
 	      if (__tzname[1] == NULL)
 		__tzname[1] = __tzstring (&zone_names[types[i].idx]);
@@ -602,14 +603,14 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 	      ++i;
 	    }
 
-	  if (i == num_types)
+	  if (i == typecnt)
 	    i = 0;
 	  __tzname[0] = __tzstring (&zone_names[types[i].idx]);
 	  if (__tzname[1] == NULL)
 	    {
 	      tzidx j = i;
-	      while (j < num_types)
-		if (types[j].isdst)
+	      while (j < typecnt)
+		if (types[j].dst)
 		  {
 		    __tzname[1] = __tzstring (&zone_names[types[j].idx]);
 		    break;
@@ -618,12 +619,12 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 		  ++j;
 	    }
 	}
-      else if (timer >= transitions[num_transitions - 1])
+      else if (timer >= transitions[timecnt - 1])
 	{
 	  if (__glibc_unlikely (tzspec == NULL))
 	    {
 	    use_last:
-	      i = num_transitions;
+	      i = timecnt;
 	      goto found;
 	    }
 
@@ -645,18 +646,18 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 	  /* Find the first transition after TIMER, and
 	     then pick the type of the transition before it.  */
 	  tzidx lo = 0;
-	  tzidx hi = num_transitions - 1;
+	  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[num_transitions - 1], timer);
+	  ckd_sub (&tdiff, transitions[timecnt - 1], timer);
 	  ckd_add (&i, tdiff / 15778476, 0);
-	  if (i < num_transitions)
+	  if (i < timecnt)
 	    {
-	      i = num_transitions - 1 - i;
+	      i = timecnt - 1 - i;
 	      if (timer < transitions[i])
 		{
 		  if (i < 10 || timer >= transitions[i - 10])
@@ -670,7 +671,7 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 		}
 	      else
 		{
-		  if (num_transitions - i <= 10 || timer < transitions[i + 10])
+		  if (timecnt - i <= 10 || timer < transitions[i + 10])
 		    {
 		      /* Linear search.  */
 		      while (timer >= transitions[i])
@@ -695,14 +696,14 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 
 	found:
 	  /* assert (timer >= transitions[i - 1]
-	     && (i == num_transitions || timer < transitions[i])); */
-	  __tzname[types[type_idxs[i - 1]].isdst]
+	     && (i == timecnt || timer < transitions[i])); */
+	  __tzname[types[type_idxs[i - 1]].dst]
 	    = __tzstring (&zone_names[types[type_idxs[i - 1]].idx]);
 	  tzidx j = i;
-	  while (j < num_transitions)
+	  while (j < timecnt)
 	    {
 	      int type = type_idxs[j];
-	      int dst = types[type].isdst;
+	      int dst = types[type].dst;
 	      int idx = types[type].idx;
 
 	      if (__tzname[dst] == NULL)
@@ -730,16 +731,16 @@  __tzfile_compute (__time64_t timer, int use_localtime,
 	{
 	  /* This should only happen if there are no transition rules.
 	     In this case there should be only one single type.  */
-	  assert (num_types == 1);
+	  assert (typecnt == 1);
 	  __tzname[0] = __tzstring (zone_names);
 	}
       if (__tzname[1] == NULL)
 	/* There is no daylight saving time.  */
 	__tzname[1] = __tzname[0];
-      tp->tm_isdst = info->isdst;
+      tp->tm_isdst = info->dst;
       assert (strcmp (&zone_names[info->idx], __tzname[tp->tm_isdst]) == 0);
       tp->tm_zone = __tzname[tp->tm_isdst];
-      tp->tm_gmtoff = info->offset;
+      tp->tm_gmtoff = info->utoff;
     }
 
  leap:
@@ -747,17 +748,17 @@  __tzfile_compute (__time64_t timer, int use_localtime,
   *leap_hit = false;
 
   /* Find the last leap second correction transition time before TIMER.  */
-  i = num_leaps;
+  i = leapcnt;
   do
     if (i-- == 0)
       return;
-  while (timer < leaps[i].transition);
+  while (timer < leaps[i].occur);
 
   /* Apply its correction.  */
-  *leap_correct = leaps[i].change;
+  *leap_correct = leaps[i].corr;
 
-  if (timer == leaps[i].transition /* Exactly at the transition time.  */
-      && (leaps[i].change > (i == 0 ? 0 : leaps[i - 1].change)))
+  if (timer == leaps[i].occur /* Exactly at the transition time.  */
+      && (leaps[i].corr > (i == 0 ? 0 : leaps[i - 1].corr)))
     *leap_hit = true;
 }