Namespaces
Variants

std::chrono:: floor (std::chrono::duration)

From cppreference.net
Definiert im Header <chrono>
template < class ToDuration, class Rep, class Period >
constexpr ToDuration floor ( const std:: chrono :: duration < Rep, Period > & d ) ;
(seit C++17)

Gibt die größte Dauer t zurück, die in ToDuration darstellbar ist und kleiner oder gleich d ist.

Die Funktion nimmt nicht an der Überlagerungsauflösung teil, es sei denn, ToDuration ist eine Spezialisierung von std::chrono::duration .

Inhaltsverzeichnis

Parameter

d - umzuwandelnde Dauer

Rückgabewert

d abgerundet auf eine Dauer vom Typ ToDuration .

Mögliche Implementierung

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 floor(const duration<Rep, Period>& d)
{
    To t = std::chrono::duration_cast<To>(d);
    if (t > d)
        return t - To{1};
    return t;
}

Beispiel

#include <chrono>
#include <iomanip>
#include <iostream>
int main()
{
    using namespace std::chrono_literals;
    std::cout << "Duration\tFloor\tRound\tCeil\n";
    for (using Sec = std::chrono::seconds;
        auto const d : {+4999ms, +5000ms, +5001ms, +5499ms, +5500ms, +5999ms,
                        -4999ms, -5000ms, -5001ms, -5499ms, -5500ms, -5999ms})
        std::cout << std::showpos << d << "\t\t"
                  << std::chrono::floor<Sec>(d) << '\t'
                  << std::chrono::round<Sec>(d) << '\t'
                  << std::chrono::ceil <Sec>(d) << '\n';
}

Ausgabe:

Duration	Floor	Round	Ceil
+4999ms		+4s	+5s	+5s
+5000ms		+5s	+5s	+5s
+5001ms		+5s	+5s	+6s
+5499ms		+5s	+5s	+6s
+5500ms		+5s	+6s	+6s
+5999ms		+5s	+6s	+6s
-4999ms		-5s	-5s	-4s
-5000ms		-5s	-5s	-5s
-5001ms		-6s	-5s	-5s
-5499ms		-6s	-5s	-5s
-5500ms		-6s	-6s	-5s
-5999ms		-6s	-6s	-5s

Siehe auch

konvertiert eine Dauer in eine andere mit einem anderen Taktintervall
(Funktions-Template)
konvertiert eine Dauer in eine andere, aufrundend
(Funktions-Template)
konvertiert eine Dauer in eine andere, rundend zur nächsten, bei Gleichheit zur geraden Zahl
(Funktions-Template)
konvertiert einen Zeitpunkt in einen anderen, abrundend
(Funktions-Template)
(C++11) (C++11)
nächstkleinere ganze Zahl nicht größer als der gegebene Wert
(Funktion)