diff options
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/BoxTime.cpp | 34 | ||||
-rw-r--r-- | lib/common/BoxTime.h | 2 |
2 files changed, 36 insertions, 0 deletions
diff --git a/lib/common/BoxTime.cpp b/lib/common/BoxTime.cpp index 7d7b1b40..0d099db5 100644 --- a/lib/common/BoxTime.cpp +++ b/lib/common/BoxTime.cpp @@ -52,3 +52,37 @@ box_time_t GetCurrentBoxTime() return SecondsToBoxTime(time(0)); } + +std::string FormatTime(box_time_t time, bool showMicros) +{ + std::ostringstream buf; + + time_t seconds = BoxTimeToSeconds(time); + int micros = BoxTimeToMicroSeconds(time) % MICRO_SEC_IN_SEC; + + struct tm tm_now, *tm_ptr = &tm_now; + + #ifdef WIN32 + if ((tm_ptr = localtime(&seconds)) != NULL) + #else + if (localtime_r(&seconds, &tm_now) != NULL) + #endif + { + buf << std::setfill('0') << + std::setw(2) << tm_ptr->tm_hour << ":" << + std::setw(2) << tm_ptr->tm_min << ":" << + std::setw(2) << tm_ptr->tm_sec; + + if (showMicros) + { + buf << "." << std::setw(6) << micros; + } + } + else + { + buf << strerror(errno); + } + + return buf.str(); +} + diff --git a/lib/common/BoxTime.h b/lib/common/BoxTime.h index e62a77ab..2023db36 100644 --- a/lib/common/BoxTime.h +++ b/lib/common/BoxTime.h @@ -40,4 +40,6 @@ inline uint64_t BoxTimeToMicroSeconds(box_time_t Time) return Time; } +std::string FormatTime(box_time_t time, bool showMicros = false); + #endif // BOXTIME__H |