chrono

How to create a custom clock for use in std::chrono functions?

会有一股神秘感。 提交于 2019-11-30 14:37:42
问题 I have some arbitrary epoch, like July 13, 1988. Essentially I want to measure the time relative to this. I was thinking of writing a custom clock class, so that I could write code like this: using std::chrono; time_point<My_Clock> tp; std::cout << duration_cast<seconds>(tp.time_since_epoch()).count() << std::endl; Is this possible? If not, what's the cleanest way to accomplish this? 回答1: The hard part of writing this custom clock is figuring out how to write its now() function. In the

Convert std::chrono::time_point to unix timestamp

笑着哭i 提交于 2019-11-30 11:51:12
问题 How can I get an std::chrono::duration since a fixed date? I need this to convert a std::chrono::time_point to an unix timestamp. Insert code into XXX auto unix_epoch_start = XXX; auto time = std::chrono::system_clock::now(); auto delta = time - unix_epoc_start; auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(delta).count(); I know time_point has a method time_since_epoch() but it's not guaranteed that this is the same as the unix epoch (00:00:00 UTC on 1 January 1970).

What is the reason behind std::chrono::duration's lack of immediate tick count manipulation?

主宰稳场 提交于 2019-11-30 11:18:06
Suppose we have #include <chrono> #include <iostream> #include <ctime> namespace Ratios { typedef std::ratio<60*60*24,1> Days; } typedef std::chrono::system_clock Clock; typedef Clock::time_point TimePoint; And our main looks like int main(int argc, char *argv[]) { // argc check left out for brevity const Clock::rep d = static_cast<Clock::rep>(std::atoi(argv[1])); // Right now TimePoint now = Clock::now(); // Start with zero days auto days = std::chrono::duration<Clock::rep, Ratios::Days>::zero(); // Now we'd like to add d to the days days += d; // Error! days.count() = d; // Error! days =

Convert std::chrono::time_point to unix timestamp

流过昼夜 提交于 2019-11-30 01:31:01
How can I get an std::chrono::duration since a fixed date? I need this to convert a std::chrono::time_point to an unix timestamp. Insert code into XXX auto unix_epoch_start = XXX; auto time = std::chrono::system_clock::now(); auto delta = time - unix_epoc_start; auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(delta).count(); I know time_point has a method time_since_epoch() but it's not guaranteed that this is the same as the unix epoch (00:00:00 UTC on 1 January 1970). bames53 A unix time stamp is defined as the number of seconds since January 1, 1970 UTC, except not

Measuring time results in return values of 0 or 0.001

瘦欲@ 提交于 2019-11-29 21:24:10
问题 I am trying to use chrono::steady_clock to measure fractional seconds elapsed between a block of code in my program. I have this block of code working in LiveWorkSpace (http://liveworkspace.org/code/YT1I$9): #include <chrono> #include <iostream> #include <vector> int main() { auto start = std::chrono::steady_clock::now(); for (unsigned long long int i = 0; i < 10000; ++i) { std::vector<int> v(i, 1); } auto end = std::chrono::steady_clock::now(); auto difference = std::chrono::duration_cast

What is the reason behind std::chrono::duration's lack of immediate tick count manipulation?

蹲街弑〆低调 提交于 2019-11-29 17:34:06
问题 Suppose we have #include <chrono> #include <iostream> #include <ctime> namespace Ratios { typedef std::ratio<60*60*24,1> Days; } typedef std::chrono::system_clock Clock; typedef Clock::time_point TimePoint; And our main looks like int main(int argc, char *argv[]) { // argc check left out for brevity const Clock::rep d = static_cast<Clock::rep>(std::atoi(argv[1])); // Right now TimePoint now = Clock::now(); // Start with zero days auto days = std::chrono::duration<Clock::rep, Ratios::Days>:

How to convert std::chrono::time_point to string

偶尔善良 提交于 2019-11-29 10:06:29
How to convert std::chrono::time_point to string? For example: "201601161125" . srdjan.veljkovic The most flexible way to do so is to convert it to struct tm and then use strftime (it's like sprintf for time). Something like: std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm now_tm = *std::localtime(&now_c); /// now you can format the string as you like with `strftime` Look up the documentation for strftime here . If you have localtime_s or localtime_r available you should use either in

Why does chrono have its own namespace?

寵の児 提交于 2019-11-29 09:17:43
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? 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. However this was a very controversial proposal. And many people, including some of my co-authors strongly

What are the uses of std::chrono::high_resolution_clock?

狂风中的少年 提交于 2019-11-28 19:39:44
At first I thought it can be used for performance measurements. But it is said that std::chrono::high_resolution_clock may be not steady ( is_steady may be false ). It is also said that std::chrono::high_resolution_clock may even be an alias of std::chrono::system_clock which is generally not steady. So I can't measure time intervals with this type of clock because at any moment the clock may be adjusted and my measurements will be wrong. At the same time I can't convert time points of std::chrono::high_resolution_clock to calendar time because it doesn't have to_time_t method. So I can't get

Interoperability between boost::date_time and std::chrono

我与影子孤独终老i 提交于 2019-11-28 17:53:51
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. 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> > { inline static posix_time::ptime apply(const chrono::time_point<Clock, Duration>& from) { typedef chrono::time