Skip to content

Commit

Permalink
bugfix time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjell Hedstrom committed Aug 25, 2016
1 parent 1496d7a commit 26f76cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/g3log/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) ;
}


Expand Down
23 changes: 11 additions & 12 deletions src/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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

Expand All @@ -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

0 comments on commit 26f76cd

Please sign in to comment.