Spec-Zone.ru › C++

std::chrono::ceil(std::chrono::time_point)

Определено в заголовке <chrono>
template< class ToDuration, class Clock, class Duration >
constexpr std::chrono::time_point<Clock, ToDuration>
    ceil( const std::chrono::time_point<Clock, Duration>& tp );
(с C++17)

Возвращает наименьшую временную точку t , представимую в ToDuration, которая больше или равна tp.

Функция не участвует в разрешении перегрузки, если ToDuration является специализацией std::chrono::duration.

Параметры

tp - временная точка для округления вверх

Возвращаемое значение

tp округляется вверх до ближайшей временной точки с помощью продолжительности типа ToDuration.

Возможная реализация

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())};
}

Пример

#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';
    }
}

Вывод:

Time point        Cast        Floor        Round        Ceil
(ms)                (s)        (s)        (s)        (s)
5432                5        5        5        6
5678                5        5        6        6

См. также

time_point_cast
(C++11)
преобразует временную точку в другую временную точку на том же таймере с другой продолжительностью
(функция-шаблон)
floor(std::chrono::time_point)
(C++17)
преобразует time_point в другую, округляя вниз
(функция-шаблон)
round(std::chrono::time_point)
(C++17)
преобразует time_point в другую, округляя до ближайшего значения, привязывая к четному
(функция-шаблон)
ceil(std::chrono::duration)
(C++17)
преобразует продолжительность в другую, округляя вверх
(функция-шаблон)
ceilceilfceill
(C++11)(C++11)
ближайшее целое число, не меньшее заданного значения
(функция)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/chrono/time_point/ceil

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API