Std::Put_Time Implementation Status in Gcc

std::put_time implementation status in GCC?

See TODO extended iomanip manipulators std::get_time and std::put_time for gcc 4.8.0.

See also Cross Platform way to get the time of day? claiming that is not implemented in 4.7.0.


UPDATE: As the gcc developer Jonathan Wakely confirmed below: The std::get_time and std::put_time manipulators are still missing in gcc 4.9.


UPDATE: Jonathan Wakely closed this ticket on 22 Dec, 2014:

Fixed for GCC 5

Thanks simonwo for letting me know about it.

C++11: put_time is not a member of std on Modern g++

The problem is that you included the wrong header. std::put_time is declared in <iomanip>, not <chrono>.


Also, it is not complaining about any of the other time operations such as std::localtime and to_time_t.

std::localtime is declared in <ctime>.

Is there a builtin alternative to std::put_time for GCC 5?

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;

std::chrono::duration_cast - is GCC implementation bugged?

It's not a bug, you're just causing a signed overflow, hence undefined behaviour. Indeed, your code is relying on platform dependent assumptions (the system clock period and rep type) that just happens to fail in the gcc test case.

At the moment of writing, the system_clock used by Coliru's GCC environment has a nanosecond duration period of long type, that in turn have the same size of long long.

So, when you duration_cast<system_clock::duration> a time_point<system_clock,Ticks>::max().time_since_epoch()

you are casting a duration of numeric_limits<Ticks::rep>::max() periods of 100 nanoseconds each into a nanosecond duration of long type, resulting in something equivalent to numeric_limits<Ticks::rep>::max()*100 that clearly overflows (your two-complement signed implementation happens to wrap, resulting in -100; anyway, this is still undefined behaviour).

Conversely, on my clang copy, system_clock has a microsecond duration period of long long type, resulting in a duration cast equivalent ot numeric_limits<Ticks::rep>::max()/10.

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

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.

c++ error: Function 'get_time' could not be resolved - Ubuntu g++

At the bottom of this bug report Jonathan Wakely reports it's available from GCC 5. You can use g++ --version to check your version. (Should be std::tm tenpTime FWIW).



Related Topics



Leave a reply



Submit