summaryrefslogtreecommitdiff
path: root/lib/common
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2008-07-27 20:09:00 +0000
committerChris Wilson <chris+github@qwirx.com>2008-07-27 20:09:00 +0000
commitc5d605e27e211dc512e9123b966736baadba6b85 (patch)
tree78963927d36695a6a101047853519ac11cbc29be /lib/common
parentef3b18abba3defefcb421bc89a5688bb60440947 (diff)
Add a function to format a BoxTime as a human-readable time only
(for use in logging).
Diffstat (limited to 'lib/common')
-rw-r--r--lib/common/BoxTime.cpp34
-rw-r--r--lib/common/BoxTime.h2
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