Recording Time in C++ with Chrono

1 day ago 1
ARTICLE AD BOX

I want my program to record the time that a particular process is running. I have a timeFunction() that should get called whenever I want to record the time. When called, it should return the time in a way that I can manipulate, by subtracting it from other times, adding it to other times, etc. What type should timeFunction() return?

time_t timeFunction() { auto duration = std::chrono::system_clock::now(); auto time = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); return time; }

Changed it to:

std::chrono::time_point timeFunction() { std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now(); return now; }

and it still gives an error in the return type:

<source>:14:1: error: deduced class type 'time_point' in function return type 14 | std::chrono::time_point timeFunction() | ^~~ In file included from /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_14.2.0/include/c++/14.2.0/chrono:41, from <source>:2: /cefs/22/22e6cdc013c8541ce3d1548e_consolidated/compilers_c++_x86_gcc_14.2.0/include/c++/14.2.0/bits/chrono.h:68:13: note: 'template<class _Clock, class _Dur> class std::chrono::time_point' declared here 68 | class time_point; | ^~~~~~~~~~
Read Entire Article