C - 在struct timeval和uint32_t之间转换以计算经过的时间

时间:2017-10-21 23:31:31

标签: c endianness uint32 timeval

我在stackoverflow上阅读了很多关于网络字节顺序的帖子 例如,如果我这样做:

struct timeval start_t
gettimeofday(&start_t, NULL);

现在,我怎么能确定这种演员会有效?

uint32_t receivedTime = someGetterSomewhere();
struct timeval result = (struct timeval) receivedTime;
struct timeval diffTime;
timersub(&start_t,&result,&diffTime);


// do stuff like timersub, etc to get ms elapsed time 

int calculatedTime =  (int) floor( ((diffTime.tv_sec) * 1000 + (diffTime.tv_usec) / 1000) + 0.5);

感谢您的回答

PS:

我的Gcc说不允许这样做

error: aggregate value used where an integer was expected
         pkt_set_timestamp(emptyPacket, (const uint32_t) start_t);

0 个答案:

没有答案