Fix TimeValue::now() on Unix.
authorTorok Edwin <edwintorok@gmail.com>
Fri, 22 Jan 2010 15:51:31 +0000 (15:51 +0000)
committerTorok Edwin <edwintorok@gmail.com>
Fri, 22 Jan 2010 15:51:31 +0000 (15:51 +0000)
TimeValue()::now().toEpochTime() is supposed to be the same as time(),
but it wasn't, because toEpoch subtracted PosixZeroTime, but now()
didn't add PosixZeroTime!
Add a unittest to check this works.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94178 91177308-0d34-0410-b5e6-96231b3b80d8

lib/System/Unix/TimeValue.inc
unittests/Support/System.cpp [new file with mode: 0644]

index 1ae8c71..d8cc8f5 100644 (file)
@@ -48,7 +48,7 @@ TimeValue TimeValue::now() {
   }
 
   return TimeValue(
-    static_cast<TimeValue::SecondsType>( the_time.tv_sec ), 
+    static_cast<TimeValue::SecondsType>( the_time.tv_sec + PosixZeroTime.seconds_ ), 
     static_cast<TimeValue::NanoSecondsType>( the_time.tv_usec * 
       NANOSECONDS_PER_MICROSECOND ) );
 }
diff --git a/unittests/Support/System.cpp b/unittests/Support/System.cpp
new file mode 100644 (file)
index 0000000..b3dd17d
--- /dev/null
@@ -0,0 +1,16 @@
+//===- llvm/unittest/Support/System.cpp - System tests --===//
+#include "gtest/gtest.h"
+#include "llvm/System/TimeValue.h"
+#include <time.h>
+
+using namespace llvm;
+namespace {
+class SystemTest : public ::testing::Test {
+};
+
+TEST_F(SystemTest, TimeValue) {
+  sys::TimeValue now = sys::TimeValue::now();
+  time_t now_t = time(NULL);
+  EXPECT_TRUE(abs(now_t - now.toEpochTime()) < 2);
+}
+}