Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11: increment time_point by one second

If I have a

std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();

variable, how can I define another time_point variable t2 such that it represents the time point exactly one second after of t1?

Something like auto t2 = t1 + "1s". What should I replace "1s" with?

like image 325
thiagowfx Avatar asked Apr 21 '16 06:04

thiagowfx


1 Answers

If you are using C++14 (VS-2015, or -std=c++14 with gcc or clang), then:

using namespace std::chrono_literals;
auto t2 = t1 + 1s;

If you are using C++11:

using namespace std::chrono;
auto t2 = t1 + seconds{1};

If you don't want to make a copy, but add 1 second to t1 itself, += is also ok:

t1 += 1s;
t1 += seconds{1};
like image 134
Howard Hinnant Avatar answered Nov 07 '22 09:11

Howard Hinnant