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>
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
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;
+}
}
}
-/*
- ***************************************************************************
- * 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
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)) ||
*/
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);
}
}
}
}
-/*
- ***************************************************************************
- * 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.
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;
*/
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.