C++ chrono::max() Function



The std::chrono::max() function in C++, returns the maximum representable time point for a specified time_point type. It is helpful for setting the upper limits on time intervals or durations which requires fixed time management.

The chrono::max() function is useful in scenarios where we need to compare or set boundaries for time value, shuch as scheduling, timeouts or duration checks.

Syntax

Following is the syntax for std::chrono::max() function.

static constexpr duration max();

Parameters

This function does not accepts any parameters.

Return value

This function returns the duration object with its highest possible value.

Example 1

In the following example, we are going to retrieve the maximum duration value of the chrono::system_clock.

#include <iostream>
#include <chrono>
#include <limits>
int main() {
   auto a = std::chrono::system_clock::duration::max();
   std::cout << "Result : " << a.count() << "\n";
   return 0;
}

Output

Output of the above code is as follows −

Result : 9223372036854775807

Example 2

Consider the following example, we are going to retrieve the maximum duration for chrono::steady_clock.

#include <iostream>
#include <chrono>
#include <limits>
int main() {
   auto a = std::chrono::steady_clock::duration::max();
   std::cout << "Result : " << a.count() << "\n";
   return 0;
}

Output

Following is the output of the above code −

Result : 9223372036854775807
cpp_chrono.htm
Advertisements