[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] maint: factor out the common show_date functionality
From: |
Nikolay Nechaev |
Subject: |
[PATCH] maint: factor out the common show_date functionality |
Date: |
Sun, 5 May 2024 12:06:18 +0300 |
* src/show-date.{h,c}: declaration and definition of show_date
* src/du.c: use the common show_date instead of the previous local
function.
* src/date.c: use the common show_date via a wrapper show_date_helper.
* src/local.mk: corresponding adjustments
---
src/date.c | 42 +++++++++++++++---------------------------
src/du.c | 29 ++++++++---------------------
src/local.mk | 3 +++
src/show-date.c | 36 ++++++++++++++++++++++++++++++++++++
src/show-date.h | 1 +
5 files changed, 63 insertions(+), 48 deletions(-)
create mode 100644 src/show-date.c
create mode 100644 src/show-date.h
diff --git a/src/date.c b/src/date.c
index 03bf012..8bf92ce 100644
--- a/src/date.c
+++ b/src/date.c
@@ -29,15 +29,15 @@
#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;
}
diff --git a/src/du.c b/src/du.c
index 023a110..9180373 100644
--- a/src/du.c
+++ b/src/du.c
@@ -32,10 +32,10 @@
#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);
diff --git a/src/local.mk b/src/local.mk
index afae907..ee377d9 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -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
index 0000000..ffa3d02
--- /dev/null
+++ b/src/show-date.c
@@ -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
index 0000000..a965fa1
--- /dev/null
+++ b/src/show-date.h
@@ -0,0 +1 @@
+bool show_date (char const *format, struct timespec when, timezone_t tz);
--
2.45.0
- [PATCH] maint: factor out the common show_date functionality,
Nikolay Nechaev <=