I need to calculate the seconds and nanoseconds between the Unix Epoch (00:00:00 January 1st 1970 UTC) and some date in the future the user will select.
Here is what I managed to find so far looking through other Stack Overflow answers:
// seconds and nanoseconds past epoch
auto now = std::chrono::system_clock::now().time_since_epoch();
auto now_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now);
auto now_sec = std::chrono::duration_cast<std::chrono::seconds>(now);
// output total ns since epoch
qDebug() << now_ns.count() << "ns";
// output total secs since epoch
qDebug() << now_sec.count() << "secs";
auto nano_secs = now_ns.count() - (now_sec.count() * 1000000000);
// output ns minus whole seconds
qDebug() << "nano_secs = " << nano_secs;
Using this, I am able to get the result I am looking for for the current date and time but that's not what I need. Let's say for demonstration purposes that future date is 00:00:00 July 1st 2020 UTC. How can I go about calculating this?