std::chrono...:time_point)(3)
π std::chrono::round(std::chrono::time_point) π
Hey bestie π this is the <std::chrono...:time_point)(3) page, straight outta the C++ Standard Libary manual (yes, that spelling is exactly what's up there, and we are not fixing it, periodt). This one is giving main character energy for rounding a time point to the nearest representable value, no cap.
π NAME π
std::chrono::round(std::chrono::time_point) β std::chrono::round(std::chrono::time_point)
Yes, the NAME section literally lists it twice. We keep both because details are sacred. π«Ά
πΈ Synopsis πΈ
Defined in header <chrono>:
template< class ToDuration, class Clock, class Duration >
constexpr std::chrono::time_point<Clock, ToDuration>
round( const std::chrono::time_point<Clock, Duration>& tp );
since C++17 β¨
Returns the nearest time point to tp representable in ToDuration, rounding to even in halfway cases.
The function does not participate in the overload resolution unless ToDuration is a specialization of std::chrono::duration and std::chrono::treat_as_floating_point_v<typename ToDuration::rep> is false.
π¦ Parameters π¦
tpβ time point to round to nearest
β¨ Return value β¨
tp rounded to nearest time point using duration of type ToDuration, rounding to even in halfway cases.
That halfway-tie behavior is the whole vibe check: ties go to even, bestie. π
π§ Possible implementation π§
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> &&
!std::chrono::treat_as_floating_point_v<typename To::rep>>>
constexpr std::chrono::time_point<Clock, To> round(
const std::chrono::time_point<Clock, FromDuration>& tp)
{
return std::chrono::time_point<Clock, To>{
std::chrono::round<To>(tp.time_since_epoch())};
}
π Example π
Run this code πββοΈπ¨:
#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';
}
}
Output:
Time point Cast Floor Round Ceil
(ms) (s) (s) (s) (s)
5432 5 5 5 6
5678 5 5 6 6
π See also π
time_point_castβ (C++11) converts a time point to another time point on the same clock, with a different duration (function template)ceil(std::chrono::time_point)β (C++17) converts atime_pointto another, rounding up (function template)floor(std::chrono::time_point)β (C++17) converts atime_pointto another, rounding down (function template)round(std::chrono::duration)β (C++17) converts adurationto another, rounding to nearest, ties to even (function template)round,roundf,roundl,lround,lroundf,lroundl,llround,llroundf,llroundlβ (C++11) nearest integer, rounding away from zero in halfway cases (function)
Source/version + date, bestie: http://cppreference.com Β· 2024.06.10 πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms