Morning!
I'm trying to get a nanosecond timestamp (uint64_t) from a timeval struct.
struct timeval t;
// initialized so that ....
print("t sec: %ld, micro sec: %ld", t.tv_sec, t.tv_usec);
// .... prints "t sec: 4, micro sec: 130728"
long int sec = (long int) t.tv_sec;
// just did that because I thought that using time_t in calculations may cause errors
// print("%ld", sec) -> 4, so this seems to work as expected
uint64_t t_int = (sec * 1000000 + t.tv_usec) * 1000; // should be nanoseconds
print("t_int (uint64_t) %llu", t_int);
// prints "t_int (uint64_t) 18446744073545312320"
My timevals should definitely fit into uint64_t. Also, the cast to long int from time_t works, so all I'm doing is a simple calculation with two long ints, putting the result into a uint64_t, so I'm not sure why I'm getting an obviously wrong value. Any ideas? Is this just a wrong way of formatting (with %llu)?