X Tutup
The Wayback Machine - https://web.archive.org/web/20201112042651/https://en.cppreference.com/w/cpp/chrono/time_point
Namespaces
Variants
Views
Actions

std::chrono::time_point

From cppreference.com
< cpp‎ | chrono
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
Date and time utilities
(C++11)
time_point
(C++11)
Time of day
(C++20)



(C++20)(C++20)(C++20)(C++20)
Clocks
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Calendars
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
Time zones
(C++20)
(C++20)
(C++20)
(C++20)
C-style date and time
 
 
Defined in header <chrono>
template<

    class Clock,
    class Duration = typename Clock::duration

> class time_point;
(since C++11)

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

Member type Definition
clock Clock, the clock on which this time point is measured
duration Duration, a std::chrono::duration type used to measure the time since epoch
rep Rep, an arithmetic type representing the number of ticks of the duration
period Period, a std::ratio type representing the tick period of the duration

[edit] Member functions

constructs a new time point
(public member function) [edit]
returns the time point as duration since the start of its clock
(public member function) [edit]
modifies the time point by the given duration
(public member function) [edit]
increments or decrements the duration
(public member function) [edit]
[static]
returns the time point corresponding to the smallest duration
(public static member function) [edit]
[static]
returns the time point corresponding to the largest duration
(public static member function) [edit]

[edit] Non-member functions

performs add and subtract operations involving a time point
(function template) [edit]
compares two time points
(function template) [edit]
converts a time point to another time point on the same clock, with a different duration
(function template) [edit]
converts a time_point to another, rounding down
(function template) [edit]
converts a time_point to another, rounding up
(function template) [edit]
converts a time_point to another, rounding to nearest, ties to even
(function template) [edit]

[edit] Helper classes

specializes the std::common_type trait
(class template specialization) [edit]

[edit] Example

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
 
void slow_motion()
{
    static int a[] {1,2,3,4,5,6,7,8,9,10,11,12};
    while (std::next_permutation(std::begin(a), std::end(a))) { }
}
 
int main()
{
    using namespace std::literals; // enables the usage of 24h instead of std::chrono::hours(24)
    auto now = std::chrono::system_clock::now();
    auto 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';
 
    auto start = std::chrono::steady_clock::now();
    slow_motion();
    auto end = std::chrono::steady_clock::now();
    std::cout
      << "Slow calculations took "
      << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "µs ≈ "
      << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms ≈ "
      << std::chrono::duration_cast<std::chrono::seconds>(end - start).count() << "s.\n";
}

Possible output:

24 hours ago, the time was 2020-09-26 17:57:24
Slow calculations took 2542785µs ≈ 2542ms ≈ 2s.

[edit] See also

(C++11)
a time interval
(class template) [edit]
X Tutup