I'm stuck on GCC4.8 for the time being. I want to print out the current time as something other than seconds. If put_time
worked, my code would be something simple, like this:
std::cout << std::setw(24) << std::put_time(c_time, "[%T%z %F] ");
Without put_time
, I'd have to access the elements of c_time
manually and do all the formatting manually, which would be a pain in the a** and is something I'm much rather avoid if possible. Note that this does not mean that I never want to interact with C in any way, even indirectly -- I'd just like to avoid coding in C directly if possible.
However, I can't find any alternatives to std::put_time
, aside from strftime
, which I'd like to avoid because it requires almost double the lines of code and is a lot harder to read, at least for me. Also, this is C++, not C, so I'd like to steer clear of C functions whenever possible.
Am I missing something? Is there a builtin alternative to std::put_time
which works under GCC 4.8?
Note that it doesn't have to work in exactly the same way -- if it, say, printed it directly to the output, rather than being a stream manipulator, that would be perfectly fine too, as would a function which returned a std::string
containing the formatted time.
I've done a good bit of Googling and found <chrono>
, but that doesn't work because it doesn't have anything to format the time automatically. I'd still have to do it manually, and I'm pretty sure it would be more work, since I'd have to parse the number of seconds since the epoch into a year, month, day, etc.
There are no functions other than put_time
for the outputing of time provided in the chrono
or the iomanip
library.
The ctime
library does provide: strftime
, ctime
, and asctime
.
Since http://stackoverflow.com does not permit questions about finding 3rd party libraries, I'm going to guess that you're just asking for someone to direct you on the use of strftime
? std::put_time(c_time, "[%T%z %F] ")
could be written in the format:
char foo[24];
if(0 < strftime(foo, sizeof(foo), "[%T%z %F] ", c_time)) cout << foo << endl;