c++ - 给定一个微秒值,如何准确得到一个timeval结构?/*UPDATE GETTING NEGATIVE VALUE*/

标签 c++ timestamp double rounding

在我的应用程序中,这是转换微秒并将其表示为时间间隔的正确方法吗?我需要使用 struct timeval,上面的代码是将双微秒转换为 timeval 的例程的一部分

此代码中的负值

//更新

#include <iostream>
#include <sys/time.h> 
#include <stdint.h>

void convert(uint64_t offset , struct timeval t)
{

std::cout <<  "currentTimeOffset " << offset / 1000000 << "startTimeOffset " << t.tv_sec  << std::endl;
t.tv_usec =  offset % 1000000L;
std::cout <<  "stattime usec " <<  t.tv_usec << std::endl ;


}

int main(int argc , char** argv)
{

struct timeval test;

uint64_t t = 18446744073709551615;
convert(t , test);

return 0;
}

最佳答案

类似于:

timeval to_timeval(double usec) {
    return { usec / 1000000, usec % 1000000 };
}

关于c++ - 给定一个微秒值,如何准确得到一个timeval结构?/*UPDATE GETTING NEGATIVE VALUE*/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899733/

相关文章:

c++ - 如何将两个类成员函数与同一个类中的另一个函数一起添加?

c++ - 无法与 libiberty 链接

postgresql - 仅选择今天(从午夜开始)的时间戳

java - 从 0.5 开始按正常方式从 Double 到 int 舍入

java - 问 : Rental Car Calculator | Use a sentinel value loop

c++ - double 值的精度错误

c++ - 为什么要这样使用 malloc?

c++ - 如何修复我的 strcat 函数中的错误

mysql - 为什么 'timestamp not null' 导致默认设置为 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' ?

powershell - 在powershell中将日期转换为unix时间戳