*-map tests: Fix compilation error.
[gnulib.git] / lib / xtime.h
blob9edd12392db91ee880b81c521706230e68f6d2b2
1 /* xtime -- extended-resolution integer timestamps
3 Copyright (C) 2005-2006, 2009-2019 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 3 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
18 /* Written by Paul Eggert. */
20 #ifndef XTIME_H_
21 #define XTIME_H_ 1
23 #ifndef _GL_INLINE_HEADER_BEGIN
24 #error "Please include config.h first."
25 #endif
26 _GL_INLINE_HEADER_BEGIN
27 #ifndef XTIME_INLINE
28 # define XTIME_INLINE _GL_INLINE
29 #endif
31 /* xtime_t is a signed type used for timestamps. It is an integer
32 type that is a count of nanoseconds -- except for obsolescent hosts
33 without sufficiently-wide integers, where it is a count of
34 seconds. */
35 #if HAVE_LONG_LONG_INT
36 typedef long long int xtime_t;
37 # define XTIME_PRECISION 1000000000
38 #else
39 # include <limits.h>
40 typedef long int xtime_t;
41 # if LONG_MAX >> 31 >> 31 == 0
42 # define XTIME_PRECISION 1
43 # else
44 # define XTIME_PRECISION 1000000000
45 # endif
46 #endif
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
52 /* Return an extended time value that contains S seconds and NS
53 nanoseconds. */
54 XTIME_INLINE xtime_t
55 xtime_make (xtime_t s, long int ns)
57 const long int giga = 1000 * 1000 * 1000;
58 s += ns / giga;
59 ns %= giga;
60 if (XTIME_PRECISION == 1)
61 return s;
62 else
63 return XTIME_PRECISION * s + ns;
66 /* Return the number of seconds in T, which must be nonnegative. */
67 XTIME_INLINE xtime_t
68 xtime_nonnegative_sec (xtime_t t)
70 return t / XTIME_PRECISION;
73 /* Return the number of seconds in T. */
74 XTIME_INLINE xtime_t
75 xtime_sec (xtime_t t)
77 return (XTIME_PRECISION == 1
78 ? t
79 : t < 0
80 ? (t + XTIME_PRECISION - 1) / XTIME_PRECISION - 1
81 : xtime_nonnegative_sec (t));
84 /* Return the number of nanoseconds in T, which must be nonnegative. */
85 XTIME_INLINE long int
86 xtime_nonnegative_nsec (xtime_t t)
88 return t % XTIME_PRECISION;
91 /* Return the number of nanoseconds in T. */
92 XTIME_INLINE long int
93 xtime_nsec (xtime_t t)
95 long int ns = t % XTIME_PRECISION;
96 if (ns < 0)
97 ns += XTIME_PRECISION;
98 return ns;
101 #ifdef __cplusplus
103 #endif
105 #endif