added timeval support to aos::time::Time
git-svn-id: https://robotics.mvla.net/svn/frc971/2013/trunk/src@4167 f308d9b7-e957-4cde-b6ac-9a88185e7312
diff --git a/aos/common/time.h b/aos/common/time.h
index 57c5f26..c64f8b8 100644
--- a/aos/common/time.h
+++ b/aos/common/time.h
@@ -6,8 +6,10 @@
#ifndef __VXWORKS__
#include <type_traits>
+#include <sys/time.h>
#else
#include <sysLib.h>
+#include <sys/times.h>
#endif
#include <ostream>
@@ -47,6 +49,16 @@
ans.tv_nsec = nsec_;
return ans;
}
+ explicit Time(const struct timeval &value)
+ : sec_(value.tv_sec), nsec_(value.tv_usec * kNSecInUSec) {
+ Check();
+ }
+ struct timeval ToTimeval() const {
+ struct timeval ans;
+ ans.tv_sec = sec_;
+ ans.tv_usec = nsec_ / kNSecInUSec;
+ return ans;
+ }
#endif // SWIG
// CLOCK_MONOTONIC on the fitpc and CLOCK_REALTIME on the cRIO because the
// cRIO doesn't have any others.
diff --git a/aos/common/time_test.cc b/aos/common/time_test.cc
index 6587759..e639d23 100644
--- a/aos/common/time_test.cc
+++ b/aos/common/time_test.cc
@@ -18,6 +18,16 @@
EXPECT_EQ(start.tv_nsec, end.tv_nsec);
}
+TEST(TimeTest, timevalConversions) {
+ timeval start{1234, 5678}; // NOLINT
+ Time time(start);
+ EXPECT_EQ(start.tv_sec, static_cast<signed time_t>(time.sec()));
+ EXPECT_EQ(start.tv_usec, time.nsec() / Time::kNSecInUSec);
+ timeval end = time.ToTimeval();
+ EXPECT_EQ(start.tv_sec, end.tv_sec);
+ EXPECT_EQ(start.tv_usec, end.tv_usec);
+}
+
// It's kind of hard not to test Now and SleepFor at the same time.
TEST(TimeTest, NowAndSleepFor) {
// without this, it tends to fail the first time (ends up sleeping for way