A common requirement in programs is getting the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in C++.
To compile and run the program:
In C++, we use the <chrono> library to work with time-related functions. The std::chrono::system_clock::now() function is used to get the current time. We can then use various duration casts to convert the time to seconds, milliseconds, or nanoseconds since the Unix epoch.
The std::time_t type and std::ctime() function are used to convert between time representations and string formats.
Next, we’ll look at another time-related task: time parsing and formatting.