std::chrono...::duration)(3)
π NAME π
okay bestie, welcome to the std::chrono...::duration)(3) page from the C++ Standard Libary (yes, thatβs how itβs spelled on the page π). Weβre here for std::chrono::ceil(std::chrono::duration) β and the NAME literally says std::chrono::ceil(std::chrono::duration) twice, because it understood the assignment π
β¨
πΈ Synopsis πΈ
Defined in header <chrono> π«Ά
template< class ToDuration, class Rep, class Period >
constexpr ToDuration ceil( const std::chrono::duration<Rep, Period>&
d );
(since C++17) β periodt, this one is living its C++17 era β
Returns the smallest duration t representable in ToDuration that is greater or equal to d. Not me being dramatic, thatβs literally the whole point π
Vibe check: the function does not participate in the overload resolution unless ToDuration is a specialization of std::chrono::duration. So if your type isnβt giving duration energy, itβs not invited to the overload party π
π«Ά Parameters π«Ά
dβ duration to convert
π Return value π
d rounded up to a duration of type ToDuration. slay π
π§ Possible implementation π§
bestie, if you wanna peek behind the curtain, hereβs the possible implementation, copied with love and zero gatekeeping:
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 Rep, class Period,
class = std::enable_if_t<detail::is_duration_v<To>>>
constexpr To ceil(const std::chrono::duration<Rep, Period>& d)
{
To t = std::chrono::duration_cast<To>(d);
if (t < d)
return t + To{1};
return t;
}
π Example π
// Run this code β thatβs what the page says, so go off π
#include <chrono>
#include <iostream>
int main()
{
using namespace std::chrono_literals;
using Min = std::chrono::minutes;
std::cout
<< std::showpos
<< "ceil(+2.4min) = " << std::chrono::ceil<Min>(+2.4min).count() << "min\n"
<< "ceil(-2.4min) = " << std::chrono::ceil<Min>(-2.4min).count() << "min\n"
<< "ceil(+0.0min) = " << std::chrono::ceil<Min>(+0.0min).count() << "min\n";
}
π Output π
ceil(+2.4min) = +3min
ceil(-2.4min) = -2min
ceil(+0.0min) = +0min
π See also π
duration_castβ converts a duration to another, with a different tick interval(C++11)(function template).floor(std::chrono::duration)β converts a duration to another, rounding down(C++17)(function template).round(std::chrono::duration)β converts a duration to another, rounding to nearest, ties to even(C++17)(function template).ceil(std::chrono::time_point)β converts a time_point to another, rounding up(C++17)(function template).ceil,ceilf,ceillβ nearest integer not less than the given value (function)(C++11)(C++11).
and thatβs the page, bestie! footer sign-off time: source http://cppreference.com, date/version 2024.06.10, title std::chrono...::duration)(3) β mwah, go round up your durations π
β¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms