Add time duration in c ++ timepoint

I have a starting point in milliseconds, for example:

using namespace std::chrono; typedef time_point<system_clock, milliseconds> MyTimePoint; MyTimePoint startTimePoint = time_point_cast<MyTimePoint::duration>(system_clock::time_point(steady_clock::now())); 

Now I will have a certain number of hours that I want to add or subtract in startTimePoint.

 int numHours = -5//or 5 etc (Can be a plus or minus number) 

How to add this time span to the original startTimePoint?

+5
source share
1 answer

If you want to add five hours to startTimePoint , this is boring simply:

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

Living example .

By the way, you are trying to convert steady_clock::now() to system_clock::time_point , which does not even compile . Change steady_clock::now() to system_clock::now() and you should be good to go.

+11
source

Source: https://habr.com/ru/post/1232411/


All Articles