|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
(C++20)(C++20) | ||||
| Non-member functions | ||||
(until C++20)(C++20) | ||||
(C++17) | ||||
ceil (C++17) | ||||
(C++17) | ||||
| Helper classes | ||||
Defined in header <chrono> | ||
template<class ToDuration,class Clock,class Duration> constexprstd::chrono::time_point<Clock, ToDuration> | (since C++17) | |
Returns the smallest time pointt representable inToDuration that is greater or equal totp.
The function does not participate in the overload resolution unlessToDuration is a specialization ofstd::chrono::duration.
Contents |
| tp | - | time point to round up |
tp rounded up to the next time point using duration of typeToDuration.
namespace detail{template<class>inlineconstexprbool is_duration_v=false;template<class Rep,class Period>inlineconstexprbool is_duration_v<std::chrono::duration<Rep, Period>>=true;} template<class To,class Clock,class FromDuration,class=std::enable_if_t<detail::is_duration_v<To>>>constexprstd::chrono::time_point<Clock, To> ceil(conststd::chrono::time_point<Clock, FromDuration>& tp){returnstd::chrono::time_point<Clock, To>{std::chrono::ceil<To>(tp.time_since_epoch())};} |
#include <chrono>#include <iostream>#include <string> template<typename TimePoint>std::string to_string(const TimePoint& time_point){returnstd::to_string(time_point.time_since_epoch().count());} int main(){usingnamespace std::literals::chrono_literals;using Sec=std::chrono::seconds; std::cout<<"Time point\t""Cast\t""Floor\t""Round\t""Ceil\n";std::cout<<"(ms)\t\t""(s)\t""(s)\t""(s)\t""(s)\n";for(constauto value_ms:{5432ms, 5678ms}){std::chrono::time_point<std::chrono::system_clock,std::chrono::milliseconds> time_point_ms(value_ms); std::cout<< to_string(time_point_ms)<<"\t\t"<< to_string(std::chrono::time_point_cast<Sec>(time_point_ms))<<'\t'<< to_string(std::chrono::floor<Sec>(time_point_ms))<<'\t'<< to_string(std::chrono::round<Sec>(time_point_ms))<<'\t'<< to_string(std::chrono::ceil<Sec>(time_point_ms))<<'\n';}}
Output:
Time pointCastFloorRoundCeil(ms)(s)(s)(s)(s)5432555656785566
(C++11) | converts a time point to another time point on the same clock, with a different duration (function template)[edit] |
| converts a time_point to another, rounding down (function template)[edit] | |
| converts a time_point to another, rounding to nearest, ties to even (function template)[edit] | |
(C++17) | converts a duration to another, rounding up (function template)[edit] |
(C++11)(C++11) | nearest integer not less than the given value (function)[edit] |