chrono

Chrono timer in C++ to double

家住魔仙堡 提交于 2021-02-08 10:33:21
问题 How can i create a timer that after a certain duration, it does something? so far i have been trying to use the % operation. I made a timer at the start of the function and subtracted the current time (now()function) then i % the difference by 5 because i want 5 seconds to pass: (at the start of the program i defined start as high_resolution_clock::now()) duration<double> dur = start-high_resolution_clock::now(); if(dur%5==0) the error ive been getting is: no operator "==" matches these

Get steady_clock and system_clock at the same time

浪子不回头ぞ 提交于 2021-02-07 18:27:27
问题 My Code looks like: // Next 2 lines should be at the same time auto nowMonotonic = std::chrono::steady_clock::now(); auto nowSystem = std::chrono::system_clock::now(); // Do some calc with nowMonotonic and nowSystem This can happen: auto nowMonotonic = std::chrono::steady_clock::now(); // Some other thread/process interrupts this thread for an // unspecific amount of time... auto nowSystem = std::chrono::system_clock::now(); // Do some calc with nowMonotonic and nowSystem I'm working on a

How can std::chrono::duration::duration() be constexpr?

ぃ、小莉子 提交于 2021-02-06 08:45:55
问题 The default constructor of std::chrono::duration is defined as follows: constexpr duration() = default; (For example, see cppreference.com or the libstdc++ source.) However, cppreference.com also says this about constexpr constructors: A constexpr constructor must satisfy the following requirements: ... every base class and every non-static member must be initialized , either in the constructors initialization list or by a member brace-or-equal initializer. In addition, every constructor

How can std::chrono::duration::duration() be constexpr?

此生再无相见时 提交于 2021-02-06 08:43:47
问题 The default constructor of std::chrono::duration is defined as follows: constexpr duration() = default; (For example, see cppreference.com or the libstdc++ source.) However, cppreference.com also says this about constexpr constructors: A constexpr constructor must satisfy the following requirements: ... every base class and every non-static member must be initialized , either in the constructors initialization list or by a member brace-or-equal initializer. In addition, every constructor

Is the epoch of steady_clock relative to when the operating system starts? or to the process itself?

99封情书 提交于 2021-02-05 06:01:41
问题 Using boost::chrono::steady_clock or std::chrono::steady_clock is suppose to guarantee that physical time is always monotonic and is not affected by date time changes in the system. Here is my question, if I have two processes that need to be immune to system date time changes, is it enough to exchange just the time_since_epoch ? In other words, the time interpretation of the two processes to the same time since epoch will be the same? Specifically I need to answer this question for Windows

Convert std::duration to human readable time

折月煮酒 提交于 2021-02-04 13:25:13
问题 Is there a standard implementation to print std::duration as a human readable duration? steady_clock::time_point start = steady_clock::now(); doSomeFoo(); steady_clock::time_point end = steady_clock::now(); std::cout << "Operation took " << may_be_std::theMagic(start-end) << std::endl; Which should print something similar to: "Operation took 10d:15h:12m:14:s" or something similar. 回答1: Agreed there is no standard implementation. Here is how you can write one yourself: #include <iostream>

How do I set a specific time using the std::chrono library?

给你一囗甜甜゛ 提交于 2021-01-29 13:12:20
问题 Instead of using std::chrono::system_clock::now(); I would like to set my own specific hours\minutes\seconds. Similar to how one would go about using struct tm to manually construct a date-time. What is the best way of doing this? 回答1: Any clock provided by <chrono> refers to an epoch, which is different from the point in time one your input delta refers to (based on the comments below the question). So you shouldn't try to use std::chrono::time_point instances (the instantiations clocks deal

How to convert C++ chrono time in UTC to string in UTC

回眸只為那壹抹淺笑 提交于 2021-01-29 04:46:59
问题 How to convert C++ chrono time in UTC to string in UTC? Because in my tests when convert chrono time to string it is using different timezone. Example: #include <iostream> #include <string> #include <chrono> #include <sstream> #include <iomanip> #include <cstdint> #include <ctime> std::string getStringFromDateTime(const std::chrono::system_clock::time_point & value) { std::time_t time_now_t = std::chrono::system_clock::to_time_t(value); std::tm now_tm = *std::localtime(&time_now_t); char buf

How to Write a Lambda Wrapping a Function with Optional Return Value

我只是一个虾纸丫 提交于 2021-01-05 11:46:45
问题 I have tried to write a lambda that measures the execution time of arbitrary functions. With a lot of help I have managed that for C++14 and functions having a return value, see Measure execution time of arbitrary functions with C++14 lambda. Then I wanted my code to also work with C++11, therefore I have implemented the same idea with template functions. Finally I have realized that this code does not work for functions having no return value. It has been quite simple to generalize the

Gaffer on games timestep: std::chrono implementation

前提是你 提交于 2020-12-01 07:43:09
问题 If you're not familiar with the Gaffer on Games article "Fix your Timestep", you can find it here: https://gafferongames.com/post/fix_your_timestep/ I'm building a game engine, and in an effort to get more comfortable with std::chrono I've been trying to implement a fixed time step using std::chrono for.. a couple of days now and I can't seem to wrap my head around it. Here is the pseudo-code I'm working towards: double t = 0.0; double dt = 0.01; double currentTime = hires_time_in_seconds();