GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::chrono::time_point_cast(3) C++ Standard Libary std::chrono::time_point_cast(3)

std::chrono::time_point_cast - std::chrono::time_point_cast


Defined in header <chrono>
template <class ToDuration, class Clock, class Duration>
(since C++11)
std::chrono::time_point<Clock, ToDuration> (until C++14)


time_point_cast(const std::chrono::time_point<Clock, Duration> &t);
template <class ToDuration, class Clock, class Duration>


constexpr std::chrono::time_point<Clock, ToDuration> (since C++14)


time_point_cast(const std::chrono::time_point<Clock, Duration> &t);


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.


t - time_point to convert from


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

// Run this code


#include <iostream>
#include <chrono>
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
}


4000 ms
5756 ms
5000 ms


floor(std::chrono::time_point) converts a time_point to another, rounding down
(C++17) (function template)
ceil(std::chrono::time_point) converts a time_point to another, rounding up
(C++17) (function template)
round(std::chrono::time_point) converts a time_point to another, rounding to
(C++17) nearest, ties to even
(function template)
duration_cast converts a duration to another, with a different tick
(C++11) interval
(function template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.