Namespaces
Variants

std::chrono:: time_point_cast

From cppreference.net
헤더 파일에 정의됨 <chrono>
template < class ToDuration, class Clock, class Duration >

std:: chrono :: time_point < Clock, ToDuration >

time_point_cast ( const std:: chrono :: time_point < Clock, Duration > & t ) ;
(C++11 이후)
(C++14 이전)
template < class ToDuration, class Clock, class Duration >

constexpr std:: chrono :: time_point < Clock, ToDuration >

time_point_cast ( const std:: chrono :: time_point < Clock, Duration > & t ) ;
(C++14 이후)

std::chrono::time_point 를 한 duration에서 다른 duration으로 변환합니다.

time_point_cast ToDuration std::chrono::duration 의 특수화(specialization)인 경우에만 오버로드 해결(overload resolution)에 참여합니다.

목차

매개변수

t - time_point 변환할 대상

반환값

std:: chrono :: time_point < Clock, ToDuration > (
std:: chrono :: duration_cast < ToDuration > ( t. time_since_epoch ( ) ) )
.

예제

#include <chrono>
#include <iostream>
using namespace std::chrono_literals;
using Clock = std::chrono::high_resolution_clock;
using Ms = std::chrono::milliseconds;
using Sec = std::chrono::seconds;
template<class Duration>
using TimePoint = std::chrono::time_point<Clock, Duration>;
inline void print_ms(const TimePoint<Ms>& time_point)
{
    std::cout << time_point.time_since_epoch().count() << " ms\n";
}
int main()
{
    TimePoint<Sec> time_point_sec{4s};
    // 암시적 변환, 정밀도 손실 없음
    TimePoint<Ms> time_point_ms = time_point_sec;
    print_ms(time_point_ms); // 4000 ms
    time_point_ms = TimePoint<Ms>{5756ms};
    print_ms(time_point_ms); // 5756 ms
    // 명시적 캐스트, 정밀도 손실이 발생할 수 있는 경우 필요
    // 5756이 5000으로 절삭됨
    time_point_sec = std::chrono::time_point_cast<Sec>(time_point_ms);
    print_ms(time_point_sec); // 5000 ms
}

출력:

4000 ms
5756 ms
5000 ms

참고 항목

time_point를 다른 단위로 변환하며 내림 처리
(함수 템플릿)
time_point를 다른 단위로 변환하며 올림 처리
(함수 템플릿)
time_point를 다른 단위로 변환하며 가장 가까운 값으로 반올림(동점 시 짝수 우선)
(함수 템플릿)
duration을 다른 틱 간격으로 변환
(함수 템플릿)