]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: factor out the common show_date functionality
authorNikolay Nechaev <Nikolay_Nechaev@mail.ru>
Sun, 5 May 2024 09:06:18 +0000 (12:06 +0300)
committerPádraig Brady <P@draigBrady.com>
Sun, 5 May 2024 16:50:14 +0000 (17:50 +0100)
* src/show-date.{h,c}: Declaration and definition of show_date.
* src/du.c: Wse the common show_date instead of the previous local
function.
* src/date.c: Wse the common show_date via a wrapper show_date_helper.
* src/local.mk: Corresponding adjustments.

po/POTFILES.in
src/date.c
src/du.c
src/local.mk
src/show-date.c [new file with mode: 0644]
src/show-date.h [new file with mode: 0644]

index 35c819ac2f12776b3928ef0e6e44251d24a60e5a..d824499c50fca704ce18bb628e41da30ce7edcd4 100644 (file)
@@ -110,6 +110,7 @@ src/rmdir.c
 src/runcon.c
 src/seq.c
 src/set-fields.c
+src/show-date.c
 src/shred.c
 src/shuf.c
 src/sleep.c
index 03bf012e2cac94308f612c84241a8bf13658794c..ee32403ef2dc4d81b0ec35516792b9fe52738591 100644 (file)
 #include "parse-datetime.h"
 #include "posixtm.h"
 #include "quote.h"
+#include "show-date.h"
 #include "stat-time.h"
-#include "fprintftime.h"
 
 /* The official name of this program (e.g., no 'g' prefix).  */
 #define PROGRAM_NAME "date"
 
 #define AUTHORS proper_name ("David MacKenzie")
 
-static bool show_date (char const *, struct timespec, timezone_t);
+static bool show_date_helper (char const *, struct timespec, timezone_t);
 
 enum Time_spec
 {
@@ -381,7 +381,7 @@ batch_convert (char const *input_filename, char const *format,
         }
       else
         {
-          ok &= show_date (format, when, tz);
+          ok &= show_date_helper (format, when, tz);
         }
     }
 
@@ -643,38 +643,26 @@ main (int argc, char **argv)
             }
         }
 
-      ok &= show_date (format_res, when, tz);
+      ok &= show_date_helper (format_res, when, tz);
     }
 
   main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
-/* Display the date and/or time in WHEN according to the format specified
-   in FORMAT, followed by a newline.  Return true if successful.  */
-
 static bool
-show_date (char const *format, struct timespec when, timezone_t tz)
+show_date_helper (char const *format, struct timespec when, timezone_t tz)
 {
-  struct tm tm;
-
   if (parse_datetime_flags & PARSE_DATETIME_DEBUG)
     error (0, 0, _("output format: %s"), quote (format));
 
-  if (localtime_rz (tz, &when.tv_sec, &tm))
-    {
-      if (format == rfc_email_format)
-        setlocale (LC_TIME, "C");
-      fprintftime (stdout, format, &tm, tz, when.tv_nsec);
-      if (format == rfc_email_format)
-        setlocale (LC_TIME, "");
-      fputc ('\n', stdout);
-      return true;
-    }
-  else
-    {
-      char buf[INT_BUFSIZE_BOUND (intmax_t)];
-      error (0, 0, _("time %s is out of range"),
-             quote (timetostr (when.tv_sec, buf)));
-      return false;
-    }
+  if (format == rfc_email_format)
+    setlocale (LC_TIME, "C");
+
+  bool ok = show_date (format, when, tz);
+
+  if (format == rfc_email_format)
+    setlocale (LC_TIME, "");
+
+  putchar ('\n');
+  return ok;
 }
index 023a11022ebe95ad61f60a573deb632281a6b043..81ec1bec8979c80d931a500a04674e7f259c8924 100644 (file)
--- a/src/du.c
+++ b/src/du.c
 #include "assure.h"
 #include "di-set.h"
 #include "exclude.h"
-#include "fprintftime.h"
 #include "human.h"
 #include "mountlist.h"
 #include "quote.h"
+#include "show-date.h"
 #include "stat-size.h"
 #include "stat-time.h"
 #include "stdio--.h"
@@ -370,25 +370,6 @@ hash_ins (struct di_set *di_set, ino_t ino, dev_t dev)
   return inserted;
 }
 
-/* FIXME: this code is nearly identical to code in date.c  */
-/* Display the date and time in WHEN according to the format specified
-   in FORMAT.  */
-
-static void
-show_date (char const *format, struct timespec when, timezone_t tz)
-{
-  struct tm tm;
-  if (localtime_rz (tz, &when.tv_sec, &tm))
-    fprintftime (stdout, format, &tm, tz, when.tv_nsec);
-  else
-    {
-      char buf[INT_BUFSIZE_BOUND (intmax_t)];
-      char *when_str = timetostr (when.tv_sec, buf);
-      error (0, 0, _("time %s is out of range"), quote (when_str));
-      fputs (when_str, stdout);
-    }
-}
-
 /* Print N_BYTES.  Convert it to a readable value before printing.  */
 
 static void
@@ -414,7 +395,13 @@ print_size (const struct duinfo *pdui, char const *string)
   if (opt_time)
     {
       putchar ('\t');
-      show_date (time_format, pdui->tmax, localtz);
+      bool ok = show_date (time_format, pdui->tmax, localtz);
+      if (!ok)
+        {
+          /* If failed to format date, print raw seconds instead.  */
+          char buf[INT_BUFSIZE_BOUND (intmax_t)];
+          fputs (timetostr (pdui->tmax.tv_sec, buf), stdout);
+        }
     }
   printf ("\t%s%c", string, opt_nul_terminate_output ? '\0' : '\n');
   fflush (stdout);
index afae90753fbbf1b27ac6aade5b8d083d72061fa2..ee377d9335316b2865d052dfd5d2f51d403ea1f1 100644 (file)
@@ -58,6 +58,7 @@ noinst_HEADERS =              \
   src/prog-fprintf.h           \
   src/remove.h                 \
   src/set-fields.h             \
+  src/show-date.h              \
   src/statx.h                  \
   src/system.h                 \
   src/temp-stream.h            \
@@ -372,7 +373,9 @@ nodist_src_coreutils_SOURCES = src/coreutils.h
 src_coreutils_SOURCES = src/coreutils.c
 
 src_cp_SOURCES = src/cp.c $(copy_sources) $(selinux_sources)
+src_date_SOURCES = src/date.c src/show-date.c
 src_dir_SOURCES = src/ls.c src/ls-dir.c
+src_du_SOURCES = src/du.c src/show-date.c
 src_env_SOURCES = src/env.c src/operand2sig.c
 src_vdir_SOURCES = src/ls.c src/ls-vdir.c
 src_id_SOURCES = src/id.c src/group-list.c
diff --git a/src/show-date.c b/src/show-date.c
new file mode 100644 (file)
index 0000000..ffa3d02
--- /dev/null
@@ -0,0 +1,36 @@
+#include <config.h>
+#include <stdio.h>
+
+#include "system.h"
+#include "fprintftime.h"
+#include "parse-datetime.h"
+#include "quote.h"
+#include "show-date.h"
+#include "stat-time.h"
+
+/* Display the date and/or time in WHEN according to the format specified
+   in FORMAT, followed by a newline.
+
+   If successful, return true.
+   If unsuccessful, prints an error message to STDERR and returns false.
+   If unsuccessful and ON_ERROR_PRINT_UNFORMATTED, also prints WHEN.TV_SEC
+   to STDOUT.  */
+
+extern bool
+show_date (char const *format, struct timespec when, timezone_t tz)
+{
+  struct tm tm;
+
+  if (localtime_rz (tz, &when.tv_sec, &tm))
+    {
+      fprintftime (stdout, format, &tm, tz, when.tv_nsec);
+      return true;
+    }
+  else
+    {
+      char buf[INT_BUFSIZE_BOUND (intmax_t)];
+      error (0, 0, _("time %s is out of range"),
+             quote (timetostr (when.tv_sec, buf)));
+      return false;
+    }
+}
diff --git a/src/show-date.h b/src/show-date.h
new file mode 100644 (file)
index 0000000..a965fa1
--- /dev/null
@@ -0,0 +1 @@
+bool show_date (char const *format, struct timespec when, timezone_t tz);