Skip to content

Instantly share code, notes, and snippets.

@bigdavedev
Last active November 23, 2016 12:51
Show Gist options
  • Save bigdavedev/c894957cdd928f21f0897475a269ed11 to your computer and use it in GitHub Desktop.
Save bigdavedev/c894957cdd928f21f0897475a269ed11 to your computer and use it in GitHub Desktop.
When are my eight hours up?!
#include <chrono>
#include <iostream>
#include <sstream>
#include <tuple>
using namespace std::chrono_literals;
auto const get_minutes(std::string const& string)
{
std::stringstream stream{ string };
int tmp = 0;
stream >> tmp;
std::chrono::hours const hours{ tmp };
stream.get(); // swallow the colon
tmp = 0;
stream >> tmp;
std::chrono::minutes const minutes{ tmp };
return hours + minutes;
}
constexpr auto get_hours_and_minutes(std::chrono::minutes const duration)
{
auto const hours = std::chrono::duration_cast<std::chrono::hours>(duration);
auto const minutes = duration % 1h;
return std::make_tuple(hours, minutes);
}
int main(int argc, char* argv[])
{
std::cout << "When did you start working? (08:45): ";
std::string start_time; std::cin >> start_time;
std::cout << "How long were your breaks in total? (minutes): ";
int tmp = 0; std::cin >> tmp;
auto const breaks = std::chrono::minutes{ tmp };
auto const end = get_minutes(start_time) + breaks + 8h;
auto [hours, minutes] = get_hours_and_minutes(end);
std::cout << "Your eight hours are up at " << hours.count() << ":" << minutes.count() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment