blob: 07e7f3442f2dcf3eb7604131a74c13b0e7e790c0 [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- 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
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -08007#include "common.hpp"
8
Alexander Afanasyevf6468892014-01-29 01:04:14 -08009#include "time.hpp"
10#include <time.h>
11#include <stdexcept>
12#include <sys/time.h>
13
14namespace ndn {
15namespace time {
16
17Point
18now()
19{
20// based on suggestion from https://svn.boost.org/trac/boost/ticket/3504
21#if defined(_POSIX_TIMERS) && ( _POSIX_TIMERS > 0 ) && defined(_POSIX_MONOTONIC_CLOCK)
22
23 struct timespec t;
24 int res = clock_gettime(CLOCK_MONOTONIC, &t);
25
26 if (res == -1) {
27 throw std::runtime_error("clock_gettime");
28 }
29
30 return Point(time::seconds(t.tv_sec) + time::nanoseconds(t.tv_nsec));
31
32#else
33
34 // fallback to wall clock time
35
36 struct timeval tv;
37 int res = gettimeofday(&tv, 0);
38
39 if (res == -1) {
40 throw std::runtime_error("gettimeofday");
41 }
42
43 return Point(time::seconds(tv.tv_sec) + time::microseconds(tv.tv_usec));
44
45#endif
46}
47
48} // namespace time
49} // namespace ndn