Currently, I have A solution and another semi-solution. The semi-solution is linked here, Get seconds since epoch in Linux but that gives the time since epoch for the current datetime. I need to have it tell me the seconds since epoch for a specified date instead. I do have a function for this, however I can't find the original post for it,
std::tm t = {};
std::istringstream ss(dateBuf);
ss >> std::get_time(&t, "%Y-%m-%d");
double seconds = (double)(std::mktime(&t));
There are a few issues I have with it though. Currently, dateBuf = "2020-01-01";
and I require it to be able to handle not just days, but hours, minutes, seconds... Also, I am hoping there is another way of doing this without allocating memory every time to make a new std::tm
and std::istringstream
and then pushing ss
into it. I am wondering if there is a way to go straight from char datebuf[]
to a double/long double
with the seconds as the value. For all intensive purposes, the program will go over this statement many, many times to convert the dates to seconds and this just seems inefficient. Something such as
long double SecondsSinceEpoch(const char* date){
//Taking in a date such as "2020-01-01 00:00:00" (Local Time)
//Will return the value 1577836800 (GMT) and Local time of 1577797200
//And also be able to take in a simpler date such as "2020-01-01"
}