C ++是否等同于Linux中Python的time.time()? [英] C++ equivalent to Python's time.time() in Linux?

查看:110
本文介绍了C ++是否等同于Linux中Python的time.time()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python程序和一个C ++程序.他们通过IPC进行通信.

I have a Python program and a C++ program. They communicate via IPC.

Python将向C ++程序发送JSON {"event_time":time.time()}.

The Python will send a JSON {"event_time":time.time()} to C++ program.

C ++程序将记录此时间,并根据通过Python发送的时间将事件插入其自己的事件队列中.我需要进行操作,例如比较和减去Python和c ++中的两个时间值.

The C++ program will record this time, and insert the event into its own event queue according to the time sent via Python. I need operations such as comparison and subtraction of two time values from Python and c++.

Python的time.time()是一个简单的双精度数字,可以轻松进行比较和排序(例如,类似于1428657539.065105).

Python's time.time() is a simple double number that can be compared and sorted easily (e.g., it is something like 1428657539.065105).

在C ++中是否有与此值等效的东西?他们至少应该同意毫秒精度的数字,但不是秒精度?也就是说,如果我同时执行这两个程序,则它们应该以秒为单位获得相同的值,并且毫秒范围内的差异较小.

Is there anything equivalent in C++ to this value? They should at least agree to the number in accuracy of millisecond but not second? I.e., if I execute the two programs in the same time, they should get the same value in seconds and minor difference in the millisecond range.

如果没有,那么我必须回退以使用YEAR,MONTH,DAY,HOUR,MIN,SEC,MILLISECOND策略.两个时间值之间的比较,减法等要比双重比较和双重减法更难.

If not, then I have to fall back to use the YEAR, MONTH, DAY, HOUR, MIN, SEC, MILLISECOND strategy. The comparison, subtraction between two time values etc. will be harder than double comparison and double subtraction.

推荐答案

要获取以纪元为单位的当前时间(以秒为单位)作为浮点值,可以将duration_cast设置为浮点持续时间类型:

To get the current time since the epoch in seconds as a floating-point value, you can duration_cast to a floating-point duration type:

#include <chrono>

double fractional_seconds_since_epoch
    = std::chrono::duration_cast<std::chrono::duration<double>>(
        std::chrono::system_clock::now().time_since_epoch()).count();

这篇关于C ++是否等同于Linux中Python的time.time()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆