diff --git a/src/util/src/ttime.c b/src/util/src/ttime.c index 7fb9738ec75ba722bc4d69ae872d5b4a0994e6ff..6f67c4a1365abeb175447fcb56237d3cfa44fbee 100644 --- a/src/util/src/ttime.c +++ b/src/util/src/ttime.c @@ -374,3 +374,34 @@ int32_t getTimestampInUsFromStr(char* token, int32_t tokenlen, int64_t* ts) { return getTimestampInUsFromStrImpl(timestamp, token[tokenlen - 1], ts); } + +// internal function, when program is paused in debugger, +// one can call this function from debugger to print a +// timestamp as human readable string, for example (gdb): +// p fmtts(1593769722) +// outputs: +// 2020-07-03 17:48:42 +// and the parameter can also be a variable. +const char* fmtts(int64_t ts) { + static char buf[32]; + + time_t tt; + if (ts > -62135625943 && ts < 32503651200) { + tt = ts; + } else if (ts > -62135625943000 && ts < 32503651200000) { + tt = ts / 1000; + } else { + tt = ts / 1000000; + } + + struct tm* ptm = localtime(&tt); + size_t pos = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ptm); + + if (ts <= -62135625943000 || ts >= 32503651200000) { + sprintf(buf + pos, ".%06d", (int)(ts % 1000000)); + } else if (ts <= -62135625943 || ts >= 32503651200) { + sprintf(buf + pos, ".%03d", (int)(ts % 1000)); + } + + return buf; +} \ No newline at end of file