Not sure why you need to use Unix timestamp, since C++11 and higher has more convinient types and functions to work with time. But anyway, here is example of how it can be done in both new and old way, copied from here: https://en.cppreference.com/w/cpp/chrono/system_clock/to_time_t
#include <iostream>
#include <ctime>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
int main()
{
// The old way
std::time_t oldt = std::time(nullptr);
std::this_thread::sleep_for(2700ms);
// The new way
std::time_t newt = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now());
std::cout << "oldt-newt == " << oldt-newt << " s\n";
}
Pay attention, that system_clock
is used here. It is the only clock that gets "wall clock" time. Other clocks, like steady_clock
is undefined in terms of what it values means. I.e. it can be time from last reboot or time from program start or anything else, the only guarantee steady_clock
has is that it is steadely increasing. At the same time system_clock
can go back, e.g. when user chages it or when adjusting for day light saving.