std::chrono::time_point_cast
From cppreference.com
 
 
 < cpp | chrono | time point 
 
 
 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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
(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)
time_point_cast
(C++17)
(C++17)
(C++17)
 Helper classes
(C++26)
Defined in header 
 
 
<chrono> 
 template< class ToDuration, class Clock, class Duration >
 
 (since C++11) std::chrono::time_point <Clock, ToDuration>
(until C++14)
template< class ToDuration, class Clock, class Duration >
 
 (since C++14) 
constexpr std::chrono::time_point <Clock, ToDuration>
Converts a std::chrono::time_point from one duration to another.
time_point_cast participates in overload resolution only if ToDuration is a specialization of std::chrono::duration .
Contents
[edit] Parameters
 t
 -
 
time_point to convert from
[edit] Return value
std::chrono::time_point <Clock, ToDuration>(
    std::chrono::duration_cast <ToDuration>(t.time_since_epoch())).
[edit] Example
Run this code
#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}; // implicit conversion, no precision loss 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 // explicit cast, need when precision loss may happen // 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 5756 ms 5000 ms
[edit] See also
(C++17)
(function template) [edit]