mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-05 09:17:44 +03:00
time-util: merge format_timestamp_internal() and format_timestamp_internal_us()
The time_util.c provides format_timestamp_internal() and format_timestamp_internal_us() functions for a timestamp formating. Both functions are very similar and differ only in formats handling. We can add additional boolean parameter to the format_timestamp_internal() function which will represent is a format for us timestamp or not. This allows us to get rid of format_timestamp_internal_us() that is prevent code duplication. We can remove format_timestamp_internal_us() safely, because it is static and has no users outside of the time_util.c. New fourth parameter will be passed inside of the format_timestamp(), format_timestamp_us() and etc, functions, but the public API is not changed.
This commit is contained in:
parent
ef9fde5378
commit
0056086af6
@ -206,9 +206,11 @@ struct timeval *timeval_store(struct timeval *tv, usec_t u) {
|
||||
return tv;
|
||||
}
|
||||
|
||||
static char *format_timestamp_internal(char *buf, size_t l, usec_t t, bool utc) {
|
||||
static char *format_timestamp_internal(char *buf, size_t l, usec_t t,
|
||||
bool utc, bool us) {
|
||||
struct tm tm;
|
||||
time_t sec;
|
||||
int k;
|
||||
|
||||
assert(buf);
|
||||
assert(l > 0);
|
||||
@ -219,48 +221,36 @@ static char *format_timestamp_internal(char *buf, size_t l, usec_t t, bool utc)
|
||||
sec = (time_t) (t / USEC_PER_SEC);
|
||||
localtime_or_gmtime_r(&sec, &tm, utc);
|
||||
|
||||
if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm) <= 0)
|
||||
if (us)
|
||||
k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm);
|
||||
else
|
||||
k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm);
|
||||
|
||||
if (k <= 0)
|
||||
return NULL;
|
||||
if (us) {
|
||||
snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC));
|
||||
if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *format_timestamp(char *buf, size_t l, usec_t t) {
|
||||
return format_timestamp_internal(buf, l, t, false);
|
||||
return format_timestamp_internal(buf, l, t, false, false);
|
||||
}
|
||||
|
||||
char *format_timestamp_utc(char *buf, size_t l, usec_t t) {
|
||||
return format_timestamp_internal(buf, l, t, true);
|
||||
}
|
||||
|
||||
static char *format_timestamp_internal_us(char *buf, size_t l, usec_t t, bool utc) {
|
||||
struct tm tm;
|
||||
time_t sec;
|
||||
|
||||
assert(buf);
|
||||
assert(l > 0);
|
||||
|
||||
if (t <= 0 || t == USEC_INFINITY)
|
||||
return NULL;
|
||||
|
||||
sec = (time_t) (t / USEC_PER_SEC);
|
||||
localtime_or_gmtime_r(&sec, &tm, utc);
|
||||
|
||||
if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm) <= 0)
|
||||
return NULL;
|
||||
snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC));
|
||||
if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
|
||||
return NULL;
|
||||
|
||||
return buf;
|
||||
return format_timestamp_internal(buf, l, t, true, false);
|
||||
}
|
||||
|
||||
char *format_timestamp_us(char *buf, size_t l, usec_t t) {
|
||||
return format_timestamp_internal_us(buf, l, t, false);
|
||||
return format_timestamp_internal(buf, l, t, false, true);
|
||||
}
|
||||
|
||||
char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) {
|
||||
return format_timestamp_internal_us(buf, l, t, true);
|
||||
return format_timestamp_internal(buf, l, t, true, true);
|
||||
}
|
||||
|
||||
char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
|
||||
|
Loading…
Reference in New Issue
Block a user