std::chrono::time_point
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Date and time library
time_point
(C++11)
(C++20)
(C++20)
(C++11)
(C++11)
(C++11)
(C++20)
(C++20)
(C++20)
(C++11)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
std::chrono::time_point
Member functions
(C++20)(C++20)
Non-member functions
(until C++20)(C++20)
(C++17)
(C++17)
(C++17)
Helper classes
(C++26)
Defined in header
<chrono>
template<
(since C++11)
class Clock,
class Duration = typename Clock::duration
Class template std::chrono::time_point
represents a point in time. It is implemented as if it stores a value of type Duration
indicating the time interval from the start of the Clock
's epoch.
Clock
must meet the requirements for Clock or be std::chrono::local_t(since C++20).
Contents
[edit] Member types
Type
Description
Clock
clock
(typedef)
duration::rep
rep
(typedef)
[edit] Member functions
[static]
(public static member function) [edit]
[static]
(public static member function) [edit]
[edit] Non-member functions
(C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)
(function template) [edit]
(C++11)
(function template) [edit]
(C++17)
(function template) [edit]
[edit] Helper classes
hash support for std::chrono::time_point
(class template specialization)
(class template specialization)
[edit] Example
Run this code
#include <algorithm> #include <chrono> #include <ctime> #include <iomanip> #include <iostream> void slow_motion() { static int a[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // Generate Γ(13) == 12! permutations: while (std::ranges::next_permutation (a).found) {} } int main() { using namespace std::literals; // enables literal suffixes, e.g. 24h, 1ms, 1s. const std::chrono::time_point<std::chrono::system_clock > now = std::chrono::system_clock::now (); const std::time_t t_c = std::chrono::system_clock::to_time_t (now - 24h); std::cout << "24 hours ago, the time was " << std::put_time (std::localtime (&t_c), "%F %T.\n") << std::flush ; const std::chrono::time_point<std::chrono::steady_clock > start = std::chrono::steady_clock::now (); std::cout << "Different clocks are not comparable: \n" " System time: " << now.time_since_epoch() << "\n" " Steady time: " << start.time_since_epoch() << '\n'; slow_motion(); const auto end = std::chrono::steady_clock::now (); std::cout << "Slow calculations took " << std::chrono::duration_cast <std::chrono::microseconds >(end - start) << " ≈ " << (end - start) / 1ms << "ms ≈ " // almost equivalent form of the above, but << (end - start) / 1s << "s.\n"; // using milliseconds and seconds accordingly }
Possible output:
24 hours ago, the time was 2021年02月15日 18:28:52. Different clocks are not comparable: System time: 1666497022681282572ns Steady time: 413668317434475ns Slow calculations took 2090448μs ≈ 2090ms ≈ 2s.