From cc51c8f47e21e6f13ea5ee21934719f16a37a180 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Wed, 7 Feb 2024 13:24:41 +0100 Subject: [PATCH] fmt/time: add fmt_timestamp_us --- include/re_fmt.h | 1 + src/fmt/time.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/re_fmt.h b/include/re_fmt.h index 245e10218..630f98f50 100644 --- a/include/re_fmt.h +++ b/include/re_fmt.h @@ -181,6 +181,7 @@ static inline bool str_isset(const char *s) /* time */ int fmt_gmtime(struct re_printf *pf, void *ts); int fmt_timestamp(struct re_printf *pf, void *ts); +int fmt_timestamp_us(struct re_printf *pf, void *arg); int fmt_human_time(struct re_printf *pf, const uint32_t *seconds); diff --git a/src/fmt/time.c b/src/fmt/time.c index a0001fd89..758cd7b97 100644 --- a/src/fmt/time.c +++ b/src/fmt/time.c @@ -129,3 +129,43 @@ int fmt_timestamp(struct re_printf *pf, void *arg) return re_hprintf(pf, "%02u:%02u:%02u.%03llu", h, m, s, ms); } + + +/** + * Print local time stamp including microseconds relative to user's timezone + * + * @param pf Print function for output + * @param arg Not used + * + * @return 0 if success, otherwise errorcode + */ +int fmt_timestamp_us(struct re_printf *pf, void *arg) +{ + int h, m, s; + uint64_t us; +#ifdef WIN32 + SYSTEMTIME st; + + GetSystemTime(&st); + + h = st.wHour; + m = st.wMinute; + s = st.wSecond; + us = st.wMilliseconds; +#else + struct timespec tspec; + struct tm tm; + + (void)clock_gettime(CLOCK_REALTIME, &tspec); + if (!localtime_r(&tspec.tv_sec, &tm)) + return EINVAL; + + h = tm.tm_hour; + m = tm.tm_min; + s = tm.tm_sec; + us = tspec.tv_nsec / 1000; +#endif + (void)arg; + + return re_hprintf(pf, "%02u:%02u:%02u.%06llu", h, m, s, us); +}