Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [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 | |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 12 | namespace nfd { |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 13 | namespace time { |
| 14 | |
| 15 | Point |
| 16 | now() |
| 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 Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 27 | return Point(time::seconds(t.tv_sec) + time::nanoseconds(t.tv_nsec)); |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 28 | |
| 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 Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame] | 39 | return Point(time::seconds(tv.tv_sec) + time::microseconds(tv.tv_usec)); |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 40 | |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | } // namespace time |
Alexander Afanasyev | 18bbf81 | 2014-01-29 01:40:23 -0800 | [diff] [blame] | 45 | } // namespace nfd |