Set std::chrono::time_point<> time

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
 
int main()
{
    std::time_t t = std::time(nullptr);
    std::tm ct = *std::gmtime(&t);
    ct.tm_min = ct.tm_sec = 0;
    ct.tm_hour = 18;
    ct.tm_mday++;
    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";
}


demo: http://coliru.stacked-crooked.com/a/1eacd088c91065cc
Last edited on
Topic archived. No new replies allowed.