我希望能够做到以下几点:
std::cerr << std::chrono::system_clock::now() << std::endl;
并获得以下信息:
Wed May 1 11:11:12 2013
所以我写了以下内容:
template<typename Clock, typename Duration>
std::ostream &operator<<(std::ostream &stream,
const std::chrono::time_point<Clock, Duration> &time_point) {
const time_t time = Clock::to_time_t(time_point);
#if __GNUC__ > 4 || \
((__GNUC__ == 4) && __GNUC_MINOR__ > 8 && __GNUC_REVISION__ > 1)
// Maybe the put_time will be implemented later?
struct tm tm;
localtime_r(&time, &tm);
return stream << std::put_time(tm, "%c");
#else
char buffer[26];
ctime_r(&time, buffer);
buffer[24] = '\0'; // Removes the newline that is added
return stream << buffer;
#endif
}
哪个有效,但是从不同的名称空间调用它时我一直遇到问题。这应该只是在全局名称空间中是否正确?