example3_time_points.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <chrono>

int main() {
    using namespace std::chrono;

    // Get the current time as a time point
    system_clock::time_point now = system_clock::now();

    // Add 1 hour to the current time
    system_clock::time_point one_hour_later = now + hours(1);

    // Convert time point to time_t to print as calendar time
    time_t now_c = system_clock::to_time_t(now);
    time_t later_c = system_clock::to_time_t(one_hour_later);

    std::cout << "Current time: " << std::ctime(&now_c);
    std::cout << "One hour later: " << std::ctime(&later_c);

    return 0;
}
Back to std_chrono