Skip to content

Commit

Permalink
fmt/time: add fmt_timestamp_us
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Feb 7, 2024
1 parent 94805b9 commit cc51c8f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/re_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
40 changes: 40 additions & 0 deletions src/fmt/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit cc51c8f

Please sign in to comment.