我正在编写一个需要访问旧C库的C++代码,该库使用timeval作为当前时间的表示.
在旧包中获取我们使用的当前日期/时间:
struct timeval dateTime;
gettimeofday(&dateTime, NULL);
function(dateTime); // The function will do its task
Run Code Online (Sandbox Code Playgroud)
现在我需要使用C++ chrono,例如:
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);
Run Code Online (Sandbox Code Playgroud)
稍后在代码中我需要返回的方法,time_point从返回的变量构建变量struct timeval:
struct timeval dateTime;
function(&dateTime);
system_clock::time_point returnedDateTime = ?? // Help appreacited
Run Code Online (Sandbox Code Playgroud)
我在使用C++ 11.
[编辑使用time_val而不是免费的vars]
假设你信任system_clock毫秒精度,你可以这样:
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;
Run Code Online (Sandbox Code Playgroud)
使用std::chrono::microseconds中duration_cast,并相应地调整你的(DIV/MOD)代号为更高的精度-照顾你有多少信任你获得值的精确度.
转换回来是:
timeval src;
// again, trusting the value with only milliseconds accuracy
using dest_timepoint_type=std::chrono::time_point<
std::chrono::system_clock, std::chrono::milliseconds
>;
dest_timepoint_type converted{
std::chrono::milliseconds{
src.tv_sec*1000+src.tv_usec/1000
}
};
// this is to make sure the converted timepoint is indistinguishable by one
// issued by the system_clock
std::chrono::system_clock::time_point recovered =
std::chrono::time_point_cast<std::chrono::system_clock::duration>(converted)
;
Run Code Online (Sandbox Code Playgroud)
以下是如何在不使用手动转换因子或取决于 的未指定舍入模式的情况下进行转换time_t:
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}};
}
Run Code Online (Sandbox Code Playgroud)
to_timeval注意向下舍入tp(如果是负数)。POSIX 规范对此有点模糊,但我假设它timeval表示纪元之前的时间点,先是负值tv_sec,然后是正值tv_usec。然后就是一个简单的操作来查找microseconds自上次以来的second。
如果我的假设不正确(并且可以找到更精确的 POSIX 规范),<chrono>则有能力对它所做的任何事情进行建模。
假设上述约定,逆向转换的可读性非常好。它不需要任何评论。
这都可以这样测试:
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}});
}
Run Code Online (Sandbox Code Playgroud)
这一切都是基于以下假设: 和 的纪元timeval相同system_clock。这没有指定,但对于所有现有的实现都是如此。如果幸运的话,我们可以在不久的将来标准化这种现有的做法。
请注意,在 POSIX 中,timeval它同时用作 atime_point和 a duration。因此,如果当前表示持续时间,to_time_point则可能会导致运行时错误。如果客户端将结果解释为持续时间,timeval则可能会导致运行时错误。to_timeval
| 归档时间: |
|
| 查看次数: |
4510 次 |
| 最近记录: |