I'm trying to read a wstring
which contains some random date, add some minutes and then compare it with my computers date. I've tried the same code on this site and it works just fine, but when I try to run it in Visual Studio 2013 t1
is set to -1
and diff
to 0
.
My code:
#include <iostream>
#include <fstream>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <thread>
int main()
{
std::wstring updateTime(L"Thu Dec 14 15:06:00 2016");
std::wstring updatePeriod(L"30");
std::string timeUpdate(updateTime.begin(), updateTime.end());
std::istringstream ss{ timeUpdate };
struct tm dateUp ={};
ss >> std::get_time(&dateUp, "%a %b %d %X %Y");
std::time_t t1 = std::mktime(&dateUp);
//time_t t1 = std::mktime(&dateUp); // Contine data la care se porneste testul
int periodUpdate = std::stoi(updatePeriod); // Contine numarul de minute
std::cout <<"Data inainte de a se adauga minutele = "<< 1900 + dateUp.tm_year << " " << dateUp.tm_mon << " " << dateUp.tm_mday << " " << dateUp.tm_hour << ":" << dateUp.tm_min << "\n\n";
// Adaug minutele la data la care se porneste testul si apoi refac structura de tip time_t (minutele se convertesc automat in ore, zile..)
dateUp.tm_min += periodUpdate;
t1 = std::mktime(&dateUp);
std::cout <<"Data dupa ce s-au adaugat minutele = "<< 1900 + dateUp.tm_year << " " << dateUp.tm_mon << " " << dateUp.tm_mday << " " << dateUp.tm_hour << ":" << dateUp.tm_min << "\n\n";
// Retin data curenta a calculatorului
time_t now = time(NULL);
struct tm now_tm = *localtime(&now);
time_t t2 = std::mktime(&now_tm);
std::cout << "Data curenta = "<<1900 + now_tm.tm_year << " " << now_tm.tm_mon << " " << now_tm.tm_mday << " " << now_tm.tm_hour << ":" << now_tm.tm_min << "\n\n";
//Compar cele 2 date. Pentru a se face update programat diferenta trebuie sa fie pozitiva. Adica data actuala sa fie mai mare decat data la care ar trebui sa se faca update
double diff = difftime(t2, t1);
std::cout << diff << "\n";
}