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