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 date in a similar format using std::chrono(or similar) but am unsure how to go about doing so. Any help would be greatly appreciated. Thanks
int main() { time_t timetoday; time(&timetoday); cout << "Calendar date and time as per todays is : " << asctime(localtime(&timetoday));
class Period = std::ratio<1> > class duration; (since C++11) Class template std::chrono::duration represents a time interval. It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational fraction representing the time in seconds from one tick to the next.
C++ includes support for two types of time manipulation: The chrono library, a flexible collection of types that track time with varying degrees of precision (e.g. std::chrono::time_point).
The <chrono>
library only deals with time and not dates, except for the system_clock
which has the ability to convert its timepoints to time_t
. So using <chrono>
for dates will not improve things much. Hopefully we get something like chrono::date
in the not too distant future.
That said, you can use <chrono>
in the following way:
#include <chrono> // chrono::system_clock #include <ctime> // localtime #include <sstream> // stringstream #include <iomanip> // put_time #include <string> // string std::string return_current_time_and_date() { auto now = std::chrono::system_clock::now(); auto in_time_t = std::chrono::system_clock::to_time_t(now); std::stringstream ss; ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X"); return ss.str(); }
Note that std::localtime
may cause data races. localtime_r
or similar functions may be available on your platforms.
Update:
Using a new version of Howard Hinnant's date library you can write:
#include "date.h" #include <chrono> #include <string> #include <sstream> std::string return_current_time_and_date() { auto now = std::chrono::system_clock::now(); auto today = date::floor<days>(now); std::stringstream ss; ss << today << ' ' << date::make_time(now - today) << " UTC"; return ss.str(); }
This will print out something like "2015-07-24 05:15:34.043473124 UTC".
On an unrelated note, returning const
objects has become undesirable with C++11; const return values cannot be moved from. I also removed the trailing const because trailing const is only valid for member functions and this function has no need to be a member.
An example:
#include <iostream> #include <chrono> #include <ctime> std::string getTimeStr(){ std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); std::string s(30, '\0'); std::strftime(&s[0], s.size(), "%Y-%m-%d %H:%M:%S", std::localtime(&now)); return s; } int main(){ std::cout<<getTimeStr()<<std::endl; return 0; }
Output as below:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With