std::chrono::ceil(std::chrono::time_point)
From cppreference.com
< cpp | chrono | time point
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Date and time library
(C++11)
(C++20)
(C++20)
(C++11)
(C++11)
(C++11)
(C++20)
(C++20)
(C++20)
(C++11)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
std::chrono::time_point
Member functions
(C++20)(C++20)
Non-member functions
(until C++20)(C++20)
(C++17)
ceil
(C++17)
(C++17)
Helper classes
(C++26)
Defined in header
<chrono>
template< class ToDuration, class Clock, class Duration >
(since C++17)
constexpr std::chrono::time_point <Clock, ToDuration>
Returns the smallest time point t
representable in ToDuration
that is greater or equal to tp.
The function does not participate in the overload resolution unless ToDuration
is a specialization of std::chrono::duration .
[edit] Parameters
tp
-
time point to round up
[edit] Return value
tp rounded up to the next time point using duration of type ToDuration
.
[edit] Possible implementation
namespace detail { template<class> inline constexpr bool is_duration_v = false; template<class Rep, class Period> inline constexpr bool 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>>> constexpr std::chrono::time_point <Clock, To> ceil(const std::chrono::time_point <Clock, FromDuration>& tp) { return std::chrono::time_point <Clock, To>{ std::chrono::ceil <To>(tp.time_since_epoch())}; }
[edit] Example
Run this code
#include <chrono> #include <iostream> #include <string> template<typename TimePoint> std::string to_string(const TimePoint& time_point) { return std::to_string (time_point.time_since_epoch().count()); } int main() { using namespace 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 (const auto 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 point Cast Floor Round Ceil (ms) (s) (s) (s) (s) 5432 5 5 5 6 5678 5 5 6 6
[edit] See also
(C++11)
(function template) [edit]
(C++17)
(function template) [edit]