std::put_time

This commit is contained in:
Kjell Hedstrom 2023-12-07 12:17:32 -07:00
parent 540f2711af
commit 8c528081be

View File

@ -106,31 +106,31 @@ namespace g3 {
// This mimics the original "std::put_time(const std::tm* tmb, const charT* fmt)" // This mimics the original "std::put_time(const std::tm* tmb, const charT* fmt)"
// This is needed since latest version (at time of writing) of gcc4.7 does not implement this library function yet. // This is needed since latest version (at time of writing) of gcc4.7 does not implement this library function yet.
// return value is SIMPLIFIED to only return a std::string // return value is SIMPLIFIED to only return a std::string
std::string put_time(const struct tm* tmb, const char* c_time_format) { // std::string put_time(const struct tm* tmb, const char* c_time_format) {
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) && !defined(__MINGW32__) // //#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) && !defined(__MINGW32__)
std::ostringstream oss; // std::ostringstream oss;
oss.fill('0'); // oss.fill('0');
// BOGUS hack done for VS2012: C++11 non-conformant since it SHOULD take a "const struct tm* " // // BOGUS hack done for VS2012: C++11 non-conformant since it SHOULD take a "const struct tm* "
oss << std::put_time(const_cast<struct tm*>(tmb), c_time_format); // oss << std::put_time(tmb, c_time_format);
return oss.str(); // return oss.str();
#else // LINUX // //#else // LINUX
const size_t size = 1024; // // const size_t size = 1024;
char buffer[size]; // IMPORTANT: check now and then for when gcc will implement std::put_time. // // char buffer[size]; // IMPORTANT: check now and then for when gcc will implement std::put_time.
// ... also ... This is way more buffer space then we need // // // ... also ... This is way more buffer space then we need
auto success = std::strftime(buffer, size, c_time_format, tmb); // // auto success = std::strftime(buffer, size, c_time_format, tmb);
// In DEBUG the assert will trigger a process exit. Once inside the if-statement // // // In DEBUG the assert will trigger a process exit. Once inside the if-statement
// the 'always true' expression will be displayed as reason for the exit // // // the 'always true' expression will be displayed as reason for the exit
// // // //
// In Production mode // // // In Production mode
// the assert will do nothing but the format string will instead be returned // // // the assert will do nothing but the format string will instead be returned
if (0 == success) { // // if (0 == success) {
assert((0 != success) && "strftime fails with illegal formatting"); // // assert((0 != success) && "strftime fails with illegal formatting");
return c_time_format; // // return c_time_format;
} // // }
return buffer; // // return buffer;
#endif // //#endif
} // }
tm localtime(std::time_t ts) { tm localtime(std::time_t ts) {
struct tm tm_snapshot; struct tm tm_snapshot;
@ -146,6 +146,8 @@ namespace g3 {
auto format_buffer = internal::localtime_formatted_fractions(ts, time_format); auto format_buffer = internal::localtime_formatted_fractions(ts, time_format);
auto time_point = std::chrono::system_clock::to_time_t(ts); auto time_point = std::chrono::system_clock::to_time_t(ts);
std::tm t = localtime(time_point); std::tm t = localtime(time_point);
return g3::put_time(&t, format_buffer.c_str()); // format example: //"%Y/%m/%d %H:%M:%S"); std::ostringstream oss;
oss << std::put_time(&t, format_buffer.c_str()); // format example: //"%Y/%m/%d %H:%M:%S");
return oss.str();
} }
} // namespace g3 } // namespace g3