std::chrono::floor(std::chrono::time_point)

From cppreference.com
< cpp‎ | chrono‎ | time point
 
 
 
Date and time utilities
(C++11)
(C++11)
Clocks
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time zones
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
C-style date and time
 
 
Defined in header <chrono>
template <class ToDuration, class Clock, class Duration>
constexpr time_point<Clock, ToDuration> floor(const time_point<Clock, Duration>& tp);
(since C++17)

Returns the largest time point t representable in ToDuration that is smaller or equal to tp.

The function does not participate in the overload resolution unless ToDuration is an specialization of std::chrono::duration.

Parameters

tp - time point to round down

Return value

d rounded down to a the next time point using duration of type ToDuration.

Possible implementation

template <class T> struct is_duration : std::false_type {};
template <class Rep, class Period> struct is_duration<
    std::chrono::duration<Rep, Period>> : std::true_type {};
 
template <class To, class Clock, class FromDuration,
          class = std::enable_if_t<is_duration<To>{}>>
constexpr std::chrono::time_point<Clock, To>
    floor(const std::chrono::time_point<Clock, FromDuration>& tp)
{
    return std::chrono::time_point<Clock, To>{
        std::chrono::floor<To>(tp.time_since_epoch())};
}

Example

#include <iostream>
#include <chrono>
#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

See also

converts a time point to another time point on the same clock, with a different duration
(function template)
converts a time_point to another, rounding up
(function template)
converts a time_point to another, rounding to nearest, ties to even
(function template)
converts a duration to another, rounding down
(function template)
(C++11)(C++11)
nearest integer not greater than the given value
(function)