I have a Python program and a C++ program. They communicate via IPC.
The Python will send a JSON {"event_time":time.time()} to C++ program.
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's time.time() is a simple double number that can be compared and sorted easily (e.g., it is something like 1428657539.065105).
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With