]> granicus.if.org Git - sysstat/commitdiff
sar/sadf: Merge functions used to get timestamps
authorSebastien GODARD <sysstat@users.noreply.github.com>
Mon, 18 Jan 2016 17:36:18 +0000 (18:36 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Mon, 18 Jan 2016 17:36:18 +0000 (18:36 +0100)
sadf code refactoring: Merge functions used to get and save timestamps.

Before:
sar: sar_get_record_timestamp_struct()
sadf: sadf_get_record_timestamp_struct()

After:
sa_get_record_timestamp_struct()

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
sa.h
sa_common.c
sadf.c
sar.c

diff --git a/sa.h b/sa.h
index bd398eadaa3857139081919f15bc65754b1fc2bc..aa83b04aa1fa6648697a2ee0a0a39d2d159c3736 100644 (file)
--- a/sa.h
+++ b/sa.h
@@ -949,6 +949,9 @@ extern void
        replace_nonprintable_char(int, char *);
 extern int
        sa_fread(int, void *, int, int);
+extern int
+       sa_get_record_timestamp_struct(unsigned int, struct record_header *,
+                                      struct tm *, struct tm *);
 extern int
        sa_open_read_magic(int *, char *, struct file_magic *, int);
 extern void
index 8fec32cce18a5a4e21368f9952cc457892c6266e..cb3afeacfa6cd36f0d5987a2bf1f363abcb0c1df 100644 (file)
@@ -2112,3 +2112,82 @@ void replace_nonprintable_char(int ifd, char *comment)
                        comment[i] = '.';
        }
 }
+
+/*
+ ***************************************************************************
+ * Fill the rectime and loctime structures with current record's date and
+ * time, based on current record's "number of seconds since the epoch" saved
+ * in file.
+ * For loctime (if given): The timestamp is expressed in local time.
+ * For rectime: The timestamp is expressed in UTC, in local time, or in the
+ * time of the file's creator depending on options entered by the user on the
+ * command line.
+ *
+ * IN:
+ * @l_flags    Flags indicating the type of time expected by the user.
+ *             S_F_LOCAL_TIME means time should be expressed in local time.
+ *             S_F_TRUE_TIME means time should be expressed in time of
+ *             file's creator.
+ *             Default is time expressed in UTC (except for sar, where it
+ *             is local time).
+ * @record_hdr Record header containing the number of seconds since the
+ *             epoch, and the HH:MM:SS of the file's creator.
+ *
+ * OUT:
+ * @rectime    Structure where timestamp for current record has been saved
+ *             (in local time or in UTC depending on options used).
+ * @loctime    Structure where timestamp for current record has been saved
+ *             (expressed in local time). This field will be used for time
+ *             comparison if options -s and/or -e have been used.
+ *
+ * RETURNS:
+ * 1 if an error was detected, or 0 otherwise.
+ ***************************************************************************
+*/
+int sa_get_record_timestamp_struct(unsigned int l_flags, struct record_header *record_hdr,
+                                  struct tm *rectime, struct tm *loctime)
+{
+       struct tm *ltm = NULL;
+       int rc = 0;
+
+       /* Fill localtime structure if given */
+       if (loctime) {
+               if ((ltm = localtime((const time_t *) &(record_hdr->ust_time))) != NULL) {
+                       *loctime = *ltm;
+               }
+               else {
+                       rc = 1;
+               }
+       }
+
+       /* Fill generic rectime structure */
+       if (PRINT_LOCAL_TIME(l_flags) && !ltm) {
+               /* Get local time if not already done */
+               ltm = localtime((const time_t *) &(record_hdr->ust_time));
+       }
+
+       if (!PRINT_LOCAL_TIME(l_flags) && !PRINT_TRUE_TIME(l_flags)) {
+               /*
+                * Get time in UTC
+                * (the user doesn't want local time nor time of file's creator).
+                */
+               ltm = gmtime((const time_t *) &(record_hdr->ust_time));
+       }
+
+       if (ltm) {
+               /* Done even in true time mode so that we have some default values */
+               *rectime = *ltm;
+       }
+       else {
+               rc = 1;
+       }
+
+       if (PRINT_TRUE_TIME(l_flags)) {
+               /* Time of file's creator */
+               rectime->tm_hour = record_hdr->hour;
+               rectime->tm_min  = record_hdr->minute;
+               rectime->tm_sec  = record_hdr->second;
+       }
+
+       return rc;
+}
diff --git a/sadf.c b/sadf.c
index 6816748158d3301dd7424b458acc53c626cd980a..e2ac62ec89bee18e87bb782c4479def953a0a3d9 100644 (file)
--- a/sadf.c
+++ b/sadf.c
@@ -176,55 +176,6 @@ void check_format_options(void)
        }
 }
 
-/*
- ***************************************************************************
- * Fill the rectime and loctime structures with current record's date and
- * time, based on current record's "number of seconds since the epoch" saved
- * in file.
- * The resulting timestamp is expressed in UTC or in local time, depending
- * on whether options -T or -t have been used or not.
- *
- * IN:
- * @curr       Index in array for current sample statistics.
- * @rectime    Structure where timestamp (expressed in local time or in UTC
- *             depending on whether options -T or -t have been used or not)
- *             can be saved for current record.
- * @loctime    Structure where timestamp (expressed in local time) can be
- *             saved for current record.
- *
- * OUT:
- * @rectime    Structure where timestamp for current record has been saved
- *             (in local time or in UTC depending on options used).
- * @loctime    Structure where timestamp for current record has been saved
- *             (expressed in local time). This field will be used for time
- *             comparison if options -s and/or -e have been used.
- ***************************************************************************
-*/
-void sadf_get_record_timestamp_struct(int curr, struct tm *rectime, struct tm *loctime)
-{
-       struct tm *ltm;
-
-       if ((ltm = localtime((const time_t *) &record_hdr[curr].ust_time)) != NULL) {
-               *loctime = *ltm;
-       }
-
-       if (!PRINT_LOCAL_TIME(flags) && !PRINT_TRUE_TIME(flags)) {
-               /* Options -T and -t not used: Display timestamp in UTC */
-               ltm = gmtime((const time_t *) &record_hdr[curr].ust_time);
-       }
-
-       if (ltm) {
-               *rectime = *ltm;
-       }
-
-       if (PRINT_TRUE_TIME(flags)) {
-               /* Option -t */
-               rectime->tm_hour = record_hdr[curr].hour;
-               rectime->tm_min  = record_hdr[curr].minute;
-               rectime->tm_sec  = record_hdr[curr].second;
-       }
-}
-
 /*
  ***************************************************************************
  * Set current record's timestamp strings (date and time). This timestamp is
@@ -357,7 +308,7 @@ void print_special_record(int curr, int use_tm_start, int use_tm_end, int rtype,
        unsigned int new_cpu_nr;
 
        /* Fill timestamp structure (rectime) for current record */
-       sadf_get_record_timestamp_struct(curr, rectime, loctime);
+       sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime, loctime);
 
        /* The record must be in the interval specified by -s/-e options */
        if ((use_tm_start && (datecmp(loctime, &tm_start) < 0)) ||
@@ -485,7 +436,7 @@ int read_next_sample(int ifd, int action, int curr, char *file, int *rtype, int
                         */
                        read_file_stat_bunch(act, curr, ifd, file_hdr.sa_act_nr,
                                             file_actlst);
-                       sadf_get_record_timestamp_struct(curr, rectime, loctime);
+                       sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime, loctime);
                }
        }
 
diff --git a/sar.c b/sar.c
index ca152c643d1e4cefbaf0a9292d960c05a9081286..2e8c6c91aaa94af88fbc3fa68e3dcfd55ab664f8 100644 (file)
--- a/sar.c
+++ b/sar.c
@@ -288,46 +288,6 @@ void reverse_check_act(unsigned int act_nr)
        }
 }
 
-/*
- ***************************************************************************
- * Fill the (struct tm) rectime structure with current record's time,
- * based on current record's time data saved in file.
- * The resulting timestamp is expressed in the locale of the file creator
- * or in the user's own locale depending on whether option -t has been used
- * or not.
- *
- * IN:
- * @curr       Index in array for current sample statistics.
- *
- * RETURNS:
- * 1 if an error was detected, or 0 otherwise.
- ***************************************************************************
-*/
-int sar_get_record_timestamp_struct(int curr)
-{
-       struct tm *ltm;
-
-       /* Check if option -t was specified on the command line */
-       if (PRINT_TRUE_TIME(flags)) {
-               /* -t */
-               rectime.tm_hour = record_hdr[curr].hour;
-               rectime.tm_min  = record_hdr[curr].minute;
-               rectime.tm_sec  = record_hdr[curr].second;
-       }
-       else {
-               if ((ltm = localtime((const time_t *) &record_hdr[curr].ust_time)) == NULL)
-                       /*
-                        * An error was detected.
-                        * The rectime structure has NOT been updated.
-                        */
-                       return 1;
-
-               rectime = *ltm;
-       }
-
-       return 0;
-}
-
 /*
  ***************************************************************************
  * Determine if a stat header line has to be displayed.
@@ -382,7 +342,8 @@ int check_line_hdr(void)
 int set_record_timestamp_string(int curr, char *cur_time, int len)
 {
        /* Fill timestamp structure */
-       if (sar_get_record_timestamp_struct(curr))
+       if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME, &record_hdr[curr],
+                                          &rectime, NULL))
                /* Error detected */
                return 1;
 
@@ -994,7 +955,9 @@ void read_stats_from_file(char from_file[])
                                 */
                                read_file_stat_bunch(act, 0, ifd, file_hdr.sa_act_nr,
                                                     file_actlst);
-                               if (sar_get_record_timestamp_struct(0))
+                               if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME,
+                                                                  &record_hdr[0],
+                                                                  &rectime, NULL))
                                        /*
                                         * An error was detected.
                                         * The timestamp hasn't been updated.