Commit | Line | Data |
---|---|---|
c8fff863 PE |
1 | /* timespec -- System time interface |
2 | ||
ab422c4d | 3 | Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2013 Free Software |
c8fff863 PE |
4 | Foundation, Inc. |
5 | ||
6 | This program is free software: you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program 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 General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #if ! defined TIMESPEC_H | |
20 | # define TIMESPEC_H | |
21 | ||
22 | # include <time.h> | |
23 | ||
a0d4efe9 PE |
24 | _GL_INLINE_HEADER_BEGIN |
25 | #ifndef _GL_TIMESPEC_INLINE | |
26 | # define _GL_TIMESPEC_INLINE _GL_INLINE | |
27 | #endif | |
28 | ||
43aac990 PE |
29 | /* Resolution of timespec time stamps (in units per second), and log |
30 | base 10 of the resolution. */ | |
31 | ||
32 | enum { TIMESPEC_RESOLUTION = 1000000000 }; | |
33 | enum { LOG10_TIMESPEC_RESOLUTION = 9 }; | |
34 | ||
35 | /* Return a timespec with seconds S and nanoseconds NS. */ | |
36 | ||
37 | _GL_TIMESPEC_INLINE struct timespec | |
38 | make_timespec (time_t s, long int ns) | |
39 | { | |
40 | struct timespec r; | |
41 | r.tv_sec = s; | |
42 | r.tv_nsec = ns; | |
43 | return r; | |
44 | } | |
45 | ||
c8fff863 PE |
46 | /* Return negative, zero, positive if A < B, A == B, A > B, respectively. |
47 | ||
48 | For each time stamp T, this code assumes that either: | |
49 | ||
50 | * T.tv_nsec is in the range 0..999999999; or | |
51 | * T.tv_sec corresponds to a valid leap second on a host that supports | |
52 | leap seconds, and T.tv_nsec is in the range 1000000000..1999999999; or | |
53 | * T.tv_sec is the minimum time_t value and T.tv_nsec is -1; or | |
54 | T.tv_sec is the maximum time_t value and T.tv_nsec is 2000000000. | |
55 | This allows for special struct timespec values that are less or | |
56 | greater than all possible valid time stamps. | |
57 | ||
58 | In all these cases, it is safe to subtract two tv_nsec values and | |
59 | convert the result to integer without worrying about overflow on | |
60 | any platform of interest to the GNU project, since all such | |
61 | platforms have 32-bit int or wider. | |
62 | ||
63 | Replacing "(int) (a.tv_nsec - b.tv_nsec)" with something like | |
64 | "a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec" would cause | |
65 | this function to work in some cases where the above assumption is | |
66 | violated, but not in all cases (e.g., a.tv_sec==1, a.tv_nsec==-2, | |
67 | b.tv_sec==0, b.tv_nsec==999999999) and is arguably not worth the | |
68 | extra instructions. Using a subtraction has the advantage of | |
69 | detecting some invalid cases on platforms that detect integer | |
70 | overflow. | |
71 | ||
72 | The (int) cast avoids a gcc -Wconversion warning. */ | |
73 | ||
a0d4efe9 | 74 | _GL_TIMESPEC_INLINE int |
c8fff863 PE |
75 | timespec_cmp (struct timespec a, struct timespec b) |
76 | { | |
77 | return (a.tv_sec < b.tv_sec ? -1 | |
78 | : a.tv_sec > b.tv_sec ? 1 | |
79 | : (int) (a.tv_nsec - b.tv_nsec)); | |
80 | } | |
81 | ||
82 | /* Return -1, 0, 1, depending on the sign of A. A.tv_nsec must be | |
83 | nonnegative. */ | |
a0d4efe9 | 84 | _GL_TIMESPEC_INLINE int |
c8fff863 PE |
85 | timespec_sign (struct timespec a) |
86 | { | |
87 | return a.tv_sec < 0 ? -1 : a.tv_sec || a.tv_nsec; | |
88 | } | |
89 | ||
2f93ecce PE |
90 | struct timespec timespec_add (struct timespec, struct timespec) |
91 | _GL_ATTRIBUTE_CONST; | |
92 | struct timespec timespec_sub (struct timespec, struct timespec) | |
93 | _GL_ATTRIBUTE_CONST; | |
94 | struct timespec dtotimespec (double) | |
95 | _GL_ATTRIBUTE_CONST; | |
c8fff863 PE |
96 | |
97 | /* Return an approximation to A, of type 'double'. */ | |
a0d4efe9 | 98 | _GL_TIMESPEC_INLINE double |
c8fff863 PE |
99 | timespectod (struct timespec a) |
100 | { | |
101 | return a.tv_sec + a.tv_nsec / 1e9; | |
102 | } | |
103 | ||
104 | void gettime (struct timespec *); | |
105 | int settime (struct timespec const *); | |
106 | ||
a0d4efe9 PE |
107 | _GL_INLINE_HEADER_END |
108 | ||
c8fff863 | 109 | #endif |