将std::chrono::system_clock::time_点转换为struct timeval并返回 我正在编写一个C++代码,它需要访问一个旧的C库,它使用TimeVall作为当前时间的表示。
在旧软件包中获取我们使用的当前日期/时间:将std::chrono::system_clock::time_点转换为struct timeval并返回 我正在编写一个C++代码,它需要访问一个旧的C库,它使用TimeVall作为当前时间的表示。,c++,c,c++11,C++,C,C++11,在旧软件包中获取我们使用的当前日期/时间: struct timeval dateTime; gettimeofday(&dateTime, NULL); function(dateTime); // The function will do its task 现在我需要使用C++计时,比如: system_clock::time_point now = system_clock::now(); struct timeval dateTime; dateTi
struct timeval dateTime;
gettimeofday(&dateTime, NULL);
function(dateTime); // The function will do its task
现在我需要使用C++计时,比如:
system_clock::time_point now = system_clock::now();
struct timeval dateTime;
dateTime.tv_sec = ???? // Help appreaciated here
dateTime.tv_usec = ???? // Help appreaciated here
function(dateTime);
稍后在代码中,我需要返回,从返回的struct timeval
构建一个time\u点
变量:
struct timeval dateTime;
function(&dateTime);
system_clock::time_point returnedDateTime = ?? // Help appreacited
我正在使用C++11 请参见
std::chrono::system_clock::to_time_t()
,它将时间点
转换为时间
,成为您的电视秒
。你没有得到电视,你可以把它设置为0;或者你可以摆弄一些其他的东西,包括持续时间\u投射
,以便从你的时间点
中提取几分之一秒
from_time_t
()则相反。[编辑为使用time_val
而不是自由变量]
假设您相信您的系统时钟的精确度为毫秒,您可以这样做:
struct timeval dest;
auto now=std::chrono::system_clock::now();
auto millisecs=
std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()
);;
dest.tv_sec=millisecs.count()/1000;
dest.tv_usec=(millisecs.count()%1000)*1000;
std::cout << "s:" << dest.tv_sec << " usec:" << dest.tv_usec << std::endl;
timeval
make_timeval(time_t s, long us)
{
timeval tv;
tv.tv_sec = s;
tv.tv_usec = us;
return tv;
}
bool
operator==(timeval x, timeval y)
{
return x.tv_sec == y.tv_sec && x.tv_usec == y.tv_usec;
}
int
main()
{
using namespace std::chrono;
assert(make_timeval(0, 0) == to_timeval(system_clock::time_point{}));
assert(make_timeval(1, 0) == to_timeval(system_clock::time_point{seconds{1}}));
assert(make_timeval(1, 400000) == to_timeval(system_clock::time_point{seconds{1} + microseconds{400000}}));
assert(make_timeval(-1, 400000) == to_timeval(system_clock::time_point{seconds{-1} + microseconds{400000}}));
assert(to_time_point(make_timeval(0, 0)) == system_clock::time_point{});
assert(to_time_point(make_timeval(1, 0)) == system_clock::time_point{seconds{1}});
assert(to_time_point(make_timeval(1, 400000)) == system_clock::time_point{seconds{1} + microseconds{400000}});
assert(to_time_point(make_timeval(-1, 400000)) == system_clock::time_point{seconds{-1} + microseconds{400000}});
}
以下是如何在不使用手动换算系数的情况下进行换算,或根据时间的未指定舍入模式进行换算:
timeval
to_timeval(std::chrono::system_clock::time_point tp)
{
using namespace std::chrono;
auto s = time_point_cast<seconds>(tp);
if (s > tp)
s = s - seconds{1};
auto us = duration_cast<microseconds>(tp - s);
timeval tv;
tv.tv_sec = s.time_since_epoch().count();
tv.tv_usec = us.count();
return tv;
}
std::chrono::system_clock::time_point
to_time_point(timeval tv)
{
using namespace std::chrono;
return system_clock::time_point{seconds{tv.tv_sec} + microseconds{tv.tv_usec}};
}
这都是基于假设timeval
和system_clock
的历元是相同的。这没有指定,但对所有现有实现都是如此。如果幸运的话,我们可以在不久的将来将这种现有做法标准化
请注意,在POSIX中,timeval
既用作时间点
又用作持续时间
。因此,如果timeval
当前表示持续时间,则to_time_point
可能会导致运行时错误。如果客户端将结果解释为一个持续时间,to_timeval
可能会导致运行时错误。可能应该将dateTime.tv_sec
和dateTime.tv_usec
分配给timeval
结构,因为目标是以有效的timeval
结构结束代码>和dest.tv\u usec=毫秒计数()%1000l当然不需要代码>。一个小问题。time\u point\u cast
仅对纪元之后的tp
值进行舍入。对于历元time\u point\u cast
四舍五入之前的值。使用C++17或更高版本,可以将time\u point\u cast
和该if
的组合替换为一个简单的:auto s=floor(tp)代码>。
timeval
make_timeval(time_t s, long us)
{
timeval tv;
tv.tv_sec = s;
tv.tv_usec = us;
return tv;
}
bool
operator==(timeval x, timeval y)
{
return x.tv_sec == y.tv_sec && x.tv_usec == y.tv_usec;
}
int
main()
{
using namespace std::chrono;
assert(make_timeval(0, 0) == to_timeval(system_clock::time_point{}));
assert(make_timeval(1, 0) == to_timeval(system_clock::time_point{seconds{1}}));
assert(make_timeval(1, 400000) == to_timeval(system_clock::time_point{seconds{1} + microseconds{400000}}));
assert(make_timeval(-1, 400000) == to_timeval(system_clock::time_point{seconds{-1} + microseconds{400000}}));
assert(to_time_point(make_timeval(0, 0)) == system_clock::time_point{});
assert(to_time_point(make_timeval(1, 0)) == system_clock::time_point{seconds{1}});
assert(to_time_point(make_timeval(1, 400000)) == system_clock::time_point{seconds{1} + microseconds{400000}});
assert(to_time_point(make_timeval(-1, 400000)) == system_clock::time_point{seconds{-1} + microseconds{400000}});
}