dimanche 3 février 2019

Get wrong date and time with high_resolution_clock in Visual Studio 2017

I have a problem with Visual Studio 2017.

I'm trying to get the current time and date with millisecond resolution. I tried the follwing code in a few compilers:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ctime>
#include <chrono>
using namespace std;
using namespace chrono;

int main()
{

    high_resolution_clock::time_point p = high_resolution_clock::now();
    milliseconds ms = duration_cast<milliseconds>(p.time_since_epoch());
    seconds s = duration_cast<seconds>(ms);
    time_t t = s.count();
    cout << ctime(&t) << "\n";
    cin.ignore(1);
}

Every compiler except Visual Studio 2017 prints the correct time. The output of Visual Studio is:

Tue Jan 6 07:28:21 1970

the MinGW Output:

Sun Feb 03 18:01:38 2019

Is there any way to fix the code so that it works in all compilers correctly? I need high_resolution_clock to have access to milliseconds.

Aucun commentaire:

Enregistrer un commentaire