core: time::Duration, time:Point, time::now
refs #1148
Change-Id: I19c3d2f9b638797a6a36ce41a98be4463b4b7a2d
diff --git a/daemon/common.hpp b/daemon/common.hpp
index 05553b6..0dd2e7e 100644
--- a/daemon/common.hpp
+++ b/daemon/common.hpp
@@ -7,6 +7,8 @@
#ifndef NFD_COMMON_HPP
#define NFD_COMMON_HPP
+#include "config.hpp"
+
#include <ndn-cpp-dev/interest.hpp>
#include <ndn-cpp-dev/data.hpp>
diff --git a/daemon/core/time.cpp b/daemon/core/time.cpp
new file mode 100644
index 0000000..e3a2cda
--- /dev/null
+++ b/daemon/core/time.cpp
@@ -0,0 +1,45 @@
+/* -*- 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()
+{
+#ifdef HAVE_RT
+
+ struct timespec t;
+ int res = clock_gettime(CLOCK_MONOTONIC, &t);
+
+ if (res == -1) {
+ throw std::runtime_error("clock_gettime");
+ }
+
+ return t.tv_sec * 1000000000 + 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 tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
+
+#endif
+}
+
+} // namespace time
+} // namespace ndn
diff --git a/daemon/core/time.hpp b/daemon/core/time.hpp
new file mode 100644
index 0000000..1299ab4
--- /dev/null
+++ b/daemon/core/time.hpp
@@ -0,0 +1,34 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_CORE_TIME_H
+#define NFD_CORE_TIME_H
+
+#include "common.hpp"
+
+namespace ndn {
+namespace time {
+
+/** \class Duration
+ * \brief represents a time interval
+ * Time unit is nanosecond.
+ */
+typedef int64_t Duration;
+
+/** \class Point
+ * \brief represents a point in time
+ * This uses monotonic clock.
+ */
+typedef Duration Point;
+
+/// \return{ the current time in monotonic clock }
+Point
+now();
+
+} // namespace time
+} // namespace ndn
+
+#endif // NFD_CORE_TIME_H
diff --git a/tests/core/time.cpp b/tests/core/time.cpp
new file mode 100644
index 0000000..caae6d0
--- /dev/null
+++ b/tests/core/time.cpp
@@ -0,0 +1,24 @@
+/* -*- 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 "core/time.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(CoreTime)
+
+BOOST_AUTO_TEST_CASE(Now)
+{
+ time::Point p1 = time::now();
+ time::Point p2 = time::now();
+ BOOST_CHECK_LE(p1, p2);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/wscript b/wscript
index 8f1f372..ef6b92f 100644
--- a/wscript
+++ b/wscript
@@ -50,12 +50,16 @@
if int(boost_version[0]) < 1 or int(boost_version[1]) < 42:
Logs.error ("Minumum required boost version is 1.42")
return
+
+ conf.check_cxx(lib='rt', uselib_store='RT', define_name='HAVE_RT', mandatory=False)
+
+ conf.write_config_header('daemon/config.hpp')
def build (bld):
bld(target = "nfd-objects",
features = "cxx",
source = bld.path.ant_glob(['daemon/**/*.cpp'], excl=['daemon/main.cpp']),
- use = 'BOOST NDN_CPP',
+ use = 'BOOST NDN_CPP RT',
includes = [".", "daemon"],
)