mercredi 16 juin 2021

How to check if current date and time is in the past or future by the given date in c++?

I have two dates which I should compare and finds out whether it in the past or in future?

I have tried chrono but I'm not able to achieve. Any leads would be highly appreciated

Also I would like to find out the given date passed the current midnight time.

My code:

auto current = chrono::system_clock::now();
auto now_timet = chrono::system_clock::to_time_t(current);
            auto now_local = localtime(&now_timet);
now_local->tm_hour = 10;
now_local->tm_min = 0;
now_local->tm_sec = 0;
ctime(&now_timet);
cout << "Local Time " << put_time(now_local, "%c") << endl;
cout << "As time_t " << now_timet << endl;

auto midnight_local = now_local;
midnight_local->tm_hour = 18; 
midnight_local->tm_min = 0; 
midnight_local->tm_sec = 0;



auto midnight_timet = mktime(midnight_local);
cout << "As time_t " << midnight_timet << endl;

auto midnight = chrono::system_clock::from_time_t(midnight_timet);


auto diff_mins = std::chrono::duration_cast<std::chrono::minutes>(current - midnight);
auto diff_hrs = std::chrono::duration_cast<std::chrono::hours>(current - midnight);
cout << "Minutes passed since midnight " << diff_mins.count()  << endl;
cout << "Hours passed since midnight " << diff_hrs.count()  << endl;

I managed to find the difference not able to proceed further. stuck up here. I cannot use strptime as I work in VS2012

Aucun commentaire:

Enregistrer un commentaire