jeudi 26 juillet 2018

How the time_point created with different duration(std::chrono::milliseconds and std::chrono::nanoseconds) is so different

I have created std::chrono::milliseconds ms and std::chrono::nanoseconds ns from (std::chrono::system_clock::now().time_since_epoch().From that duartion i created timepoints and convert it to time_t using system_clock::to_time_t and print it using ctime function.But the time printed is not same. As i undersatand the time_point have duration and duration have rep and period(ratio).So time_point must have same value up to milisecond precesion in both time_points.Why the output is different? Here is my code -

#include <ctime>
#include <ratio>
#include <chrono>
#include <iostream>

using namespace std::chrono;

int main ()
{
    std::chrono::milliseconds ms = std::chrono::duration_cast < std::chrono::milliseconds > (std::chrono::system_clock::now().time_since_epoch());


    std::chrono::nanoseconds ns = std::chrono::duration_cast< std::chrono::nanoseconds > (std::chrono::system_clock::now().time_since_epoch());

    std::chrono::duration<unsigned int,std::ratio<1,1000>> today_day (ms.count());

    std::chrono::duration<system_clock::duration::rep,system_clock::duration::period> same_day(ns.count());
    system_clock::time_point abc(today_day);
    system_clock::time_point abc1(same_day);


    std::time_t tt;


    tt = system_clock::to_time_t ( abc );
    std::cout << "today is: " << ctime(&tt);

    tt = system_clock::to_time_t ( abc1 );
    std::cout << "today is: " << ctime(&tt);

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire