std::chrono...::time_point(3)

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

okay bestie, welcome to std::chrono...::time_point(3) from the C++ Standard Libary manual β€” yes, the manual name is folded in right here, and we are keeping the typo iconic. πŸ’–

πŸ’– NAME πŸ’–

std::chrono::time_point::time_point β€” that's the constructor, and its name is literally std::chrono::time_point::time_point, repeated like a queen. πŸ‘‘

πŸ¦‹ Synopsis πŸ¦‹

the synopsis, with all the (since C++11) and (constexpr since C++14) receipts:

time_point();                                         (1) (since C++11) (constexpr since C++14)
explicit time_point( const duration& d );             (2) (since C++11) (constexpr since C++14)
template< class Duration2 >
time_point( const time_point<Clock, Duration2>& t );  (3) (since C++11) (constexpr since C++14)

Constructs a new time_point from one of several optional data sources. ✨

  1. Default constructor β€” creates a time_point representing the Clock's epoch (i.e., time_since_epoch() is zero). πŸ•°οΈ
  2. Constructs a time_point at Clock's epoch plus d. βž•
  3. Constructs a time_point by converting t to duration. This constructor only participates in overload resolution if Duration2 is implicitly convertible to duration. 🧚

🌸 Parameters 🌸

  • d β€” a duration to copy from. πŸ’«
  • t β€” a time_point to convert from. πŸ¦‹

✨ Example ✨

the page literally says // Run this code, so here's the whole thing, bestie β€” no cap:

#include <chrono>
#include <iostream>

using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;

void print_ms(const TimePoint& point)
{
    using Ms = std::chrono::milliseconds;
    const Clock::duration since_epoch = point.time_since_epoch();
    std::cout << std::chrono::duration_cast<Ms>(since_epoch) << '\n';
}

int main()
{
    const TimePoint default_value = TimePoint(); // (1)
    print_ms(default_value); // 0ms

    const Clock::duration duration_4_seconds = std::chrono::seconds(4);
    const TimePoint time_point_4_seconds(duration_4_seconds); // (2)
    // 4 seconds from start of epoch
    print_ms(time_point_4_seconds); // 4000ms

    const TimePoint time_point_now = Clock::now(); // (3)
    print_ms(time_point_now); // 212178842ms
}

🌈 Possible output: 🌈

0ms
4000ms
212178842ms

πŸ’… See also πŸ’…

  • constructor β€” constructs new duration (public member function of std::chrono::duration<Rep,Period>). πŸŽ€
  • duration_cast β€” converts a duration to another, with a different tick interval (C++11) (function template). ⭐

mwah πŸ’‹ β€” source/version: http://cppreference.com, date: 2024.06.10, and yeah, this was std::chrono...::time_point(3). 🫢

πŸ“¦ where this page comes from