]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
parse-datetime: debug mktime failures better
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 9 Jun 2025 06:51:25 +0000 (23:51 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 9 Jun 2025 06:51:52 +0000 (23:51 -0700)
* lib/parse-datetime.y (debug_mktime_not_ok):
Handle mktime failures too.
Don’t pretend that the time was normalized if mktime failed.
Don’t attempt to deduce DST shifts; it’s impractical in general
and even a reasonable heuristic is too painful.

ChangeLog
lib/parse-datetime.y

index ef6c6d19be18e5f91e78a7ec57fab6f10eb62dc9..c0eed85a660e96e327083fb9af807f7ca5fb0676 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2025-06-08  Paul Eggert  <eggert@cs.ucla.edu>
+
+       parse-datetime: debug mktime failures better
+       * lib/parse-datetime.y (debug_mktime_not_ok):
+       Handle mktime failures too.
+       Don’t pretend that the time was normalized if mktime failed.
+       Don’t attempt to deduce DST shifts; it’s impractical in general
+       and even a reasonable heuristic is too painful.
+
 2025-06-07  Bruno Haible  <bruno@clisp.org>
 
        stdcountof-h: Work around a clang bug.
index 9ffd0993a61e20c0005fd7d2fa90b1ebfb200ab1..c120eae04a9d9f0534b4501452dc8c2aa68f632d 100644 (file)
@@ -1686,18 +1686,7 @@ static void
 debug_mktime_not_ok (struct tm const *tm0, struct tm const *tm1,
                      parser_control const *pc, bool time_zone_seen)
 {
-  /* TODO: handle t == -1 (as in 'mktime_ok').  */
   char tmp[DBGBUFSIZE];
-  int i;
-  const bool eq_sec   = (tm0->tm_sec  == tm1->tm_sec);
-  const bool eq_min   = (tm0->tm_min  == tm1->tm_min);
-  const bool eq_hour  = (tm0->tm_hour == tm1->tm_hour);
-  const bool eq_mday  = (tm0->tm_mday == tm1->tm_mday);
-  const bool eq_month = (tm0->tm_mon  == tm1->tm_mon);
-  const bool eq_year  = (tm0->tm_year == tm1->tm_year);
-
-  const bool dst_shift = eq_sec && eq_min && !eq_hour
-                         && eq_mday && eq_month && eq_year;
 
   if (!debugging (pc))
     return;
@@ -1705,35 +1694,33 @@ debug_mktime_not_ok (struct tm const *tm0, struct tm const *tm1,
   dbg_fputs (_("error: invalid date/time value:\n"));
   dbg_printf (_("    user provided time: '%s'\n"),
               debug_strfdatetime (tm0, pc, tmp, sizeof tmp));
-  dbg_printf (_("       normalized time: '%s'\n"),
-              debug_strfdatetime (tm1, pc, tmp, sizeof tmp));
-  /* The format must be aligned with debug_strfdatetime and the two
-     DEBUG statements above.  This string is not translated.  */
-  i = snprintf (tmp, sizeof tmp,
-                "                                 %4s %2s %2s %2s %2s %2s",
-                eq_year ? "" : "----",
-                eq_month ? "" : "--",
-                eq_mday ? "" : "--",
-                eq_hour ? "" : "--",
-                eq_min ? "" : "--",
-                eq_sec ? "" : "--");
-  /* Trim trailing whitespace.  */
-  if (0 <= i)
+  bool mktime_failed = tm1->tm_wday < 0;
+  if (mktime_failed)
+    dbg_fputs (_("    time could not be normalized\n"));
+  else
     {
-      if (sizeof tmp - 1 < i)
-        i = sizeof tmp - 1;
+      dbg_printf (_("       normalized time: '%s'\n"),
+                  debug_strfdatetime (tm1, pc, tmp, sizeof tmp));
+      /* The format must be aligned with debug_strfdatetime and the
+         dbg_printf statements above.  This string is not translated.  */
+      int i = sprintf (tmp, "%37s %2s %2s %2s %2s %2s",
+                       tm0->tm_year == tm1->tm_year ? "" : "----",
+                       tm0->tm_mon  == tm1->tm_mon  ? "" : "--",
+                       tm0->tm_mday == tm1->tm_mday ? "" : "--",
+                       tm0->tm_hour == tm1->tm_hour ? "" : "--",
+                       tm0->tm_min  == tm1->tm_min  ? "" : "--",
+                       tm0->tm_sec  == tm1->tm_sec  ? "" : "--");
+      /* Trim trailing whitespace.  */
       while (0 < i && tmp[i - 1] == ' ')
-        --i;
-      tmp[i] = '\0';
+        i--;
+      dbg_printf ("%.*s\n", i, tmp);
     }
-  dbg_printf ("%s\n", tmp);
 
   dbg_fputs (_("     possible reasons:\n"));
-  if (dst_shift)
-    dbg_fputs (_("       nonexistent due to daylight-saving time;\n"));
-  if (!eq_mday && !eq_month)
-    dbg_fputs (_("       invalid day/month combination;\n"));
-  dbg_fputs (_("       numeric values overflow;\n"));
+  dbg_fputs (_("       nonexistent due to daylight-saving time;\n"));
+  dbg_fputs (_("       invalid day/month combination;\n"));
+  if (mktime_failed)
+    dbg_fputs (_("       numeric values overflow;\n"));
   dbg_printf ("       %s\n", (time_zone_seen ? _("incorrect timezone")
                               : _("missing timezone")));
 }