util: Importing scheduler, originally implemented in NFD
The scheduler uses monotonic clock whenever available.
This commit also includes an example to show how to work with the
scheduler.
Change-Id: I006bcc6afddd33caa8d1ec9575c0596234315411
diff --git a/src/util/time.cpp b/src/util/time.cpp
new file mode 100644
index 0000000..97ac14c
--- /dev/null
+++ b/src/util/time.cpp
@@ -0,0 +1,47 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "time.hpp"
+#include <time.h>
+#include <stdexcept>
+#include <sys/time.h>
+
+namespace ndn {
+namespace time {
+
+Point
+now()
+{
+// based on suggestion from https://svn.boost.org/trac/boost/ticket/3504
+#if defined(_POSIX_TIMERS) && ( _POSIX_TIMERS > 0 ) && defined(_POSIX_MONOTONIC_CLOCK)
+
+ struct timespec t;
+ int res = clock_gettime(CLOCK_MONOTONIC, &t);
+
+ if (res == -1) {
+ throw std::runtime_error("clock_gettime");
+ }
+
+ return Point(time::seconds(t.tv_sec) + time::nanoseconds(t.tv_nsec));
+
+#else
+
+ // fallback to wall clock time
+
+ struct timeval tv;
+ int res = gettimeofday(&tv, 0);
+
+ if (res == -1) {
+ throw std::runtime_error("gettimeofday");
+ }
+
+ return Point(time::seconds(tv.tv_sec) + time::microseconds(tv.tv_usec));
+
+#endif
+}
+
+} // namespace time
+} // namespace ndn