chrono

How to declare a variable for high resolution clock in C++?

浪子不回头ぞ 提交于 2020-06-27 18:36:08
问题 In the example here: https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now They declared the clock time point with auto . auto start = std::chrono::high_resolution_clock::now(); and the doc says it returns 'A time point representing the current time.' But I am not sure how do declare in my code below because I am used to declaring the variables at the beginning of the function and I don't know what to declare it as. Code has been simplified here to show what I mean. What do I put

How do I go from a NaiveDate to a specific TimeZone with Chrono?

痴心易碎 提交于 2020-06-22 22:51:28
问题 I am parsing dates and times in Rust using the chrono crate. The dates and times are from a website in which the date and time are from different sections of the page. The date is shown in the format %d/%m/%Y (example: 27/08/2018). The time is shown with only the hour (example: 12, 10, 21, etc.) I want to store these datetimes as UTC so that I can compute time remaining until a given datetime from now in a "timezone agnostic" way. I know which timezone these datetimes are from (Paris time). I

std::chrono add days to current date

岁酱吖の 提交于 2020-05-29 05:28:43
问题 I would like to use std::chrono in order to find calculate the date in the future based on an expiration period. The expiration period is an integer that specifies "days from now". So how do I use chrono lib in order to find the date after 100 days? 回答1: Say you have a time_point. To add days to that object you can just use operator+ with std::chrono::hours : #include <chrono> std::chrono::system_clock::time_point t = std::chrono::system_clock::now(); std::chrono::system_clock::time_point new

std::chrono add days to current date

谁说胖子不能爱 提交于 2020-05-29 05:23:33
问题 I would like to use std::chrono in order to find calculate the date in the future based on an expiration period. The expiration period is an integer that specifies "days from now". So how do I use chrono lib in order to find the date after 100 days? 回答1: Say you have a time_point. To add days to that object you can just use operator+ with std::chrono::hours : #include <chrono> std::chrono::system_clock::time_point t = std::chrono::system_clock::now(); std::chrono::system_clock::time_point new

How to convert std::filesystem::file_time_type to time_t?

谁都会走 提交于 2020-05-24 07:29:53
问题 I wrote a solution for windows using MSVC2015 where the follow code converts the std::filesystem::last_write_time result time_t: time_t ftime = std::file_time_type::clock::to_time_t(fs::last_write_time("/Path/filename")) It works well. Then when I tried to port the solution to Linux using gcc 9.3 (-std=C++2a) I've got the follow error: Error: 'to_time_t' is not member of 'std::chrono::time_point::clock' {aka 'std::filesystem::__file_clock'} I searched for a solution, but what I found is based

How to convert std::chrono::duration to double (seconds)?

血红的双手。 提交于 2020-05-23 21:19:40
问题 Let's have using duration = std::chrono::steady_clock::duration . I would like to convert duration to double in seconds with maximal precition elegantly. I have found the reverse way (convert seconds as double to std::chrono::duration? ), but it didn't help me finding it out. Alternatively expressed, I want optimally some std function double F(duration) , which returns seconds. Thank you. 回答1: Simply do: std::chrono::duration<double>(d).count() Or, as a function: template <class Rep, class

How does std::chrono::duration default constructed?

僤鯓⒐⒋嵵緔 提交于 2020-05-13 07:13:34
问题 cppreference.com says The default constructor is defaulted . I also checked the C++14 draft , it said nothing on the default constructor, except the declaration: constexpr duration() = default; When I run the following code, I was surprised. chrono::seconds s; cout << s.count() << endl; Each time I run it, the program prints some arbitrary numbers: 140737364037104 , 140737078676496 and so on. It seems that s does NOT well initialized. Then I checked my compiler (GCC 4.8)'s implementation for

How does std::chrono::duration default constructed?

六眼飞鱼酱① 提交于 2020-05-13 07:10:16
问题 cppreference.com says The default constructor is defaulted . I also checked the C++14 draft , it said nothing on the default constructor, except the declaration: constexpr duration() = default; When I run the following code, I was surprised. chrono::seconds s; cout << s.count() << endl; Each time I run it, the program prints some arbitrary numbers: 140737364037104 , 140737078676496 and so on. It seems that s does NOT well initialized. Then I checked my compiler (GCC 4.8)'s implementation for

How does duration_cast round

偶尔善良 提交于 2020-02-23 09:15:33
问题 If I convert to a coarser unit of time (say std::chrono::minutes to std::chrono::hours ) how will duration_cast round? For example, what value will std::chrono::minutes(91) become if converted to std::chrono::hours ? 2h, 1h? 回答1: duration_cast always rounds towards zero. I.e. positive values round down and negative values round up. For other rounding options see: http://howardhinnant.github.io/duration_io/chrono_util.html floor , ceil , and round are currently in the draft C++ 1z (hopefully C

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

余生长醉 提交于 2020-02-02 02:21:32
问题 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,