|
Packit Service |
fdd496 |
/* timespec -- System time interface
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2017 Free Software
|
|
Packit Service |
fdd496 |
Foundation, Inc.
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
fdd496 |
it under the terms of the GNU General Public License as published by
|
|
Packit Service |
fdd496 |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
fdd496 |
(at your option) any later version.
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
This program is distributed in the hope that it will be useful,
|
|
Packit Service |
fdd496 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
fdd496 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
fdd496 |
GNU General Public License for more details.
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
fdd496 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
#if ! defined TIMESPEC_H
|
|
Packit Service |
fdd496 |
# define TIMESPEC_H
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
# include <time.h>
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
#ifndef _GL_INLINE_HEADER_BEGIN
|
|
Packit Service |
fdd496 |
#error "Please include config.h first."
|
|
Packit Service |
fdd496 |
#endif
|
|
Packit Service |
fdd496 |
_GL_INLINE_HEADER_BEGIN
|
|
Packit Service |
fdd496 |
#ifndef _GL_TIMESPEC_INLINE
|
|
Packit Service |
fdd496 |
# define _GL_TIMESPEC_INLINE _GL_INLINE
|
|
Packit Service |
fdd496 |
#endif
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
#ifdef __cplusplus
|
|
Packit Service |
fdd496 |
extern "C" {
|
|
Packit Service |
fdd496 |
#endif
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
/* Resolution of timespec timestamps (in units per second), and log
|
|
Packit Service |
fdd496 |
base 10 of the resolution. */
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
enum { TIMESPEC_RESOLUTION = 1000000000 };
|
|
Packit Service |
fdd496 |
enum { LOG10_TIMESPEC_RESOLUTION = 9 };
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
/* Return a timespec with seconds S and nanoseconds NS. */
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
_GL_TIMESPEC_INLINE struct timespec
|
|
Packit Service |
fdd496 |
make_timespec (time_t s, long int ns)
|
|
Packit Service |
fdd496 |
{
|
|
Packit Service |
fdd496 |
struct timespec r;
|
|
Packit Service |
fdd496 |
r.tv_sec = s;
|
|
Packit Service |
fdd496 |
r.tv_nsec = ns;
|
|
Packit Service |
fdd496 |
return r;
|
|
Packit Service |
fdd496 |
}
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
/* Return negative, zero, positive if A < B, A == B, A > B, respectively.
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
For each timestamp T, this code assumes that either:
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
* T.tv_nsec is in the range 0..999999999; or
|
|
Packit Service |
fdd496 |
* T.tv_sec corresponds to a valid leap second on a host that supports
|
|
Packit Service |
fdd496 |
leap seconds, and T.tv_nsec is in the range 1000000000..1999999999; or
|
|
Packit Service |
fdd496 |
* T.tv_sec is the minimum time_t value and T.tv_nsec is -1; or
|
|
Packit Service |
fdd496 |
T.tv_sec is the maximum time_t value and T.tv_nsec is 2000000000.
|
|
Packit Service |
fdd496 |
This allows for special struct timespec values that are less or
|
|
Packit Service |
fdd496 |
greater than all possible valid timestamps.
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
In all these cases, it is safe to subtract two tv_nsec values and
|
|
Packit Service |
fdd496 |
convert the result to integer without worrying about overflow on
|
|
Packit Service |
fdd496 |
any platform of interest to the GNU project, since all such
|
|
Packit Service |
fdd496 |
platforms have 32-bit int or wider.
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
Replacing "(int) (a.tv_nsec - b.tv_nsec)" with something like
|
|
Packit Service |
fdd496 |
"a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec" would cause
|
|
Packit Service |
fdd496 |
this function to work in some cases where the above assumption is
|
|
Packit Service |
fdd496 |
violated, but not in all cases (e.g., a.tv_sec==1, a.tv_nsec==-2,
|
|
Packit Service |
fdd496 |
b.tv_sec==0, b.tv_nsec==999999999) and is arguably not worth the
|
|
Packit Service |
fdd496 |
extra instructions. Using a subtraction has the advantage of
|
|
Packit Service |
fdd496 |
detecting some invalid cases on platforms that detect integer
|
|
Packit Service |
fdd496 |
overflow.
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
The (int) cast avoids a gcc -Wconversion warning. */
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
|
|
Packit Service |
fdd496 |
timespec_cmp (struct timespec a, struct timespec b)
|
|
Packit Service |
fdd496 |
{
|
|
Packit Service |
fdd496 |
return (a.tv_sec < b.tv_sec ? -1
|
|
Packit Service |
fdd496 |
: a.tv_sec > b.tv_sec ? 1
|
|
Packit Service |
fdd496 |
: (int) (a.tv_nsec - b.tv_nsec));
|
|
Packit Service |
fdd496 |
}
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
/* Return -1, 0, 1, depending on the sign of A. A.tv_nsec must be
|
|
Packit Service |
fdd496 |
nonnegative. */
|
|
Packit Service |
fdd496 |
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE
|
|
Packit Service |
fdd496 |
timespec_sign (struct timespec a)
|
|
Packit Service |
fdd496 |
{
|
|
Packit Service |
fdd496 |
return a.tv_sec < 0 ? -1 : a.tv_sec || a.tv_nsec;
|
|
Packit Service |
fdd496 |
}
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
struct timespec timespec_add (struct timespec, struct timespec)
|
|
Packit Service |
fdd496 |
_GL_ATTRIBUTE_CONST;
|
|
Packit Service |
fdd496 |
struct timespec timespec_sub (struct timespec, struct timespec)
|
|
Packit Service |
fdd496 |
_GL_ATTRIBUTE_CONST;
|
|
Packit Service |
fdd496 |
struct timespec dtotimespec (double)
|
|
Packit Service |
fdd496 |
_GL_ATTRIBUTE_CONST;
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
/* Return an approximation to A, of type 'double'. */
|
|
Packit Service |
fdd496 |
_GL_TIMESPEC_INLINE double
|
|
Packit Service |
fdd496 |
timespectod (struct timespec a)
|
|
Packit Service |
fdd496 |
{
|
|
Packit Service |
fdd496 |
return a.tv_sec + a.tv_nsec / 1e9;
|
|
Packit Service |
fdd496 |
}
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
void gettime (struct timespec *);
|
|
Packit Service |
fdd496 |
int settime (struct timespec const *);
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
#ifdef __cplusplus
|
|
Packit Service |
fdd496 |
}
|
|
Packit Service |
fdd496 |
#endif
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
_GL_INLINE_HEADER_END
|
|
Packit Service |
fdd496 |
|
|
Packit Service |
fdd496 |
#endif
|