std::chrono::time_point_cast (3) - Linux Manuals
std::chrono::time_point_cast: std::chrono::time_point_cast
Command to display std::chrono::time_point_cast
manual in Linux: $ man 3 std::chrono::time_point_cast
NAME
std::chrono::time_point_cast - std::chrono::time_point_cast
Synopsis
template <class ToDuration, class Clock, class Duration> (since C++11)
time_point<Clock, ToDuration> time_point_cast( (until C++14)
const time_point<Clock, Duration> &t);
template <class ToDuration, class Clock, class Duration>
constexpr time_point<Clock, ToDuration> time_point_cast( (since C++14)
const time_point<Clock, Duration> &t);
Converts a std::chrono::time_point from one duration to another.
Parameters
t - time_point to convert from
Return value
std::chrono::time_point<Clock, ToDuration>(std::chrono::duration_cast<ToDuration>(t.time_since_epoch()))
Notes
time_point_cast will only participate in overload resolution if ToDuration is an instantiation of duration.
Example
// Run this code
#include <iostream>
#include <chrono>
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(Sec(4));
// implicit cast, no precision loss
TimePoint<Ms> time_point_ms(time_point_sec);
print_ms(time_point_ms); // 4000 ms
time_point_ms = TimePoint<Ms>(Ms(5756));
// explicit cast, need when precision loss may happens
// 5756 truncated to 5000
time_point_sec = std::chrono::time_point_cast<Sec>(time_point_ms);
print_ms(time_point_sec); // 5000 ms
}
Output:
4000 ms
5000 ms
Pages related to std::chrono::time_point_cast
- std::chrono::time_point (3) - std::chrono::time_point
- std::chrono::time_point<Clock,Duration>::max (3) - std::chrono::time_point<Clock,Duration>::max
- std::chrono::time_point<Clock,Duration>::min (3) - std::chrono::time_point<Clock,Duration>::min
- std::chrono::time_point<Clock,Duration>::operator++, (3) - std::chrono::time_point<Clock,Duration>::operator++,
- std::chrono::time_point<Clock,Duration>::operator++,std::chrono::time_point<Clock,Duration>::operator-- (3) - - std::chrono::time_point<Clock,Duration>::operator++,std::chrono::time_point<Clock,Duration>::operator--
- std::chrono::time_point<Clock,Duration>::operator+,operator- (3) - - std::chrono::time_point<Clock,Duration>::operator+,operator-
- std::chrono::time_point<Clock,Duration>::time_point (3) - std::chrono::time_point<Clock,Duration>::time_point
- std::chrono::time_point<Clock,Duration>::time_since_epoch (3) - std::chrono::time_point<Clock,Duration>::time_since_epoch
- std::chrono::time_of_day (3) - std::chrono::time_of_day
- std::chrono::time_of_day<Duration>::hours,std::chrono::time_of_day<Duration>::minutes, (3) - std::chrono::time_of_day<Duration>::hours,std::chrono::time_of_day<Duration>::minutes,
- std::chrono::time_of_day<Duration>::make12,std::chrono::time_of_day<Duration>::make24 (3) - std::chrono::time_of_day<Duration>::make12,std::chrono::time_of_day<Duration>::make24
- std::chrono::time_of_day<Duration>::operatorprecision, (3) - std::chrono::time_of_day<Duration>::operatorprecision,
- std::chrono::time_of_day<Duration>::operatorprecision,std::chrono::time_of_day<Duration>::to_duration (3) - std::chrono::time_of_day<Duration>::operatorprecision,std::chrono::time_of_day<Duration>::to_duration
- std::chrono::time_of_day<Duration>::time_of_day (3) - std::chrono::time_of_day<Duration>::time_of_day
- std::chrono::time_zone (3) - std::chrono::time_zone
- std::chrono::time_zone::get_info (3) - std::chrono::time_zone::get_info
- std::chrono::time_zone::name (3) - std::chrono::time_zone::name
- std::chrono::time_zone::to_local (3) - std::chrono::time_zone::to_local
- std::chrono::time_zone::to_sys (3) - std::chrono::time_zone::to_sys
- std::chrono::tai_clock (3) - std::chrono::tai_clock