From 852c9832a02efec47d912d69ad4e32421d16b672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 5 Jul 2016 17:40:53 +0200 Subject: [PATCH] Fix inverted gettimeofday() return value handling The gettimeofday function returns 0 for success, not for failure. As a result of the inverted logic we were losing millisecond precision when parsing incoming messages on non-HAVE_CLOCK_GETTIME systems (macOS). --- src/Message.cpp | 2 +- test/UtilsTest.cpp | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Message.cpp b/src/Message.cpp index 603f33ec..426d009f 100644 --- a/src/Message.cpp +++ b/src/Message.cpp @@ -225,7 +225,7 @@ void CMessage::InitTime() { } #endif - if (!gettimeofday(&m_time, nullptr)) { + if (gettimeofday(&m_time, nullptr)) { m_time.tv_sec = time(nullptr); m_time.tv_usec = 0; } diff --git a/test/UtilsTest.cpp b/test/UtilsTest.cpp index 9c659927..cbfa49e9 100644 --- a/test/UtilsTest.cpp +++ b/test/UtilsTest.cpp @@ -96,11 +96,14 @@ TEST(UtilsTest, ServerTime) { EXPECT_EQ("2011-10-19T16:40:51.620Z", str1); timeval now; - if (!gettimeofday(&now, nullptr)) { + if (gettimeofday(&now, nullptr)) { now.tv_sec = time(nullptr); now.tv_usec = 0; } + // Strip microseconds, server time is ms only + now.tv_usec = (now.tv_usec / 1000) * 1000; + CString str2 = CUtils::FormatServerTime(now); timeval tv2 = CUtils::ParseServerTime(str2); EXPECT_EQ(now.tv_sec, tv2.tv_sec);