summaryrefslogtreecommitdiff
path: root/lib/common/BoxTime.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common/BoxTime.cpp')
-rw-r--r--lib/common/BoxTime.cpp52
1 files changed, 33 insertions, 19 deletions
diff --git a/lib/common/BoxTime.cpp b/lib/common/BoxTime.cpp
index f62b1c35..77daae6d 100644
--- a/lib/common/BoxTime.cpp
+++ b/lib/common/BoxTime.cpp
@@ -35,21 +35,30 @@
// --------------------------------------------------------------------------
box_time_t GetCurrentBoxTime()
{
- #ifdef HAVE_GETTIMEOFDAY
- struct timeval tv;
- if (gettimeofday(&tv, NULL) != 0)
- {
- BOX_LOG_SYS_ERROR("Failed to gettimeofday(), "
- "dropping precision");
- }
- else
- {
- box_time_t timeNow = (tv.tv_sec * MICRO_SEC_IN_SEC_LL)
- + tv.tv_usec;
- return timeNow;
- }
- #endif
-
+#ifdef HAVE_GETTIMEOFDAY
+ struct timeval tv;
+ if (gettimeofday(&tv, NULL) != 0)
+ {
+ BOX_LOG_SYS_ERROR("Failed to gettimeofday(), "
+ "dropping precision");
+ }
+ else
+ {
+ box_time_t time_now = (tv.tv_sec * MICRO_SEC_IN_SEC_LL) + tv.tv_usec;
+ return time_now;
+ }
+#elif WIN32
+ // There's no Win32 API function that returns the current time as a UNIX timestamp with
+ // sub-second precision. So we use time(0) and add the fractional part from
+ // GetSystemTime() in the hope that the difference between these two (if any) is a whole
+ // number of seconds.
+ box_time_t time_now = SecondsToBoxTime(time(0));
+ SYSTEMTIME system_time;
+ GetSystemTime(&system_time);
+ time_now += MilliSecondsToBoxTime(system_time.wMilliseconds);
+ return time_now;
+#endif
+
return SecondsToBoxTime(time(0));
}
@@ -83,7 +92,7 @@ std::string FormatTime(box_time_t time, bool includeDate, bool showMicros)
if (showMicros)
{
- buf << "." << std::setw(6) << micros;
+ buf << "." << std::setw(3) << (int)(micros / 1000);
}
}
else
@@ -108,8 +117,7 @@ void ShortSleep(box_time_t duration, bool logDuration)
{
if(logDuration)
{
- BOX_TRACE("Sleeping for " << BoxTimeToMicroSeconds(duration) <<
- " microseconds");
+ BOX_TRACE("Sleeping for " << BOX_FORMAT_MICROSECONDS(duration));
}
#ifdef WIN32
@@ -118,7 +126,9 @@ void ShortSleep(box_time_t duration, bool logDuration)
struct timespec ts;
memset(&ts, 0, sizeof(ts));
ts.tv_sec = duration / MICRO_SEC_IN_SEC;
- ts.tv_nsec = duration % MICRO_SEC_IN_SEC;
+ ts.tv_nsec = (duration % MICRO_SEC_IN_SEC) * 1000;
+
+ box_time_t start_time = GetCurrentBoxTime();
while (nanosleep(&ts, &ts) == -1 && errno == EINTR)
{
@@ -140,6 +150,10 @@ void ShortSleep(box_time_t duration, bool logDuration)
BOX_TRACE("nanosleep interrupted with " << remain_ns <<
" nanosecs remaining, sleeping again");
}
+
+ box_time_t sleep_time = GetCurrentBoxTime() - start_time;
+ BOX_TRACE("Actually slept for " << BOX_FORMAT_MICROSECONDS(sleep_time) <<
+ ", was aiming for " << BOX_FORMAT_MICROSECONDS(duration));
#endif
}