std::chrono..._point_cast(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 β€” the time_point you'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 a time_point to another, rounding down (C++17) (function template)
  • ceil(std::chrono::time_point) β€” converts a time_point to another, rounding up (C++17) (function template)
  • round(std::chrono::time_point) β€” converts a time_point to 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