diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 0bcc8f41c9..bf574d32a5 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -348,10 +348,7 @@ static int output_timestamp_monotonic(FILE *f, sd_journal *j, const char *monoto static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, OutputFlags flags, const char *realtime) { char buf[MAX(FORMAT_TIMESTAMP_MAX, 64U)]; - struct tm *(*gettime_r)(const time_t *, struct tm *); - struct tm tm; uint64_t x; - time_t t; int r; assert(f); @@ -376,9 +373,9 @@ static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, Ou "Failed to format timestamp: %" PRIu64, x); } else { - char usec[7]; + struct tm tm; + time_t t; - gettime_r = (flags & OUTPUT_UTC) ? gmtime_r : localtime_r; t = (time_t) (x / USEC_PER_SEC); switch (mode) { @@ -388,24 +385,29 @@ static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, Ou break; case OUTPUT_SHORT_ISO: - if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t, &tm)) <= 0) + if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", + localtime_or_gmtime_r(&t, &tm, flags & OUTPUT_UTC)) <= 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format ISO time"); break; - case OUTPUT_SHORT_ISO_PRECISE: + case OUTPUT_SHORT_ISO_PRECISE: { + char usec[7]; + /* No usec in strftime, so we leave space and copy over */ - if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t, &tm)) <= 0) + if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", + localtime_or_gmtime_r(&t, &tm, flags & OUTPUT_UTC)) <= 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format ISO-precise time"); xsprintf(usec, "%06"PRI_USEC, x % USEC_PER_SEC); memcpy(buf + 20, usec, 6); break; - + } case OUTPUT_SHORT: case OUTPUT_SHORT_PRECISE: - if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", gettime_r(&t, &tm)) <= 0) + if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", + localtime_or_gmtime_r(&t, &tm, flags & OUTPUT_UTC)) <= 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format syslog time"); diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index cdc0938d5f..8bfcfd5cdc 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -763,10 +763,7 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error struct tm tm; /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */ - if (c->local_rtc) - localtime_r(&ts.tv_sec, &tm); - else - gmtime_r(&ts.tv_sec, &tm); + localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc); /* Override the main fields of struct tm, but not the timezone fields */ r = clock_get_hwclock(&tm); @@ -774,10 +771,7 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error log_debug_errno(r, "Failed to get hardware clock, ignoring: %m"); else { /* And set the system clock with this */ - if (c->local_rtc) - ts.tv_sec = mktime(&tm); - else - ts.tv_sec = timegm(&tm); + mktime_or_timegm(&tm, !c->local_rtc); if (clock_settime(CLOCK_REALTIME, &ts) < 0) log_debug_errno(errno, "Failed to update system clock, ignoring: %m"); @@ -787,10 +781,7 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error struct tm tm; /* Sync RTC from system clock */ - if (c->local_rtc) - localtime_r(&ts.tv_sec, &tm); - else - gmtime_r(&ts.tv_sec, &tm); + localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc); r = clock_set_hwclock(&tm); if (r < 0) @@ -884,10 +875,7 @@ static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *erro } /* Sync down to RTC */ - if (c->local_rtc) - localtime_r(&ts.tv_sec, &tm); - else - gmtime_r(&ts.tv_sec, &tm); + localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc); r = clock_set_hwclock(&tm); if (r < 0)