How do you set time for an instance of
std::chrono::time_point<>? For example: 6 pm tomorrow. I just wanna get the time from now til then. Thanks in advance.
chrono library doesn't have a concept of a calendar day. You can take the current time and add 24 hours, but you can't find the beginning of tomorrow just from the chrono library.
You need boost.date_time ( http://www.boost.org/doc/libs/release/doc/html/date_time.html
) for that, although for simple things you can also get away with C:
std::time_t t = std::time(nullptr);
std::tm ct = *std::gmtime(&t);
ct.tm_min = ct.tm_sec = 0;
ct.tm_hour = 18;
t = std::mktime(&ct);
std::chrono::system_clock::time_point tomorrow_at_6 = std::chrono::system_clock::from_time_t(t);
std::cout << "There's only " << std::chrono::duration<double, std::ratio<3600>>(
tomorrow_at_6 - std::chrono::system_clock::now()
).count() << " hours left until tomorrow 6 pm\n";
Last edited on