[Spice-devel] [PATCH spice-streaming-agent v2 3/9] FrameLog: Use std::chrono instead of gettimeofday for time

Lukáš Hrázký lhrazky at redhat.com
Tue Jun 26 11:50:47 UTC 2018


A C++ standard library implementation is a bit simpler and preferred in
C++ codebase. The system_clock has microsecond precision and efficiency
should be on par.

Signed-off-by: Lukáš Hrázký <lhrazky at redhat.com>
---
 src/frame-log.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/frame-log.cpp b/src/frame-log.cpp
index b0bd09e..2ebae91 100644
--- a/src/frame-log.cpp
+++ b/src/frame-log.cpp
@@ -9,9 +9,9 @@
 #include "error.hpp"
 #include "hexdump.h"
 
+#include <chrono>
 #include <cstdarg>
 #include <string.h>
-#include <sys/time.h>
 
 
 namespace spice {
@@ -67,10 +67,8 @@ void FrameLog::log_frame(const void* buffer, size_t buffer_size)
  */
 uint64_t FrameLog::get_time()
 {
-    struct timeval now;
-    gettimeofday(&now, NULL);
-
-    return (uint64_t)now.tv_sec * 1000000 + (uint64_t)now.tv_usec;
+    auto now = std::chrono::system_clock::now().time_since_epoch();
+    return std::chrono::duration_cast<std::chrono::microseconds>(now).count();
 }
 
 }} // namespace spice::streaming_agent
-- 
2.17.1



More information about the Spice-devel mailing list