diff options
author | Chris Wilson <chris+github@qwirx.com> | 2008-07-27 20:09:00 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2008-07-27 20:09:00 +0000 |
commit | c5d605e27e211dc512e9123b966736baadba6b85 (patch) | |
tree | 78963927d36695a6a101047853519ac11cbc29be /lib/common/BoxTime.cpp | |
parent | ef3b18abba3defefcb421bc89a5688bb60440947 (diff) |
Add a function to format a BoxTime as a human-readable time only
(for use in logging).
Diffstat (limited to 'lib/common/BoxTime.cpp')
-rw-r--r-- | lib/common/BoxTime.cpp | 34 |
1 files changed, 34 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(); +} + |