std::chrono...::duration)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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