Skip to content

Commit 636fed7

Browse files
committed
fdsdump: dont assume we are on 64bit architecture when formatting time
1 parent 85009a8 commit 636fed7

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

src/tools/fdsdump/src/aggregator/print.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <sys/socket.h>
1616

1717
#include <cctype>
18+
#include <cinttypes>
1819
#include <iostream>
1920

2021
namespace fdsdump {
@@ -61,7 +62,6 @@ std::string
6162
datetime_to_str(uint64_t ts_millisecs)
6263
{
6364
char buffer[128];
64-
static_assert(sizeof(uint64_t) == sizeof(time_t), "Assumed that time_t is uint64_t, but it's not");
6565

6666
uint64_t secs = ts_millisecs / 1000;
6767
uint64_t msecs_part = ts_millisecs % 1000;
@@ -71,13 +71,12 @@ datetime_to_str(uint64_t ts_millisecs)
7171
msecs_part *= 10;
7272
}
7373

74+
time_t t_secs = std::min<time_t>(secs, std::numeric_limits<time_t>::max());
7475
tm tm;
75-
localtime_r(reinterpret_cast<time_t *>(&secs), &tm);
76-
//TODO: The format should probably be configrable
77-
//TODO: Have a look at the hard coded buffer size
76+
localtime_r(&t_secs, &tm);
7877
std::size_t n = strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm);
7978
assert(n > 0);
80-
snprintf(&buffer[n], sizeof(buffer) - n, ".%03lu", msecs_part);
79+
snprintf(&buffer[n], sizeof(buffer) - n, ".%03" PRIu64, msecs_part);
8180

8281
return buffer;
8382
}

0 commit comments

Comments
 (0)