From: Sebastien GODARD Date: Mon, 18 Jan 2016 17:36:18 +0000 (+0100) Subject: sar/sadf: Merge functions used to get timestamps X-Git-Tag: v11.3.1~32 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5f5fc79148f4419add710af2a20a1cdf98912484;p=sysstat sar/sadf: Merge functions used to get timestamps 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 --- diff --git a/sa.h b/sa.h index bd398ea..aa83b04 100644 --- 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 diff --git a/sa_common.c b/sa_common.c index 8fec32c..cb3afea 100644 --- a/sa_common.c +++ b/sa_common.c @@ -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 6816748..e2ac62e 100644 --- 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 ca152c6..2e8c6c9 100644 --- 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.