C++ C++;如何将std::chrono::time_点转换为long和back

C++ C++;如何将std::chrono::time_点转换为long和back,c++,c++11,chrono,C++,C++11,Chrono,我需要将std::chrono::time_point转换为long类型(整数64位)。我开始使用std::chrono 这是我的密码: int main () { std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now(); auto epoch = now.time_since_epoch(); auto value = std::c

我需要将
std::chrono::time_point
转换为
long
类型(整数64位)。我开始使用
std::chrono

这是我的密码:

int main ()
{
     std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();

    auto epoch = now.time_since_epoch();
    auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch);
    long duration = value.count();


    std::chrono::duration<long> dur(duration);

    std::chrono::time_point<std::chrono::system_clock> dt(dur);

    if (dt != now)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
int main()
{
std::chrono::time_point now=std::chrono::system_clock::now();
自动历元=现在。自历元()以来的时间;
自动值=std::chrono::duration\u cast(历元);
长持续时间=value.count();
标准::时间::持续时间dur(持续时间);
标准:时间:时间点dt(dur);
如果(dt!=现在)
std::cout仅支持其他
时间点
持续时间
对象的算法

您需要将
long
转换为指定的单位数,然后您的代码才能正常工作

std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
由于您希望以
毫秒
的精度进行通信,因此最好在
时间点进行转换:

auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
epoch
现在的类型为
std::chrono::millides
。下一条语句本质上变成了no-op(只做一个拷贝,不做转换):

在您和我的代码中,
duration
保存自
系统时钟时代以来的
毫秒数

这:

这一行:

std::chrono::time_point<std::chrono::system_clock> dt(dur);
因为
dt
持有的是
毫秒的整数
,而
现在的
持有的是比
毫秒
更精细的整数倍的滴答声(例如
微秒
纳秒
),因此只有在
系统时钟::now()
返回测试通过时的整数
毫秒

但你可以:

if (dt != now_ms)
现在,您将可靠地获得预期结果

总而言之:

int main ()
{
    auto now = std::chrono::system_clock::now();
    auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);

    auto value = now_ms.time_since_epoch();
    long duration = value.count();

    std::chrono::milliseconds dur(duration);

    std::chrono::time_point<std::chrono::system_clock> dt(dur);

    if (dt != now_ms)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
将可靠地输出:

Success.
最后,我建议消除临时变量,以将
time\u point
和integral类型之间的代码转换减少到最低限度。这些转换是危险的,因此操作裸整数类型的代码越少越好:

int main ()
{
    using namespace std::chrono;
    // Get current time with precision of milliseconds
    auto now = time_point_cast<milliseconds>(system_clock::now());
    // sys_milliseconds is type time_point<system_clock, milliseconds>
    using sys_milliseconds = decltype(now);
    // Convert time_point to signed integral type
    auto integral_duration = now.time_since_epoch().count();
    // Convert signed integral type to time_point
    sys_milliseconds dt{milliseconds{integral_duration}};
    // test
    if (dt != now)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
这降低了风险,只需确保在退出时使用
sys\u毫秒
,在返回时在两个地方使用

还有一个例子:假设您想转换成一个积分,该积分表示系统时钟所支持的任何持续时间(微秒、10微秒或纳秒)。那么您就不必像上面那样指定毫秒。代码简化为:

int main ()
{
    using namespace std::chrono;
    // Get current time with native precision
    auto now = system_clock::now();
    // Convert time_point to signed integral type
    auto integral_duration = now.time_since_epoch().count();
    // Convert signed integral type to time_point
    system_clock::time_point dt{system_clock::duration{integral_duration}};
    // test
    if (dt != now)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
int main()
{
使用名称空间std::chrono;
//以本机精度获取当前时间
自动现在=系统时钟::现在();
//将时间点转换为有符号整数类型
自动积分\u持续时间=现在。时间\u自\u epoch()开始。计数();
//将有符号整数类型转换为时间点
系统时钟:时间点dt{system时钟:持续时间{积分持续时间};
//试验
如果(dt!=现在)

std::cout我还想指出,有两种方法可以获得时间点的毫秒数。我不确定哪一种更好,我已经对它们进行了基准测试,它们都有相同的性能,所以我想这是一个偏好问题。也许霍华德可以插话:

auto now = system_clock::now();

//Cast the time point to ms, then get its duration, then get the duration's count.
auto ms = time_point_cast<milliseconds>(now).time_since_epoch().count();

//Get the time point's duration, then cast to ms, then get its count.
auto ms = duration_cast<milliseconds>(tpBid.time_since_epoch()).count();
auto now=system_clock::now();
//将时间点强制转换为毫秒,然后获取其持续时间,然后获取持续时间的计数。
自动毫秒=时间点施法(现在)。时间自纪元()开始。计数();
//获取时间点的持续时间,然后强制转换为毫秒,然后获取其计数。
自动毫秒=持续时间(tpBid.time_自_epoch()).count();
第一条从左到右在我脑海中读得更清楚。

作为一行:

long value_ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch()).count();
long value_ms=std::chrono::duration_cast(std::chrono::time_point_cast(std::chrono::high_resolution_clock::now())。time_since_epoch())。count();

你能举个例子吗?提供的两个链接都有一个“例子”我让它编译,但有一些逻辑错误…我编辑了原始代码…包含示例将有利于答案。链接会消失或更改。尽管如此,我不知道为什么会被否决。我对chrono库不太熟悉,但我相信您必须使用
std::chrono::dur
d即使这样,也可能会出现舍入错误(
std::chrono::system_clock
的分辨率可能高于毫秒)@MikeMB无论如何,新硬件上的所有时钟的精度都应该在微秒/亚微秒左右。请参阅一篇标题为的文章。实际上,windows通常会在
系统时钟上具有更高的精度(大约是后者的100倍),但即使这通常也是亚微秒。@BrentNash:小心,我可能会告诉你这一点。-)我将在Cppcon 2015()上谈论这一点:。这个主题与我上面的回答中的
时间点(now)
密切相关。只有持续时间是courser:
时间点(now)。最后一个注释:粗,不是Curs.虽然很好的回答。英语的问题是它不能用C++编译器来解析,编译器可以做一个更好的拼写检查。-这是非常有用的。尽管我认为如果你没有使用“Auto'”,它会是一个更好的标签。只是为了像我这样的懒惰的人。我们想看看哪些类型被操纵,而不必去看其他地方。非常感谢@HowardHinnant
if (dt != now_ms)
int main ()
{
    auto now = std::chrono::system_clock::now();
    auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);

    auto value = now_ms.time_since_epoch();
    long duration = value.count();

    std::chrono::milliseconds dur(duration);

    std::chrono::time_point<std::chrono::system_clock> dt(dur);

    if (dt != now_ms)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
int main ()
{
    using namespace std::chrono;
    auto now = system_clock::now();
    auto now_ms = time_point_cast<milliseconds>(now);

    auto value = now_ms.time_since_epoch();
    long duration = value.count();

    milliseconds dur(duration);

    time_point<system_clock> dt(dur);

    if (dt != now_ms)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
Success.
int main ()
{
    using namespace std::chrono;
    // Get current time with precision of milliseconds
    auto now = time_point_cast<milliseconds>(system_clock::now());
    // sys_milliseconds is type time_point<system_clock, milliseconds>
    using sys_milliseconds = decltype(now);
    // Convert time_point to signed integral type
    auto integral_duration = now.time_since_epoch().count();
    // Convert signed integral type to time_point
    sys_milliseconds dt{milliseconds{integral_duration}};
    // test
    if (dt != now)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
    sys_milliseconds dt{sys_milliseconds::duration{integral_duration}};
int main ()
{
    using namespace std::chrono;
    // Get current time with native precision
    auto now = system_clock::now();
    // Convert time_point to signed integral type
    auto integral_duration = now.time_since_epoch().count();
    // Convert signed integral type to time_point
    system_clock::time_point dt{system_clock::duration{integral_duration}};
    // test
    if (dt != now)
        std::cout << "Failure." << std::endl;
    else
        std::cout << "Success." << std::endl;
}
auto now = system_clock::now();

//Cast the time point to ms, then get its duration, then get the duration's count.
auto ms = time_point_cast<milliseconds>(now).time_since_epoch().count();

//Get the time point's duration, then cast to ms, then get its count.
auto ms = duration_cast<milliseconds>(tpBid.time_since_epoch()).count();
long value_ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch()).count();