Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent to Python's time.time() in Linux? [duplicate]

Tags:

c++

python

linux

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.

like image 236
user534498 Avatar asked Apr 10 '15 09:04

user534498


1 Answers

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();
like image 195
ecatmur Avatar answered Oct 06 '22 12:10

ecatmur