1 /* gethrxtime -- get high resolution real time
3 Copyright (C) 2005 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Paul Eggert. */
25 #include "gethrxtime.h"
27 #include <sys/types.h>
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
39 /* Get the time of a high-resolution clock, preferably one that is not
40 subject to resetting or drifting. */
49 return xtime_make (ts.tv_sec, ts.tv_nsec);
53 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
56 if (clock_gettime (CLOCK_MONOTONIC, &ts) == 0)
57 return xtime_make (ts.tv_sec, ts.tv_nsec);
65 return xtime_make (tv.tv_sec, 1000 * tv.tv_usec);
68 /* No monotonically increasing clocks are available; fall back on a
69 clock that might jump backwards, since it's the best we can do. */
70 # elif HAVE_GETTIMEOFDAY && XTIME_PRECISION != 1
73 gettimeofday (&tv, NULL);
74 return xtime_make (tv.tv_sec, 1000 * tv.tv_usec);
77 return xtime_make (time (NULL), 0);