chrono

Converting steady_clock::time_point to time_t

人盡茶涼 提交于 2019-12-03 09:43:08
问题 I'm using the steady_clock for saving the time stamp of some messages. For debug purpose is usefull to have the calendar (or something similar). For other clocks ther's the static function to_time_t , but on GCC (MinGW 4.8.0) this function is not present. Now i print something like: Timestamp: 26735259098242 For timestamp i need a steady_clock so I cannot use system_clock or others. Edit The previous print is given from the time_since_epoch().count () 回答1: Assuming you need the steady

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

拈花ヽ惹草 提交于 2019-12-03 05:20:49
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 the following conclusion. Using time_t where it is 64bit long is ok (for most purposes). Using Boost

Why is there no C++11 threadsafe alternative to std::localtime and std::gmtime?

江枫思渺然 提交于 2019-12-03 04:43:07
问题 In C++11 you still have to use std::localtime and std::gmtime as indirection to print a std::chrono::time_point . These functions are not safe to use in a multithreaded environment as introduced with C++11 because they return a pointer to an internal static struct. This is especially annoying since C++11 introduced the convenient function std::put_time which is nearly unusable for the same reason. Why is this so fundamental broken or do I overlook something? 回答1: According to N2661, the paper

How to limit FPS in a loop with C++?

﹥>﹥吖頭↗ 提交于 2019-12-03 03:43:25
问题 I'm trying to limit the frames per second in a loop that is performing intersection checking, using C++ with chrono and thread. Here is my code: std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::chrono::system_clock::time_point lastFrame = std::chrono::system_clock::now(); while (true) { // Maintain designated frequency of 5 Hz (200 ms per frame) now = std::chrono::system_clock::now(); std::chrono::duration<double, std::milli> delta = now - lastFrame;

Converting steady_clock::time_point to time_t

若如初见. 提交于 2019-12-03 03:15:01
I'm using the steady_clock for saving the time stamp of some messages. For debug purpose is usefull to have the calendar (or something similar). For other clocks ther's the static function to_time_t , but on GCC (MinGW 4.8.0) this function is not present. Now i print something like: Timestamp: 26735259098242 For timestamp i need a steady_clock so I cannot use system_clock or others. Edit The previous print is given from the time_since_epoch().count () Ben Voigt Assuming you need the steady behavior for internal calculations, and not for display, here's a function you can use to convert to time

Why is there no C++11 threadsafe alternative to std::localtime and std::gmtime?

与世无争的帅哥 提交于 2019-12-02 19:53:53
In C++11 you still have to use std::localtime and std::gmtime as indirection to print a std::chrono::time_point . These functions are not safe to use in a multithreaded environment as introduced with C++11 because they return a pointer to an internal static struct. This is especially annoying since C++11 introduced the convenient function std::put_time which is nearly unusable for the same reason. Why is this so fundamental broken or do I overlook something? According to N2661 , the paper that added <chrono> : This paper does not offer calendrical services except for a minimal mapping to and

Using 'auto' type deduction - how to find out what type the compiler deduced?

不问归期 提交于 2019-12-02 16:28:15
How can I find out what type the compiler deduced when using the auto keyword? Example 1: Simpler auto tickTime = 0.001; Was this deduced as a float or a double? Example 2: More complex (and my present headache): typedef std::ratio<1, 1> sec; std::chrono::duration<double, sec > timePerTick2{0.001}; auto nextTickTime = std::chrono::high_resolution_clock::now() + timePerTick2; What type is nextTickTime ? The problem I'm having is when I try to send nextTickTime to std::cout . I get the following error: ./main.cpp: In function ‘int main(int, char**)’: ./main.cpp:143:16: error: cannot bind ‘std:

Why can't I change the 'last write time' of my newly created files?

我怕爱的太早我们不能终老 提交于 2019-12-02 11:59:49
First off, I'm using Visual Studio 2015's implementation of the Filesystem library from the upcoming C++17 standard, which is based on Boost::Filesystem. Basically, what I'm trying to do is save a file's timestamp (it's "last write time"), copy that file's contents into an archive along with said timestamp, then extract that file back out and use the saved timestamp to restore the correct "last write time". // Get the file's 'last write time' and convert it into a usable integer. __int64 timestamp = chrono::time_point_cast<chrono::seconds>(fs::last_write_time(src)).time_since_epoch().count();

How is a chrono::year Object Constructed?

北战南征 提交于 2019-12-01 22:09:58
问题 I just noticed that c++20 is going to have chrono::year. It's constructor takes in an int in the range: [-32767, 32767] , however I am unclear what this argument represents. Would this be consistent with tm_year's 1900 origin? Or perhaps time_t's 1970 origin? Or perhaps it's in Anno Domini with a 0 origin? EDIT: This is key to the understanding of what is meant by the is_leap function chrono::year offers. Without an origin it's unclear what year is represented here. 回答1: In 25.8.1 [time.cal

How is a chrono::year Object Constructed?

限于喜欢 提交于 2019-12-01 19:26:21
I just noticed that c++20 is going to have chrono::year . It's constructor takes in an int in the range: [-32767, 32767] , however I am unclear what this argument represents. Would this be consistent with tm_year 's 1900 origin? Or perhaps time_t 's 1970 origin? Or perhaps it's in Anno Domini with a 0 origin? EDIT: This is key to the understanding of what is meant by the is_leap function chrono::year offers. Without an origin it's unclear what year is represented here. In 25.8.1 [time.cal.general]: The types in 25.8 describe the civil (Gregorian) calendar and its relationship to sys_days and