Merge pull request #2547 from 0xAX/generalize-format-timestamp-internal
time-util: merge format_timestamp_internal() and format_timestamp_int…
This commit is contained in:
commit
41c24512cc
|
@ -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 a new issue