Add time duration to C++ timepoint

Here I have used time in minutes you can go for anything that you want from the user. So the below is the simple programme using chrono

#include <iostream>
#include <chrono>
using namespace std;
int main() {
    using clock = std::chrono::system_clock;
    clock::time_point nowp = clock::now();
    cout<<"Enter the time that you want to add in minutes"<<endl;
    int time_min;
    cin>>time_min;
    cin.ignore();
    clock::time_point end = nowp + std::chrono::minutes(time_min);
    time_t nowt =  clock::to_time_t ( nowp );
    time_t endt =  clock::to_time_t ( end);
    std::cout  << " " << ctime(&nowt) << "\n";
    std::cout << ctime(&endt) << std::endl;
    return 0;
}

If you want to add five hours to startTimePoint, it's boringly simple:

startTimePoint += hours(5); // from the alias std::chrono::hours

Live example.

By the way, you're trying to convert a steady_clock::now() into a system_clock::time_point, which shouldn't even compile. Change the steady_clock::now() to system_clock::now() and you should be good to go.

Tags:

C++

C++11

Chrono