chrono

Several questions about <chrono> header in C++ 11

独自空忆成欢 提交于 2019-12-04 18:45:23
问题 I have several questions about new <chrono> header in C++ 11. Using Windows 7, Visual Studio 2012. Looking at the example http://en.cppreference.com/w/cpp/chrono #include <iostream> #include <chrono> #include <ctime> int fibonacci(int n) { if (n < 3) return 1; return fibonacci(n-1) + fibonacci(n-2); } int main() { std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); int result = fibonacci(42); end = std::chrono::system_clock::now(); int

using std::chrono::high_resolution_clock to write a frame 30 times per second

本小妞迷上赌 提交于 2019-12-04 12:06:05
I'm using OpenCV to write a video file. For cv::VideoWriter to work correctly the call to the write() function has to happen exactly 30 times per second (for a 30fps video). I found this code which uses the boost library to achieve this. I want to to the same but using std::chrono in my program. This is my implementation: std::chrono::high_resolution_clock::time_point prev = std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::time_point current = prev; long long difference = std::chrono::duration_cast<std::chrono::microseconds>(current-prev).count(); while(recording)

Using <chrono> as a timer in bare-metal microcontroller?

我与影子孤独终老i 提交于 2019-12-04 11:18:42
Can chrono be used as a timer/counter in a bare-metal microcontroller (e.g. MSP432 running an RTOS)? Can the high_resolution_clock (and other APIs in chrono) be configured so that it increments based on the given microcontroller's actual timer tick/register? The Real-Time C++ book (section 16.5) seems to suggest this is possible, but I haven't found any examples of this being applied, especially within bare-metal microcontrollers. How could this be implemented? Would this be even recommended? If not, where can chrono aid in RTOS-based embedded software? I would create a clock that implements

How to validate whether my data is x seconds old using chrono package?

我只是一个虾纸丫 提交于 2019-12-04 07:25:26
问题 I am trying to see whether my data is 120 second old or not by looking at the timestamp of the data so I have below small code in my library project which is using std::chrono package: uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); bool is_old = (120 * 1000 < (now - data_holder->getTimestamp())); // some logging to print out above values LOG4CXX_WARN(logger, "data logging, now: " << now << ", data holder timestamp: " << data_holder->getTimestamp()

Convert double to time_t

我的未来我决定 提交于 2019-12-04 04:05:27
I have a double containing seconds. I would like to convert this into a time_t . I can't find a standard function which accomplishes this. Do I have to fill out the time_t by hand? The type of std::time_t is unspecified. Although not defined, this is almost always an integral value holding the number of seconds (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time. So, just a safe casting between them could be fine. Also be carefull about portability (because it's type is not specified in the standard) and consider about the values than can not fit while casting

odd behaviour using chrono::high_resolution_clock::now()

倖福魔咒の 提交于 2019-12-04 00:50:02
问题 I've been looking at various game timing loop methods e.g. Glenn Fiedler and DeWitter. I found critical areas difficult to understand due to my own C++ knowledge limitations. With this I set about trying to implement my own method....I thought a good way to try to understand something about these methods. [edit1: I'm using CodeBlocks IDE with minGW-w64 (x64-4.8.1-posix-seh-rev5) as the compiler] [edit2: code and output windows amended to include a 3rd timer, QueryPerformanceCounter] In trying

How to create a custom chrono clock

半城伤御伤魂 提交于 2019-12-03 20:15:56
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'? Here is an example custom chrono clock that is based on steady_clock and counts time since the first time it is called (approximately): #include "chrono_io.h" #include <chrono> #include <iostream> struct MyClock { using

How to convert std::chrono::time_point to std::tm without using time_t?

懵懂的女人 提交于 2019-12-03 15:55:54
问题 I would like to print or extract year/month/day values. I don't want to use time_t because of the year 2038 problem, but all examples I found on the Internet use it to convert time_point to tm . Is there a simple way to convert from time_point to tm (preferably without boost )? An implementation like timesub from libc would be my last resort: http://www.opensource.apple.com/source/Libc/Libc-262/stdtime/localtime.c Edit: After reading the suggested links and doing some more research, I came to

Convert between c++11 clocks

馋奶兔 提交于 2019-12-03 12:18:36
问题 If I have a time_point for an arbitrary clock (say high_resolution_clock::time_point ), is there a way to convert it to a time_point for another arbitrary clock (say system_clock::time_point )? I know there would have to be limits, if this ability existed, because not all clocks are steady, but is there any functionality to help such conversions in the spec at all? 回答1: I was wondering whether the accuracy of the conversion proposed by T.C. and Howard Hinnant could be improved. For reference,

Several questions about <chrono> header in C++ 11

大兔子大兔子 提交于 2019-12-03 12:05:52
I have several questions about new <chrono> header in C++ 11. Using Windows 7, Visual Studio 2012. Looking at the example http://en.cppreference.com/w/cpp/chrono #include <iostream> #include <chrono> #include <ctime> int fibonacci(int n) { if (n < 3) return 1; return fibonacci(n-1) + fibonacci(n-2); } int main() { std::chrono::time_point<std::chrono::system_clock> start, end; start = std::chrono::system_clock::now(); int result = fibonacci(42); end = std::chrono::system_clock::now(); int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds> (end-start).count(); std::time_t end