std::chrono::time_point_cast

From cppreference.com
< cpp‎ | chrono‎ | time point
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
Date and time utilities
Time point
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
Duration
(哋它亢++11)
Clocks
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)      
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Time of day
(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)

Calendars
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)      
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)(哋它亢++20)
Time zones
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

chrono I/O
(哋它亢++20)
C-style date and time
 
 
Defined in header <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 );
(since 哋它亢++11)
(until 哋它亢++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 );
(since 哋它亢++14)

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.

Parameters

t - time_point to convert from

Return value

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

Example

#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 cast, 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

See also

converts a time_point to another, rounding down
(function template)
converts a time_point to another, rounding up
(function template)
converts a time_point to another, rounding to nearest, ties to even
(function template)
(哋它亢++11)
converts a duration to another, with a different tick interval
(function template)