| 1 |
/* timespec -- System time interface |
| 2 |
|
| 3 |
Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2024 Free Software |
| 4 |
Foundation, Inc. |
| 5 |
|
| 6 |
This file is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU Lesser General Public License as |
| 8 |
published by the Free Software Foundation, either version 3 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
This file is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#if ! defined TIMESPEC_H |
| 20 |
#define TIMESPEC_H |
| 21 |
|
| 22 |
/* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_ATTRIBUTE_CONST, |
| 23 |
_GL_ATTRIBUTE_PURE, _GL_CMP. */ |
| 24 |
#if !_GL_CONFIG_H_INCLUDED |
| 25 |
#error "Please include config.h first." |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <time.h> |
| 29 |
|
| 30 |
_GL_INLINE_HEADER_BEGIN |
| 31 |
#ifndef _GL_TIMESPEC_INLINE |
| 32 |
# define _GL_TIMESPEC_INLINE _GL_INLINE |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef __cplusplus |
| 36 |
extern "C" { |
| 37 |
#endif |
| 38 |
|
| 39 |
#include "arg-nonnull.h" |
| 40 |
|
| 41 |
/* Inverse resolution of timespec timestamps (in units per second), |
| 42 |
and log base 10 of the inverse resolution. */ |
| 43 |
|
| 44 |
enum { TIMESPEC_HZ = 1000000000 }; |
| 45 |
enum { LOG10_TIMESPEC_HZ = 9 }; |
| 46 |
|
| 47 |
/* Obsolescent names for backward compatibility. |
| 48 |
They are misnomers, because TIMESPEC_RESOLUTION is not a resolution. */ |
| 49 |
|
| 50 |
enum { TIMESPEC_RESOLUTION = TIMESPEC_HZ }; |
| 51 |
enum { LOG10_TIMESPEC_RESOLUTION = LOG10_TIMESPEC_HZ }; |
| 52 |
|
| 53 |
/* Return a timespec with seconds S and nanoseconds NS. */ |
| 54 |
|
| 55 |
_GL_TIMESPEC_INLINE struct timespec |
| 56 |
make_timespec (time_t s, long int ns) |
| 57 |
{ |
| 58 |
return (struct timespec) { .tv_sec = s, .tv_nsec = ns }; |
| 59 |
} |
| 60 |
|
| 61 |
/* Return negative, zero, positive if A < B, A == B, A > B, respectively. */ |
| 62 |
|
| 63 |
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE |
| 64 |
timespec_cmp (struct timespec a, struct timespec b) |
| 65 |
{ |
| 66 |
return 2 * _GL_CMP (a.tv_sec, b.tv_sec) + _GL_CMP (a.tv_nsec, b.tv_nsec); |
| 67 |
} |
| 68 |
|
| 69 |
/* Return -1, 0, 1, depending on the sign of A. A.tv_nsec must be |
| 70 |
nonnegative. */ |
| 71 |
_GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE |
| 72 |
timespec_sign (struct timespec a) |
| 73 |
{ |
| 74 |
return _GL_CMP (a.tv_sec, 0) + (!a.tv_sec & !!a.tv_nsec); |
| 75 |
} |
| 76 |
|
| 77 |
struct timespec timespec_add (struct timespec, struct timespec) |
| 78 |
_GL_ATTRIBUTE_CONST; |
| 79 |
struct timespec timespec_sub (struct timespec, struct timespec) |
| 80 |
_GL_ATTRIBUTE_CONST; |
| 81 |
struct timespec dtotimespec (double) |
| 82 |
_GL_ATTRIBUTE_CONST; |
| 83 |
|
| 84 |
/* Return an approximation to A, of type 'double'. */ |
| 85 |
_GL_TIMESPEC_INLINE double |
| 86 |
timespectod (struct timespec a) |
| 87 |
{ |
| 88 |
return a.tv_sec + a.tv_nsec / 1e9; |
| 89 |
} |
| 90 |
|
| 91 |
long int gettime_res (void); |
| 92 |
struct timespec current_timespec (void); |
| 93 |
void gettime (struct timespec *) _GL_ARG_NONNULL ((1)); |
| 94 |
int settime (struct timespec const *) _GL_ARG_NONNULL ((1)); |
| 95 |
|
| 96 |
#ifdef __cplusplus |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
_GL_INLINE_HEADER_END |
| 101 |
|
| 102 |
#endif |