Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/timespec.h @ 15332:c762e3063952
dtotimespec: new module
* lib/timespec.h (dtotimespec): New decl.
* lib/dtotimespec.c, modules/dtotimespec: New files.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Thu, 30 Jun 2011 15:14:25 -0700 |
parents | d214a85acda4 |
children | f03da5e72c61 |
rev | line source |
---|---|
3766
6ed5059e1333
new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents:
3765
diff
changeset
|
1 /* timespec -- System time interface |
6ed5059e1333
new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents:
3765
diff
changeset
|
2 |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
13476
diff
changeset
|
3 Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2011 Free Software |
12559
c2cbabec01dd
update nearly all FSF copyright year lists to include 2010
Jim Meyering <meyering@redhat.com>
parents:
12518
diff
changeset
|
4 Foundation, Inc. |
3765 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8146
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
3765 | 7 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8146
diff
changeset
|
8 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8146
diff
changeset
|
9 (at your option) any later version. |
3765 | 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8146
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
3765 | 18 |
3766
6ed5059e1333
new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents:
3765
diff
changeset
|
19 #if ! defined TIMESPEC_H |
6ed5059e1333
new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents:
3765
diff
changeset
|
20 # define TIMESPEC_H |
3765 | 21 |
7897
b7a83a69ac23
* MODULES.html.sh (Support for systems lacking POSIX:2001): New
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
22 # include <time.h> |
3765 | 23 |
6237
d81a8251a9b0
* MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
24 /* Return negative, zero, positive if A < B, A == B, A > B, respectively. |
13476
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
25 |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
26 For each time stamp T, this code assumes that either: |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
27 |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
28 * T.tv_nsec is in the range 0..999999999; or |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
29 * T.tv_sec corresponds to a valid leap second on a host that supports |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
30 leap seconds, and T.tv_nsec is in the range 1000000000..1999999999; or |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
31 * T.tv_sec is the minimum time_t value and T.tv_nsec is -1; or |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
32 T.tv_sec is the maximum time_t value and T.tv_nsec is 2000000000. |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
33 This allows for special struct timespec values that are less or |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
34 greater than all possible valid time stamps. |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
35 |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
36 In all these cases, it is safe to subtract two tv_nsec values and |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
37 convert the result to integer without worrying about overflow on |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
38 any platform of interest to the GNU project, since all such |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
39 platforms have 32-bit int or wider. |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
40 |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
41 Replacing "(int) (a.tv_nsec - b.tv_nsec)" with something like |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
42 "a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec" would cause |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
43 this function to work in some cases where the above assumption is |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
44 violated, but not in all cases (e.g., a.tv_sec==1, a.tv_nsec==-2, |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
45 b.tv_sec==0, b.tv_nsec==999999999) and is arguably not worth the |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
46 extra instructions. Using a subtraction has the advantage of |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
47 detecting some invalid cases on platforms that detect integer |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
48 overflow. |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
49 |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
50 The (int) cast avoids a gcc -Wconversion warning. */ |
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
51 |
6237
d81a8251a9b0
* MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
52 static inline int |
d81a8251a9b0
* MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
53 timespec_cmp (struct timespec a, struct timespec b) |
d81a8251a9b0
* MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
54 { |
d81a8251a9b0
* MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
55 return (a.tv_sec < b.tv_sec ? -1 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12207
diff
changeset
|
56 : a.tv_sec > b.tv_sec ? 1 |
13476
5767ef1b4889
timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
57 : (int) (a.tv_nsec - b.tv_nsec)); |
6237
d81a8251a9b0
* MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
58 } |
3766
6ed5059e1333
new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents:
3765
diff
changeset
|
59 |
15330
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
60 /* Return -1, 0, 1, depending on the sign of A. A.tv_nsec must be |
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
61 nonnegative. */ |
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
62 static inline int |
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
63 timespec_sign (struct timespec a) |
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
64 { |
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
65 return a.tv_sec < 0 ? -1 : a.tv_sec || a.tv_nsec; |
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
66 } |
15ab18909697
* lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
67 |
15332 | 68 struct timespec dtotimespec (double); |
69 | |
15331
d214a85acda4
* lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
15330
diff
changeset
|
70 /* Return an approximation to A, of type 'double'. */ |
d214a85acda4
* lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
15330
diff
changeset
|
71 static inline double |
d214a85acda4
* lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
15330
diff
changeset
|
72 timespectod (struct timespec a) |
d214a85acda4
* lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
15330
diff
changeset
|
73 { |
d214a85acda4
* lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
15330
diff
changeset
|
74 return a.tv_sec + a.tv_nsec / 1e9; |
d214a85acda4
* lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
15330
diff
changeset
|
75 } |
d214a85acda4
* lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents:
15330
diff
changeset
|
76 |
5662
42df9db003cc
* modules/gethrxtime: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4989
diff
changeset
|
77 void gettime (struct timespec *); |
3766
6ed5059e1333
new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents:
3765
diff
changeset
|
78 int settime (struct timespec const *); |
6ed5059e1333
new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents:
3765
diff
changeset
|
79 |
3765 | 80 #endif |