chrono

How can I use std::chrono::duration as a template parameter?

自作多情 提交于 2020-02-02 02:21:29
问题 I have a template class, something like: template < typename T, size_t Seconds > class MyClass {} Now, I would like to change Seconds to be a duration, so the class can be parametrized with std::chrono::duration . For example, I'd like to be able to do this: MyClass < std::string, std::chrono::seconds(30) > object; Also, in the template, I'd like to specify a default value, something like std::chrono::seconds(30) . 回答1: You can design your template in a clever way: template < typename T,

C++ chrono - get duration as float or long long

∥☆過路亽.° 提交于 2020-01-22 23:01:47
问题 I have a duration typedef std::chrono::high_resolution_clock Clock; Clock::time_point beginTime; Clock::time_point endTime; auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - beginTime); And I get duration in std::chrono::milliseconds . But I need duration as float or long long . How to do that? 回答1: From the documentation template< class Rep, class Period = std::ratio<1> > class duration; Class template std::chrono::duration represents a time interval. It

C++ chrono - get duration as float or long long

丶灬走出姿态 提交于 2020-01-22 23:01:12
问题 I have a duration typedef std::chrono::high_resolution_clock Clock; Clock::time_point beginTime; Clock::time_point endTime; auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - beginTime); And I get duration in std::chrono::milliseconds . But I need duration as float or long long . How to do that? 回答1: From the documentation template< class Rep, class Period = std::ratio<1> > class duration; Class template std::chrono::duration represents a time interval. It

Using (draft) C++20 chrono, how to compute various facts about a date

情到浓时终转凉″ 提交于 2020-01-22 06:59:05
问题 https://www.timeanddate.com/date/weekday.html computes various facts about a day of the year, for example: Given an arbitrary date, how can these numbers be computed with the draft C++20 chrono specification? 回答1: This is remarkably easy with the draft C++20 chrono specification. Below I show a function which inputs an arbitrary date, and prints this information to cout . Though at the time of this writing, the draft C++20 chrono specification isn't yet shipping, it is approximated by a free,

Using (draft) C++20 chrono, how to compute various facts about a date

随声附和 提交于 2020-01-22 06:58:29
问题 https://www.timeanddate.com/date/weekday.html computes various facts about a day of the year, for example: Given an arbitrary date, how can these numbers be computed with the draft C++20 chrono specification? 回答1: This is remarkably easy with the draft C++20 chrono specification. Below I show a function which inputs an arbitrary date, and prints this information to cout . Though at the time of this writing, the draft C++20 chrono specification isn't yet shipping, it is approximated by a free,

Running a function for specified duration : C++ with <chrono> [duplicate]

て烟熏妆下的殇ゞ 提交于 2020-01-15 03:59:05
问题 This question already has answers here : What is the best way to exit out of a loop after an elapsed time of 30ms in C++ (9 answers) Closed 6 months ago . I need to implement a function that should do specified task for specified duration which is passed to it as parameter (std::chrono::milliseconds) . I have come up with code : void Run(std::chrono::milliseconds ms) { std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); std::chrono:

How to cast `std::chrono::milliseconds` to `boost::posix_time::milliseconds`

天涯浪子 提交于 2020-01-14 19:21:06
问题 I am using a boost::asio::deadline_timer like so to set some timeout before an async_read operation on a tcp socket . I am using boost 1.61 . long time_out_millis = 2000; boost::asio::deadline_timer theTimer(theSocket.get_io_service(), boost::posix_time::milliseconds(time_out_millis)); Problem: Other parts of my code use std::chrono::milliseconds . I want use std::chrono instead of the "long time_out_millis" & if possible use std::chrono::milliseconds instead of boost::posix_time:

why is std::chrono::duration based on seconds

为君一笑 提交于 2020-01-13 09:54:15
问题 I'm learning <chrono> library, and considering the std::chrono::duration class, is there any specific reason to base it on seconds? For example a variable to store seconds would be chrono::duration<int> two_seconds(2); and all other time spans require relating them to seconds, like chrono::duration<int, ratio<60>> two_minutes(2); chrono::duration<int, ratio<1, 1000>> two_milliseconds(2); chrono::duration<int, ratio<60 * 60 * 24>> two_days(2); Are there any reasons to base duration on seconds

How can this code be constexpr? (std::chrono)

夙愿已清 提交于 2020-01-11 08:29:27
问题 In the standards paper P0092R1, Howard Hinnant wrote: template <class To, class Rep, class Period, class = enable_if_t<detail::is_duration<To>{}>> constexpr To floor(const duration<Rep, Period>& d) { To t = duration_cast<To>(d); if (t > d) --t; return t; } How can this code work? The problem is that operator-- on a std::chrono::duration is not a constexpr operation. It is defined as: duration& operator--(); And yet this code compiles, and gives the right answer at compile time: static_assert

When is std::chrono epoch?

ぐ巨炮叔叔 提交于 2020-01-09 06:47:39
问题 std::chrono::time_point::time_since_epoch() returns a duration , referred to some time_point in the past. When is such a time_point ? It depends on the C++ implementation or it's defined by the C++ standard? Or it is a de facto standard to set the epoch to 1 January 1970 UTC? 回答1: It is a function of both the specific clock the time_point refers to, and the implementation of that clock . The standard specifies three different clocks: system_clock steady_clock high_resolution_clock And the