1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-02-11 09:51:56 +02:00

maint: factor out the common show_date functionality

* 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.
This commit is contained in:
Nikolay Nechaev
2024-05-05 12:06:18 +03:00
committed by Pádraig Brady
parent ecf7d12937
commit 292cf89545
6 changed files with 64 additions and 48 deletions

View 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

View File

@@ -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;
}

View File

@@ -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);

View 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

36
src/show-date.c Normal file
View File

@@ -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;
}
}

1
src/show-date.h Normal file
View File

@@ -0,0 +1 @@
bool show_date (char const *format, struct timespec when, timezone_t tz);