Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame^] | 7 | #include "common.hpp" |
| 8 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 9 | #include "time.hpp" |
| 10 | #include <time.h> |
| 11 | #include <stdexcept> |
| 12 | #include <sys/time.h> |
| 13 | |
| 14 | namespace ndn { |
| 15 | namespace time { |
| 16 | |
| 17 | Point |
| 18 | now() |
| 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 |