X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;ds=sidebyside;f=src%2Fbasic%2Ftime-util.c;h=98ec92918697f0c6600abe0ba150d67dbf4dbe64;hb=66ecc207e203db5434610395cd04c40ae8727b58;hp=047d2a66d042f03979de9c82dc9b694e360404fb;hpb=4f8a2329ef626675895974d53a6afad68a0d255a;p=elogind.git diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 047d2a66d..98ec92918 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -1,5 +1,3 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - /*** This file is part of systemd. @@ -19,20 +17,32 @@ along with systemd; If not, see . ***/ +#include +#include +#include #include +#include +#include #include #include +#include +#include #include "alloc-util.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "log.h" +#include "macro.h" #include "parse-util.h" #include "path-util.h" #include "string-util.h" #include "strv.h" #include "time-util.h" -#include "util.h" + +#if 0 /// UNNEEDED by elogind +static nsec_t timespec_load_nsec(const struct timespec *ts); +#endif // 0 usec_t now(clockid_t clock_id) { struct timespec ts; @@ -127,7 +137,8 @@ usec_t timespec_load(const struct timespec *ts) { (usec_t) ts->tv_nsec / NSEC_PER_USEC; } -nsec_t timespec_load_nsec(const struct timespec *ts) { +#if 0 /// UNNEEDED by elogind +static nsec_t timespec_load_nsec(const struct timespec *ts) { assert(ts); if (ts->tv_sec == (time_t) -1 && @@ -138,6 +149,7 @@ nsec_t timespec_load_nsec(const struct timespec *ts) { (nsec_t) ts->tv_sec * NSEC_PER_SEC + (nsec_t) ts->tv_nsec; } +#endif // 0 struct timespec *timespec_store(struct timespec *ts, usec_t u) { assert(ts); @@ -183,9 +195,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); @@ -196,51 +210,39 @@ 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); } #if 0 /// UNNEEDED by elogind char *format_timestamp_utc(char *buf, size_t l, usec_t t) { - return format_timestamp_internal(buf, l, t, true); + return format_timestamp_internal(buf, l, t, true, false); } #endif // 0 -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; -} - 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); } #if 0 /// UNNEEDED by elogind 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); } #endif // 0