2002-03-02 07:20:20 +08:00
|
|
|
/* gettime -- get the system clock
|
2005-02-21 16:09:30 +08:00
|
|
|
Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
|
2002-03-02 07:20:20 +08:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
2005-05-14 15:58:06 +08:00
|
|
|
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
2002-03-02 07:20:20 +08:00
|
|
|
|
|
|
|
/* Written by Paul Eggert. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "timespec.h"
|
|
|
|
|
2005-02-21 16:09:30 +08:00
|
|
|
/* Get the system time into *TS. */
|
2002-03-02 07:20:20 +08:00
|
|
|
|
2005-02-21 16:09:30 +08:00
|
|
|
void
|
2002-03-02 07:20:20 +08:00
|
|
|
gettime (struct timespec *ts)
|
|
|
|
{
|
2005-02-21 16:09:30 +08:00
|
|
|
#if HAVE_NANOTIME
|
|
|
|
nanotime (ts);
|
|
|
|
#else
|
|
|
|
|
|
|
|
# if defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME
|
2002-03-02 07:20:20 +08:00
|
|
|
if (clock_gettime (CLOCK_REALTIME, ts) == 0)
|
2005-02-21 16:09:30 +08:00
|
|
|
return;
|
|
|
|
# endif
|
2002-03-02 07:20:20 +08:00
|
|
|
|
2005-02-21 16:09:30 +08:00
|
|
|
# if HAVE_GETTIMEOFDAY
|
2002-03-02 07:20:20 +08:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2005-02-21 16:09:30 +08:00
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
ts->tv_sec = tv.tv_sec;
|
|
|
|
ts->tv_nsec = tv.tv_usec * 1000;
|
2002-03-02 07:20:20 +08:00
|
|
|
}
|
2005-02-21 16:09:30 +08:00
|
|
|
# else
|
2005-11-26 14:30:35 +08:00
|
|
|
|
|
|
|
# ifndef OK_TO_USE_1S_CLOCK
|
|
|
|
# error "Only 1-second nominal clock resolution found. Is that intended?" \
|
|
|
|
"If so, compile with the -DOK_TO_USE_1S_CLOCK option."
|
|
|
|
# endif
|
2005-02-21 16:09:30 +08:00
|
|
|
ts->tv_sec = time (NULL);
|
|
|
|
ts->tv_nsec = 0;
|
2005-11-26 14:30:35 +08:00
|
|
|
|
2005-02-21 16:09:30 +08:00
|
|
|
# endif
|
2004-08-06 07:01:03 +08:00
|
|
|
|
2005-02-21 16:09:30 +08:00
|
|
|
#endif
|
2002-03-02 07:20:20 +08:00
|
|
|
}
|