From c11a0ee297e149b893c9f620626c2e1321a781fb Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sun, 18 Sep 2022 10:35:42 +0200 Subject: [PATCH] Merge get_locatime() and get_gmtime() functions Signed-off-by: Sebastien GODARD --- cifsiostat.c | 4 ++-- common.c | 45 +++++++++++++-------------------------------- common.h | 5 +++-- iostat.c | 4 ++-- mpstat.c | 4 ++-- pidstat.c | 4 ++-- tapestat.c | 4 ++-- 7 files changed, 26 insertions(+), 44 deletions(-) diff --git a/cifsiostat.c b/cifsiostat.c index 03be68a..b33fb37 100644 --- a/cifsiostat.c +++ b/cifsiostat.c @@ -461,7 +461,7 @@ void rw_io_stat_loop(long int count, struct tm *rectime) read_cifs_stat(curr); /* Get time */ - get_localtime(rectime, 0); + get_xtime(rectime, 0, LOCAL_TIME); /* Print results */ write_stats(curr, rectime); @@ -601,7 +601,7 @@ int main(int argc, char **argv) /* How many processors on this machine? */ cpu_nr = get_cpu_nr(~0, FALSE); - get_localtime(&rectime, 0); + get_xtime(&rectime, 0, LOCAL_TIME); /* * Don't buffer data if redirected to a pipe. diff --git a/common.c b/common.c index b92be22..81c7762 100644 --- a/common.c +++ b/common.c @@ -87,50 +87,34 @@ void print_version(void) /* *************************************************************************** - * Get local date and time. + * Get date and time, expressed in UTC or in local time. * * IN: * @d_off Day offset (number of days to go back in the past). + * @utc TRUE if date and time shall be expressed in UTC. * * OUT: * @rectime Current local date and time. * * RETURNS: - * Value of time in seconds since the Epoch. + * Value of time in seconds since the Epoch (always in UTC) *************************************************************************** */ -time_t get_localtime(struct tm *rectime, int d_off) +time_t get_xtime(struct tm *rectime, int d_off, int utc) { time_t timer; timer = __time(NULL); timer -= SEC_PER_DAY * d_off; - localtime_r(&timer, rectime); - - return timer; -} -/* - *************************************************************************** - * Get date and time expressed in UTC. - * - * IN: - * @d_off Day offset (number of days to go back in the past). - * - * OUT: - * @rectime Current date and time expressed in UTC. - * - * RETURNS: - * Value of time in seconds since the Epoch. - *************************************************************************** - */ -time_t get_gmtime(struct tm *rectime, int d_off) -{ - time_t timer; - - timer = __time(NULL); - timer -= SEC_PER_DAY * d_off; - gmtime_r(&timer, rectime); + if (utc) { + /* Get date and time in UTC */ + gmtime_r(&timer, rectime); + } + else { + /* Get date and time in local time */ + localtime_r(&timer, rectime); + } return timer; } @@ -162,10 +146,7 @@ time_t get_time(struct tm *rectime, int d_off) utc++; } - if (utc == 2) - return get_gmtime(rectime, d_off); - else - return get_localtime(rectime, d_off); + return get_xtime(rectime, d_off, utc == 2); } #ifdef USE_NLS diff --git a/common.h b/common.h index 7efc7ca..55b6657 100644 --- a/common.h +++ b/common.h @@ -102,6 +102,7 @@ #define IGNORE_VIRTUAL_DEVICES FALSE #define ACCEPT_VIRTUAL_DEVICES TRUE +#define LOCAL_TIME FALSE /* Environment variables */ #define ENV_TIME_FMT "S_TIME_FORMAT" @@ -241,8 +242,8 @@ void get_HZ (void); void get_kb_shift (void); -time_t get_localtime - (struct tm *, int); +time_t get_xtime + (struct tm *, int, int); time_t get_time (struct tm *, int); void init_nls diff --git a/iostat.c b/iostat.c index 03f5623..b490bc1 100644 --- a/iostat.c +++ b/iostat.c @@ -2013,7 +2013,7 @@ void rw_io_stat_loop(long int count, struct tm *rectime) } /* Get time */ - get_localtime(rectime, 0); + get_xtime(rectime, 0, LOCAL_TIME); /* Print results */ write_stats(curr, rectime, skip); @@ -2379,7 +2379,7 @@ int main(int argc, char **argv) /* How many processors on this machine? */ cpu_nr = get_cpu_nr(~0, FALSE); - get_localtime(&rectime, 0); + get_xtime(&rectime, 0, LOCAL_TIME); /* * Don't buffer data if redirected to a pipe. diff --git a/mpstat.c b/mpstat.c index d48f076..1032410 100644 --- a/mpstat.c +++ b/mpstat.c @@ -2069,7 +2069,7 @@ void rw_mpstat_loop(int dis_hdr, int rows) memset(st_cpu[curr], 0, STATS_CPU_SIZE * (cpu_nr + 1)); /* Get time */ - get_localtime(&(mp_tstamp[curr]), 0); + get_xtime(&(mp_tstamp[curr]), 0, LOCAL_TIME); /* Read uptime and CPU stats */ read_uptime(&(uptime_cs[curr])); @@ -2379,7 +2379,7 @@ int main(int argc, char **argv) } /* Get time */ - get_localtime(&(mp_tstamp[0]), 0); + get_xtime(&(mp_tstamp[0]), 0, LOCAL_TIME); /* * Don't buffer data if redirected to a pipe. diff --git a/pidstat.c b/pidstat.c index 4026e27..7e302d5 100644 --- a/pidstat.c +++ b/pidstat.c @@ -2414,7 +2414,7 @@ void rw_pidstat_loop(int dis_hdr, int rows) set_pid_nonexistent(pid_list); /* Get time */ - get_localtime(&ps_tstamp[curr], 0); + get_xtime(&ps_tstamp[curr], 0, LOCAL_TIME); /* Read system uptime (in 1/100th of a second) */ read_uptime(&(uptime_cs[curr])); @@ -2809,7 +2809,7 @@ int main(int argc, char **argv) } /* Get time */ - get_localtime(&(ps_tstamp[0]), 0); + get_xtime(&(ps_tstamp[0]), 0, LOCAL_TIME); /* * Don't buffer data if redirected to a pipe. diff --git a/tapestat.c b/tapestat.c index e3d57ad..e0f2769 100644 --- a/tapestat.c +++ b/tapestat.c @@ -569,7 +569,7 @@ void rw_tape_stat_loop(long int count, struct tm *rectime) } /* Get time */ - get_localtime(rectime, 0); + get_xtime(rectime, 0, LOCAL_TIME); /* Check whether we should skip first report */ if (!skip) { @@ -699,7 +699,7 @@ int main(int argc, char **argv) tape_initialise(); - get_localtime(&rectime, 0); + get_xtime(&rectime, 0, LOCAL_TIME); /* * Don't buffer data if redirected to a pipe. -- 2.40.0