chrono

Why std::chrono::time_point is not large enough to store struct timespec?

主宰稳场 提交于 2019-12-22 04:03:46
问题 I'm trying the recent std::chrono api and I found that on 64 bit Linux architecture and gcc compiler the time_point and duration classes are not able to handle the maximum time range of the operating system at the maximum resolution (nanoseconds). In fact it seems the storage for these classes is a 64bit integral type, compared to timespec and timeval which are internally using two 64 bit integers, one for seconds and one for nanoseconds: #include <iostream> #include <chrono> #include

Get POSIX epoch as system_clock::time_point

大城市里の小女人 提交于 2019-12-22 04:01:05
问题 I'm aware that the default value of a std::chrono::system_clock::time_point is the clock's epoch, but I can't find any mandate in the C++11 standard that system_clock 's epoch is the same as the POSIX epoch (1970-01-01T00:00:00Z). Is it safe to assume on Linux and Windows that this is the case? Or would it be smarter to use std::chrono::system_clock::from_time_t(0) ? 回答1: The standard leaves the epoch of std::chrono::system_clock::time_point unspecified. There are three implementations of std

How to create a custom chrono clock

别说谁变了你拦得住时间么 提交于 2019-12-21 05:34:09
问题 I'm creating a timeline UI control, and time on this timeline always starts at zero, like a stopwatch. I thought of using std::chrono::steady_clock to keep the start and end time on the scale, however, it feels wrong to have a clock-time here, like '10am march 2017' has nothing to do with the beginning of the scale. Should I/can I create a custom clock that starts time at '0'? 回答1: Here is an example custom chrono clock that is based on steady_clock and counts time since the first time it is

How to convert std::chrono::system_clock::duration into struct timeval

◇◆丶佛笑我妖孽 提交于 2019-12-21 05:20:15
问题 The title says it all. I have to implement a function that receives a std::chrono::system_clock::duration value and that needs to convert it into a timeval sruct so I can pass it to some system function. 回答1: More general implementation. template<typename Duration> void to_timeval(Duration&& d, struct timeval & tv) { std::chrono::seconds const sec = std::chrono::duration_cast<std::chrono::seconds>(d); tv.tv_sec = sec.count(); tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>

accurate sampling in c++

早过忘川 提交于 2019-12-19 04:13:24
问题 I want to sample values I get from a gpio 4000 times per second, currently I do something like that: std::vector<int> sample_a_chunk(unsigned int rate, unsigned int block_size_in_seconds) { std::vector<std::int> data; constexpr unsigned int times = rate * block_size_in_seconds; constexpr unsigned int delay = 1000000 / rate; // microseconds for (int j=0; j<times; j++) { data.emplace_back(/* read the value from the gpio */); std::this_thread::sleep_for(std::chrono::microseconds(delay)); }

C++11 actual system time with milliseconds

那年仲夏 提交于 2019-12-19 02:32:25
问题 I've got a problem with getting actual system time with milliseconds. The only one good method I found is in Windows.h , but I can't use it. I'm supposed to use std::chrono . How can I do this? I spent a lot of time trying to google it, but I found only second-precision examples. I'm trying to get string like this: [2014-11-25 22:15:38:449] 回答1: Using code from this answer: #include <chrono> #include <ctime> #include <iostream> template <typename Duration> void print_time(tm t, Duration

C++11 how to print out high resolution clock time_point

血红的双手。 提交于 2019-12-18 15:28:23
问题 How do I print out a time_point when the time_point is obtained from high_resolution_clock? timestamp = std::chrono::high_resolution_clock::now(); std::time_t now = std::chrono::system_clock::to_time_t(timestamp); std::cout << std::ctime(&now) << std::endl; I get the following error message when compiling: error: no viable conversion from 'time_point<class std::__1::chrono::steady_clock, duration<[...], ratio<[...], 1000000000>>>' to 'const time_point<class std::__1::chrono::system_clock,

Why does chrono have its own namespace?

做~自己de王妃 提交于 2019-12-18 05:39:06
问题 Everything else I have seen so far in the C++ standard library is in the std namespace. If I use things from std::chrono I usually exceed my 80 character per line limit - that is not a problem, just inconvienent. So here my simple question: Why does the chrono header has its own namespace? 回答1: I was lead author on the chrono proposal. A sub-namespace was not my first choice, just because of the verbosity. I find myself writing using namespace std::chrono almost every time I use the facility.

Interoperability between boost::date_time and std::chrono

别说谁变了你拦得住时间么 提交于 2019-12-17 22:11:22
问题 How interoperable are boost::date_time and std::chrono? For example, is there a way to convert between boost::posix_time::ptime and std::chrono::time_point? I tried searching for documentation on such conversions but couldn't find any. 回答1: I found this on the boost commits mailing list: http://lists.boost.org/boost-commit/2009/04/15209.php Here are the relevant functions: template < class Clock, class Duration> struct convert_to<posix_time::ptime, chrono::time_point<Clock, Duration> > {

A timer for arbitrary functions

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 20:09:57
问题 I tried to build a function template that can measure the execution time of functions of arbitrary type. Here is what I've tried so far: #include <chrono> #include <iostream> #include <type_traits> #include <utility> // Executes fn with arguments args and returns the time needed // and the result of f if it is not void template <class Fn, class... Args> auto timer(Fn fn, Args... args) -> std::pair<double, decltype(fn(args...))> { static_assert(!std::is_void<decltype(fn(args...))>::value,