std::chrono..._point::min(3)

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

πŸ’… std::chrono::time_point::min(3)

okay so bestie, welcome to the C++ Standard Libary manual β€” yes that's how it's spelled up there, we don't fix it, we respect it 🫢 β€” and today's main character is std::chrono::time_point::min. She's a static member function and she is, quite literally, the smallest. no cap. πŸ•βœ¨


🌸 NAME 🌸

std::chrono::time_point::min β€” which, and I cannot stress this enough, is std::chrono::time_point::min. iconique. we love a self-identifying queen πŸ‘‘


✨ Synopsis ✨

Two vibes depending on which era of C++ you're living in:

static constexpr time_point min();           (until C++20)
static constexpr time_point min() noexcept;  (since C++20)

So basically she returns a time_point with the smallest possible duration, i.e.:

time_point(std::chrono::duration::min())

that's it. that's the whole thing. she's the bottom of the barrel and she's thriving πŸ’€


πŸŽ€ Parameters πŸŽ€

(none)

not a single argument. she takes nothing. she needs nothing. queen of boundaries πŸ’…


πŸ¦‹ Return value πŸ¦‹

The smallest possible time_point. that's the whole vibe check. the absolute floor. the "going to the club at 9pm" of time points πŸŒ™


πŸ’– Example πŸ’–

// Run this code β€” the manpage literally says that, and honestly? do it. run it.

#include <chrono>
#include <iomanip>
#include <iostream>
#include <ratio>
#include <string>

constexpr auto steady_min =
std::chrono::steady_clock::time_point::min();

void animate_frame_at_time_offset(double game_time)
{
    std::cout << std::string(static_cast<int>(game_time) % 10 + 1, '*')
<< '\n';
}

int main()
{
    auto last_frame = steady_min;
    std::chrono::duration<double, std::micro> game_time{0.0};

    for (int n = 0; n < 5; ++n)
    {
        const auto current_frame = std::chrono::steady_clock::now();
        // initialize timer if first frame ever:
        if (last_frame == steady_min)
            last_frame = current_frame;
        game_time += current_frame - last_frame;
        std::cout << "Drawing frame at " << std::setprecision(10)
                  << std::setw(8) << game_time.count() << " ΞΌs ";
        animate_frame_at_time_offset(game_time.count());
    }
}

okay so what's happening here is lowkey genius: steady_min is used as a sentinel β€” the first frame compares against it to know "oh we've never drawn before, let's initialize the timer." that's the move. that's the whole move. last_frame == steady_min is the check, and the first iteration sets last_frame = current_frame because of it. the way that works 🧚✨

also std::string(static_cast<int>(game_time) % 10 + 1, '*') prints between 1 and 10 stars based on your elapsed time. cute lil star bar chart. ate. ⭐


🌈 Possible output 🌈

Drawing frame at        0 ΞΌs *
Drawing frame at  134.499 ΞΌs *****
Drawing frame at  274.337 ΞΌs *****
Drawing frame at  416.571 ΞΌs *******
Drawing frame at  561.124 ΞΌs **

note the word Possible β€” the exact microsecond numbers are gonna differ on your machine 'cause that's just clocks being clocks, but the star counts and the "ΞΌs" units are the vibe fr fr πŸ•°οΈ

lowkey reminder: the numbers above are illustrative timing from real run, not a promise your CPU is that fast. your mileage will vary, bestie. πŸ™ˆ

πŸ‘‘ sign-off πŸ‘‘

and that's the page!! πŸ’œ source is http://cppreference.com, version stamped 2024.06.10, and this whole entry lives under std::chrono..._point::min(3) β€” go forth and find your minimum, bestie, you're already doing amazing πŸŒΈπŸŽ€πŸ’…

πŸ“¦ where this page comes from