blob: 712779d980a117110485a3d6774f7046d2bab1e2 [file] [log] [blame]
Junxiao Shi61c5ef32014-01-24 20:59:30 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "time.hpp"
8#include <time.h>
9#include <stdexcept>
10#include <sys/time.h>
11
12namespace ndn {
13namespace time {
14
15Point
16now()
17{
18#ifdef HAVE_RT
19
20 struct timespec t;
21 int res = clock_gettime(CLOCK_MONOTONIC, &t);
22
23 if (res == -1) {
24 throw std::runtime_error("clock_gettime");
25 }
26
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080027 return Point(time::seconds(t.tv_sec) + time::nanoseconds(t.tv_nsec));
Junxiao Shi61c5ef32014-01-24 20:59:30 -070028
29#else
30 // fallback to wall clock time
31
32 struct timeval tv;
33 int res = gettimeofday(&tv, 0);
34
35 if (res == -1) {
36 throw std::runtime_error("gettimeofday");
37 }
38
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080039 return Point(time::seconds(tv.tv_sec) + time::microseconds(tv.tv_usec));
Junxiao Shi61c5ef32014-01-24 20:59:30 -070040
41#endif
42}
43
44} // namespace time
45} // namespace ndn