From 3d31fddbc34990bc42de6f89cd06afc168ba4d85 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sun, 26 Mar 2017 11:44:31 +0200 Subject: [PATCH] Create a separate function to get report's date Signed-off-by: Sebastien GODARD --- common.c | 44 ++++++++++++++++++++++++++++++++++---------- common.h | 2 ++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/common.c b/common.c index d6f156d..dfed735 100644 --- a/common.c +++ b/common.c @@ -415,6 +415,39 @@ void xprintf(int nr_tab, const char *fmtf, ...) printf("%s\n", buf); } +/* + *************************************************************************** + * Get report date as a string of characters. + * + * IN: + * @rectime Date to display (don't use time fields). + * @cur_date String where date will be saved. + * @sz Max size of cur_date string. + * + * OUT: + * @cur_date Date (string format). + * + * RETURNS: + * TRUE if S_TIME_FORMAT is set to ISO, or FALSE otherwise. + *************************************************************************** + */ +int set_report_date(struct tm *rectime, char cur_date[], int sz) +{ + if (rectime == NULL) { + strncpy(cur_date, "?/?/?", sz); + cur_date[sz - 1] = '\0'; + } + else if (is_iso_time_fmt()) { + strftime(cur_date, sz, "%Y-%m-%d", rectime); + return 1; + } + else { + strftime(cur_date, sz, "%x", rectime); + } + + return 0; +} + /* *************************************************************************** * Print banner. @@ -439,16 +472,7 @@ int print_gal_header(struct tm *rectime, char *sysname, char *release, char cur_date[TIMESTAMP_LEN]; int rc = 0; - if (rectime == NULL) { - strcpy(cur_date, "?/?/?"); - } - else if (is_iso_time_fmt()) { - strftime(cur_date, sizeof(cur_date), "%Y-%m-%d", rectime); - rc = 1; - } - else { - strftime(cur_date, sizeof(cur_date), "%x", rectime); - } + rc = set_report_date(rectime, cur_date, sizeof(cur_date)); if (format == PLAIN_OUTPUT) { /* Plain output */ diff --git a/common.h b/common.h index 04226a3..70736f7 100644 --- a/common.h +++ b/common.h @@ -284,6 +284,8 @@ int print_gal_header (struct tm *, char *, char *, char *, char *, int, int); void print_version (void); +int set_report_date + (struct tm *, char[], int); char *strtolower (char *); void sysstat_panic -- 2.40.0