bugfix time formatting

This commit is contained in:
Kjell Hedstrom 2016-08-25 00:49:17 -06:00
parent 1496d7aa40
commit 26f76cd103
2 changed files with 14 additions and 15 deletions

View File

@ -40,8 +40,9 @@ namespace g3 {
// wrap for std::chrono::system_clock::now()
std::time_t systemtime_now();
// custom wrap for std::chrono::system_clock::now()but this one
// returns timespec struct instead which is what we use in g3log
struct timespec systemtime_now();
// OSX, Windows needed wrapper for std::timespec_get(struct timespec *ts, int base)
// OSX and Windows also lacks the POSIX clock_gettime(int base, struct timespec *ts)
@ -63,7 +64,6 @@ namespace g3 {
* std::put_time. A possible fix if your c++11 library is not updated is to
* modify this to use std::strftime instead */
std::string localtime_formatted(const timespec& time_snapshot, const std::string& time_format) ;
std::string localtime_formatted(const std::time_t& time_snapshot, const std::string& time_format) ;
}

View File

@ -79,9 +79,15 @@ namespace g3 {
namespace g3 {
std::time_t systemtime_now() {
system_time_point system_now = std::chrono::system_clock::now();
return std::chrono::system_clock::to_time_t(system_now);
// timespec_get systemtime_now() {
// system_time_point system_now = std::chrono::system_clock::now();
// return std::chrono::system_clock::to_time_t(system_now);
// }
struct timespec systemtime_now() {
struct timespec ts;
timespec_get(&ts);
return ts;
}
@ -155,8 +161,6 @@ namespace g3 {
std::string localtime_formatted(const timespec& time_snapshot, const std::string& time_format) {
auto format_buffer = time_format; // copying format string to a separate buffer
@ -174,12 +178,7 @@ namespace g3 {
// replacing "%f[3|6|9]" with sec fractional part value
format_buffer.replace(pos, g3::internal::kFractionalIdentier.size() + padding, value);
}
return localtime_formatted(time_snapshot.tv_sec, format_buffer);
}
std::string localtime_formatted(const std::time_t& time_snapshot, const std::string& time_format) {
std::tm t = localtime(time_snapshot); // could be const, but cannot due to VS2012 is non conformant for C++11's std::put_time (see above)
return g3::put_time(&t, time_format.c_str()); // format example: //"%Y/%m/%d %H:%M:%S");
std::tm t = localtime(time_snapshot.tv_sec);
return g3::put_time(&t, format_buffer.c_str()); // format example: //"%Y/%m/%d %H:%M:%S");
}
} // g3