blob: c216df46b4a4a1cbc6f32271abf00deec66d25af [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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080012namespace nfd {
Junxiao Shi61c5ef32014-01-24 20:59:30 -070013namespace 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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080045} // namespace nfd