C ++ Python time.time () equivalent on Linux?

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

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

The C ++ program will record this time and add the event to its own event queue according to the time sent through Python. I need operations such as comparing and subtracting two time values ​​from Python and C ++.

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

Is there something equivalent in C ++ for this value? Should they at least agree with a number exactly milliseconds, but not the second? Ie, if I run two programs at the same time, they should get the same value in seconds and a slight difference in the millisecond range.

If not, then I should return to using the strategies YEAR, MONTH, DAY, HOUR, MIN, SEC, MILLISECOND. Comparison, subtraction between two times, etc. It will be more complicated than double comparison and double subtraction.

+4
source share
1 answer

To get the current time from an era in seconds as a floating point value, you can duration_castuse 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();
+1

All Articles