Namespaces
Variants

std::chrono:: time_point

From cppreference.net
헤더 파일에 정의됨 <chrono>
template <

class Clock,
class Duration = typename Clock :: duration

> class time_point ;
(C++11 이후)

클래스 템플릿 std::chrono::time_point 는 시간상의 한 지점을 나타냅니다. 이것은 Clock 의 epoch 시작부터의 시간 간격을 나타내는 Duration 타입의 값을 저장하는 것처럼 구현됩니다.

Clock Clock 요구사항을 충족해야 합니다 또는 std::chrono::local_t 이어야 합니다 (C++20부터) .

(C++23 이전)

목차

멤버 타입

유형 설명
Clock clock
이 시간점이 측정되는 클록
(typedef)
Duration duration
에포크 이후의 시간을 측정하는 데 사용되는 std::chrono::duration 유형
(typedef)
duration::rep rep
지속 시간의 틱 수를 나타내는 산술 유형
(typedef)
duration::period period
지속 시간의 틱 주기를 나타내는 std::ratio 유형
(typedef)

멤버 함수

새로운 time point를 생성
(public member function)
클록 시작 이후 duration으로서의 time point를 반환
(public member function)
주어진 duration으로 time point를 수정
(public member function)
duration을 증가 또는 감소
(public member function)
[static]
가장 작은 duration에 해당하는 time point를 반환
(public static member function)
[static]
가장 큰 duration에 해당하는 time point를 반환
(public static member function)

비멤버 함수

time_point와 관련된 덧셈 및 뺄셈 연산 수행
(함수 템플릿)
(C++11) (C++11) (C++20에서 제거됨) (C++11) (C++11) (C++11) (C++11) (C++20)
두 time_point 비교
(함수 템플릿)
동일한 클록에서 다른 duration을 가진 time_point로 변환
(함수 템플릿)
time_point를 다른 time_point로 내림하여 변환
(함수 템플릿)
time_point를 다른 time_point로 올림하여 변환
(함수 템플릿)
time_point를 다른 time_point로 반올림 변환 (가까운 값으로, 동점 시 짝수로)
(함수 템플릿)

헬퍼 클래스

std::common_type 특성의 특수화
(클래스 템플릿 특수화)
std::chrono::time_point 대한 해시 지원
(클래스 템플릿 특수화)

예제

#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
}

가능한 출력:

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.

참고 항목

(C++11)
시간 간격
(클래스 템플릿)
특정 year , month , 및 day 를 나타냄
(클래스)