Namespaces
Variants

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

From cppreference.net
Défini dans l'en-tête <chrono>
template < class ToDuration, class Clock, class Duration >

constexpr std:: chrono :: time_point < Clock, ToDuration >

floor ( const std:: chrono :: time_point < Clock, Duration > & tp ) ;
(depuis C++17)

Retourne le plus grand point temporel t représentable dans ToDuration qui est inférieur ou égal à tp .

La fonction ne participe pas à la résolution de surcharge sauf si ToDuration est une spécialisation de std::chrono::duration .

Table des matières

Paramètres

tp - point temporel à arrondir vers le bas

Valeur de retour

tp arrondi vers le bas au point temporel suivant en utilisant la durée de type ToDuration .

Implémentation possible

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>
    floor(const std::chrono::time_point<Clock, FromDuration>& tp)
{
    return std::chrono::time_point<Clock, To>{
        std::chrono::floor<To>(tp.time_since_epoch())};
}

Exemple

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

Sortie :

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

Voir aussi

convertit un point temporel en un autre point temporel sur la même horloge, avec une durée différente
(modèle de fonction)
convertit un time_point en un autre, en arrondissant vers le haut
(modèle de fonction)
convertit un time_point en un autre, en arrondissant au plus proche, les égalités vers le pair
(modèle de fonction)
convertit une durée en une autre, en arrondissant vers le bas
(modèle de fonction)
(C++11) (C++11)
entier le plus proche non supérieur à la valeur donnée
(fonction)