Exploring date and time with chrono

auto current = chrono::system_clock::now();
auto now_timet = chrono::system_clock::to_time_t(current);
auto now_local = localtime(&now_timet);
cout << "Local Time " << put_time(now_local, "%c") << endl;
output:
Local Time Tue Apr 21 17:56:27 2020
cout << "As time_t " << now_timet << endl;output:
As time_t 1587506187
auto midnight_local = now_local;
midnight_local->tm_hour = 0;
midnight_local->tm_min = 0;
midnight_local->tm_sec = 0;
auto midnight_timet = mktime(midnight_local);
cout << "As time_t " << midnight_timet << endl;
output:
As time_t 1587441600
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;
output:
Minutes passed since midnight 1076
Hours passed since midnight 17

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store