std::chrono..._point_cast(3)
π NAME
std::chrono::time_point_cast β and yes, that's the whole name, bestie, this page is literally about std::chrono::time_point_cast. Straight out of the C++ Standard Libary manual, no cap π«Ά
β¨ SYNOPSIS
okay so first things first: this whole thing lives in the header <chrono> π
Here's the OG version, (since C++11), and it's the one that ran (until C++14):
template< class ToDuration, class Clock, class Duration >
std::chrono::time_point<Clock, ToDuration>
time_point_cast( const std::chrono::time_point<Clock, Duration>&t );
And here's the glow-up, (since C++14) β she got a constexpr upgrade, we love to see it β¨:
template< class ToDuration, class Clock, class Duration >
constexpr std::chrono::time_point<Clock, ToDuration>
time_point_cast( const std::chrono::time_point<Clock, Duration>&t );
πΈ DESCRIPTION
So what does she do? She converts a std::chrono::time_point from one duration to another. That's the whole vibe. Time point in, time point out, different duration, slay π
Now the tiny little detail you cannot skip, because it's the whole gatekeeping moment: time_point_cast participates in overload resolution only if ToDuration is a specialization of std::chrono::duration. Like, if it's not, she's not even showing up to the function. Not invited. Periodt π
π PARAMETERS
tβ thetime_pointyou're converting from, i.e. the one catching these hands π
π¦ RETURN VALUE
She hands you back exactly this, no more, no less, character for character:
std::chrono::time_point<Clock, ToDuration>(
std::chrono::duration_cast<ToDuration>(t.time_since_epoch())).
π₯ EXAMPLE
// Run this code
#include <chrono>
#include <iostream>
using namespace std::chrono_literals;
using Clock = std::chrono::high_resolution_clock;
using Ms = std::chrono::milliseconds;
using Sec = std::chrono::seconds;
template<class Duration>
using TimePoint = std::chrono::time_point<Clock, Duration>;
inline void print_ms(const TimePoint<Ms>& time_point)
{
std::cout << time_point.time_since_epoch().count() << " ms\n";
}
int main()
{
TimePoint<Sec> time_point_sec{4s};
// implicit cast, no precision loss
TimePoint<Ms> time_point_ms{time_point_sec};
print_ms(time_point_ms); // 4000 ms
time_point_ms = TimePoint<Ms>{5756ms};
print_ms(time_point_ms); // 5756 ms
// explicit cast, need when precision loss may happen
// 5756 truncated to 5000
time_point_sec = std::chrono::time_point_cast<Sec>(time_point_ms);
print_ms(time_point_sec); // 5000 ms
}
Notice the comments in there β that's the tea β the implicit cast keeps every drop of precision, but the explicit cast is the one you need when precision loss may happen, and 5756 gets truncated to 5000. She ate and left no crumbs, but she did leave crumbs of your milliseconds on the floor π
β OUTPUT
4000 ms
5756 ms
5000 ms
π SEE ALSO
floor(std::chrono::time_point)β converts atime_pointto another, rounding down(C++17)(function template)ceil(std::chrono::time_point)β converts atime_pointto another, rounding up(C++17)(function template)round(std::chrono::time_point)β converts atime_pointto another, rounding to nearest, ties to even(C++17)(function template)duration_castβ converts a duration to another, with a different tick interval(C++11)(function template)
bestie fr fr read the whole thing before you compile, these rounding girlies are all different and they will humble you π
okay that's the page!! sourced straight from http://cppreference.com, version 2024.06.10, and the doc itself is std::chrono..._point_cast(3) β go forth and cast responsibly, mwah ππ§ββοΈπ
π¦ 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