I use the function below with a few minor improvements, but others claim the definition of an era may not be portable. For GCC, it returns seconds as a double value from the time of Unix, but in VC ++ it returns the value from the moment the machine boots. If your goal is simply to get some value in order to make the difference between the two timestamps without storing or separating them, this should be fine. If you need to save or share timestamps, I would suggest explicitly subtracting some era from now () so that the duration object is portable.
//high precision time in seconds since epoch static double getTimeSinceEpoch(std::chrono::high_resolution_clock::time_point* t = nullptr) { using Clock = std::chrono::high_resolution_clock; return std::chrono::duration<double>((t != nullptr ? *t : Clock::now() ).time_since_epoch()).count(); }
Shital shah
source share