chrono

Outputting Date and Time in C++ using std::chrono

大城市里の小女人 提交于 2019-11-26 18:57:23
问题 I have been upgrading some old code and have been trying to update to c++11 where possible. The following code is how I used to display the time and date in my program #include <iostream> #include <string> #include <stdio.h> #include <time.h> const std::string return_current_time_and_date() const { time_t now = time(0); struct tm tstruct; char buf[80]; tstruct = *localtime(&now); strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct); return buf; } I would like to output the current time and

How to convert std::chrono::time_point to calendar datetime string with fractional seconds?

久未见 提交于 2019-11-26 12:16:21
问题 How to convert std::chrono::time_point to calendar datetime string with fractional seconds? For example: \"10-10-2012 12:38:40.123456\" 回答1: If system_clock, this class have time_t conversion. #include <iostream> #include <chrono> #include <ctime> using namespace std::chrono; int main() { system_clock::time_point p = system_clock::now(); std::time_t t = system_clock::to_time_t(p); std::cout << std::ctime(&t) << std::endl; // for example : Tue Sep 27 14:21:13 2011 } example result: Thu Oct 11

Extract year/month/day etc. from std::chrono::time_point in C++

喜夏-厌秋 提交于 2019-11-26 11:58:36
问题 How can I extract the year, month, day, hour, minute, second and millisecond from an std::chrono::time_point object? I only saw examples on how to extract the total amount of e.g. seconds from a duration . 回答1: You can only extract this information from a system_clock::time_point . This is the only system-supplied clock that has a relationship with the civil calendar. Here is how to get the current time_point using this clock: system_clock::time_point now = system_clock::now(); You can then

std::put_time implementation status in GCC?

痞子三分冷 提交于 2019-11-26 11:04:49
问题 I was trying to compile this example program using GCC (tested versions 4.5.1, 4.6.3, 4.8.4): #include <iostream> #include <iomanip> #include <ctime> #include <chrono> using std::chrono::system_clock; int main() { system_clock::time_point now = system_clock::now(); std::time_t now_c = system_clock::to_time_t( now - std::chrono::hours(24)); std::cout << \"One day ago, the time was \" << std::put_time(std::localtime(&now_c), \"%F %T\") << \'\\n\'; } But it tells me: prog.cpp: In function \'int

Difference between std::system_clock and std::steady_clock?

雨燕双飞 提交于 2019-11-26 07:24:20
问题 What is the difference between std::system_clock and std::steady_clock ? (An example case that illustrate different results/behaviours would be great). If my goal is to precisely measure execution time of functions (like a benchmark), what would be the best choice between std::system_clock , std::steady_clock and std::high_resolution_clock ? 回答1: From N3376: 20.11.7.1 [time.clock.system]/1: Objects of class system_clock represent wall clock time from the system-wide realtime clock. 20.11.7.2

resolution of std::chrono::high_resolution_clock doesn&#39;t correspond to measurements

回眸只為那壹抹淺笑 提交于 2019-11-26 02:20:43
问题 Let me ask my question by this test program: #include <iostream> #include <chrono> using std::chrono::nanoseconds; using std::chrono::duration_cast; int main(int argc, char* argv[]) { std::cout << \"resolution (nano) = \" << (double) std::chrono::high_resolution_clock::period::num / std::chrono::high_resolution_clock::period::den * 1000 * 1000 * 1000 << std::endl; auto t1 = std::chrono::high_resolution_clock::now(); std::cout << \"how much nanoseconds std::cout takes?\" << std::endl; auto t2