vendredi 6 novembre 2020

How to Convert String date to double in C++,

I am getting String from server 11/07/2020 10:57:13 which I am converting to double value 1604723233, however converted double value is less than actual value by 1 hour.

Below is how I am trying to convert...

double StringDateToDouble(string &strDate, string format)
{
    tm currentTime = CurrentTimeToTM();
    tm timeinfo;

    string strTimeZone = currentTime.tm_zone;

    strDate.append("");
    strDate.append(strTimeZone);
    strptime(strDate.c_str(), format.c_str(), &timeinfo);

    time_t timeInSec = mktime(&timeinfo);
    return static_cast<double>(timeInSec);
 }

tm CurrentTimeToTM()
{
 struct tm timeinfo = {0};
 time_t rawtime;
 rawtime = time(&rawtime);

 localtime_r(&rawtime, &timeinfo);
 return timeinfo;
}

What is the correct way? is anything missed here? Any suggetion is appreciated.

Aucun commentaire:

Enregistrer un commentaire