[PATCH 3/3] make -p now uses consistent timestamp format

2023-05-10 Thread Paul Eggert
* NEWS: mention this.
* src/main.c (safer_ctime, time_now): Remove.
(print_data_base): Use file_timestamp_sprintf to format timestamps.
---
 NEWS   |  5 +
 src/main.c | 51 ++-
 2 files changed, 11 insertions(+), 45 deletions(-)

diff --git a/NEWS b/NEWS
index d14fc568..b3a63d30 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,11 @@ which is contained in this distribution as the file 
doc/make.texi.
 See the README file and the GNU Make manual for instructions for
 reporting bugs.
 
+* 'make --print-data-base' (or 'make -p') now outputs time of day
+  using the same form as for file timestamps, e.g., "2023-05-10
+  10:43:57.570558743".  Previously it used the form "Wed May 10
+  10:43:57 2023", which has less detail and is harder to compare.
+
 
 Version 4.5 (26 Feb 2023)
 
diff --git a/src/main.c b/src/main.c
index 0f61b069..3240b91c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3720,57 +3720,18 @@ print_version (void)
   printed_version = 1;
 }
 
-/* Like ctime, except do not have undefined behavior with timestamps
-   out of ctime range.  */
-static char const *
-safer_ctime (time_t *t)
-{
-  struct tm *tm = localtime (t);
-  if (tm && -999 - 1900 <= tm->tm_year && tm->tm_year <=  - 1900)
-return ctime (t);
-  else
-return "(time out of range)\n";
-}
-
-static time_t
-time_now (void)
-{
-  /* Use an algorithm like file_timestamp_now's, extracting just the
- seconds part of the timestamp.  This avoids a race that would
- generate output that incorrectly makes it look like the system
- clock jumped backwards on platforms like GNU/Linux where the
- 'time' function does not use the CLOCK_REALTIME clock and the two
- clocks can disagree in their seconds component.  */
-#if FILE_TIMESTAMP_HI_RES
-# if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
-  {
-struct timespec timespec;
-if (clock_gettime (CLOCK_REALTIME, ×pec) == 0)
-  return timespec.tv_sec;
-  }
-# endif
-# if HAVE_GETTIMEOFDAY
-  {
-struct timeval timeval;
-if (gettimeofday (&timeval, 0) == 0)
-  return timeval.tv_sec;
-  }
-# endif
-#endif
-
-  return time ((time_t *) 0);
-}
-
 /* Print a bunch of information about this and that.  */
 
 static void
 print_data_base (void)
 {
-  time_t when = time_now ();
+  int resolution;
+  char buf[FILE_TIMESTAMP_PRINT_LEN_BOUND + 1];
+  file_timestamp_sprintf (buf, file_timestamp_now (&resolution));
 
   print_version ();
 
-  printf (_("\n# Make data base, printed on %s"), safer_ctime (&when));
+  printf (_("\n# Make data base, printed on %s\n"), buf);
 
   print_variable_data_base ();
   print_dir_data_base ();
@@ -3779,8 +3740,8 @@ print_data_base (void)
   print_vpath_data_base ();
   strcache_print_stats ("#");
 
-  when = time_now ();
-  printf (_("\n# Finished Make data base on %s\n"), safer_ctime (&when));
+  file_timestamp_sprintf (buf, file_timestamp_now (&resolution));
+  printf (_("\n# Finished Make data base on %s\n\n"), buf);
 }
 
 static void
-- 
2.39.2




[PATCH 1/3] make -p buffer overrun fix with outlandish current time

2023-05-10 Thread Paul Eggert
* src/main.c (safer_ctime): New function.
(print_data_base): Use it.
---
 src/main.c | 16 ++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index 8215ed78..76e392de 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3720,6 +3720,18 @@ print_version (void)
   printed_version = 1;
 }
 
+/* Like ctime, except do not have undefined behavior with timestamps
+   out of ctime range.  */
+static char const *
+safer_ctime (time_t *t)
+{
+  struct tm *tm = localtime (t);
+  if (tm && -999 - 1900 <= tm->tm_year && tm->tm_year <=  - 1900)
+return ctime (t);
+  else
+return "(time out of range)\n";
+}
+
 /* Print a bunch of information about this and that.  */
 
 static void
@@ -3729,7 +3741,7 @@ print_data_base (void)
 
   print_version ();
 
-  printf (_("\n# Make data base, printed on %s"), ctime (&when));
+  printf (_("\n# Make data base, printed on %s"), safer_ctime (&when));
 
   print_variable_data_base ();
   print_dir_data_base ();
@@ -3739,7 +3751,7 @@ print_data_base (void)
   strcache_print_stats ("#");
 
   when = time ((time_t *) 0);
-  printf (_("\n# Finished Make data base on %s\n"), ctime (&when));
+  printf (_("\n# Finished Make data base on %s\n"), safer_ctime (&when));
 }
 
 static void
-- 
2.39.2




[PATCH 2/3] make -p uses same clock as rest of 'make'

2023-05-10 Thread Paul Eggert
Without this patch, the output of 'make -p' would generate output that
sometimes incorrectly implied that the clock jumped backwards.
* src/main.c (time_now): New function.
(print_data_base): Use it.
---
 src/main.c | 33 +++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index 76e392de..0f61b069 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3732,12 +3732,41 @@ safer_ctime (time_t *t)
 return "(time out of range)\n";
 }
 
+static time_t
+time_now (void)
+{
+  /* Use an algorithm like file_timestamp_now's, extracting just the
+ seconds part of the timestamp.  This avoids a race that would
+ generate output that incorrectly makes it look like the system
+ clock jumped backwards on platforms like GNU/Linux where the
+ 'time' function does not use the CLOCK_REALTIME clock and the two
+ clocks can disagree in their seconds component.  */
+#if FILE_TIMESTAMP_HI_RES
+# if HAVE_CLOCK_GETTIME && defined CLOCK_REALTIME
+  {
+struct timespec timespec;
+if (clock_gettime (CLOCK_REALTIME, ×pec) == 0)
+  return timespec.tv_sec;
+  }
+# endif
+# if HAVE_GETTIMEOFDAY
+  {
+struct timeval timeval;
+if (gettimeofday (&timeval, 0) == 0)
+  return timeval.tv_sec;
+  }
+# endif
+#endif
+
+  return time ((time_t *) 0);
+}
+
 /* Print a bunch of information about this and that.  */
 
 static void
 print_data_base (void)
 {
-  time_t when = time ((time_t *) 0);
+  time_t when = time_now ();
 
   print_version ();
 
@@ -3750,7 +3779,7 @@ print_data_base (void)
   print_vpath_data_base ();
   strcache_print_stats ("#");
 
-  when = time ((time_t *) 0);
+  when = time_now ();
   printf (_("\n# Finished Make data base on %s\n"), safer_ctime (&when));
 }
 
-- 
2.39.2




[PATCH 0/3] fix clock skew and unlikely timestamp crashes

2023-05-10 Thread Paul Eggert
This is a followup to my earlier patch "Two 'make -p' timestamp
issues"  which
hasn't been incorporated yet into the master branch. Although that
patch was simple, it changes the output format of 'make -p' and I
assume that's why it's still languishing. To help make it easier to
review I have split it up into three patches, the combined effect of
which is the same as the original proposal.

Patch 0001 fixes a buffer overrun when the current time has a value
far in the past or future.

Patch 0002 fixes a clock skew race bug, in which the 'make' output can
make it look like the system clock jumped backwards, even though it
didn't.

Patch 0003 improves the output format of 'make -p' by having it use a
consistent form for timestamps, rather than one form for current time
(with less precision) and a different form for file timestamps.
This is the only part of the patch that possibly needs a NEWS entry,
as the other two patches are purely bug fixes.

Paul Eggert (3):
  make -p buffer overrun fix with outlandish current time
  make -p uses same clock as rest of 'make'
  make -p now uses consistent timestamp format

 NEWS   |  5 +
 src/main.c | 10 ++
 2 files changed, 11 insertions(+), 4 deletions(-)

--
2.39.2