annotate lib/timespec.h @ 17426:90f3d53e01f5

sig2str: port to C++ * lib/sig2str.h (sig2str, str2sig): Declare as extern "C". Reported by Daniel J Sebald in <http://lists.gnu.org/archive/html/bug-gnulib/2013-06/msg00000.html>.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 02 Jun 2013 11:52:41 -0700
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 17028
diff changeset
3 Copyright (C) 2000, 2002, 2004-2005, 2007, 2009-2013 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
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 GNU General Public License for more details.
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23
17024
fc5763921b7d timespec: use extern-inline
Paul Eggert <eggert@cs.ucla.edu>
parents: 16976
diff changeset
24 _GL_INLINE_HEADER_BEGIN
17028
ce2683fe3994 Keep the extern-inline macros closer together.
Paul Eggert <eggert@cs.ucla.edu>
parents: 17024
diff changeset
25 #ifndef _GL_TIMESPEC_INLINE
ce2683fe3994 Keep the extern-inline macros closer together.
Paul Eggert <eggert@cs.ucla.edu>
parents: 17024
diff changeset
26 # define _GL_TIMESPEC_INLINE _GL_INLINE
ce2683fe3994 Keep the extern-inline macros closer together.
Paul Eggert <eggert@cs.ucla.edu>
parents: 17024
diff changeset
27 #endif
17024
fc5763921b7d timespec: use extern-inline
Paul Eggert <eggert@cs.ucla.edu>
parents: 16976
diff changeset
28
6237
d81a8251a9b0 * MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
29 /* 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
30
5767ef1b4889 timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
31 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
32
5767ef1b4889 timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
33 * 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
34 * 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
35 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
36 * 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
37 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
38 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
39 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
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 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
42 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
43 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
44 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
45
5767ef1b4889 timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
46 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
47 "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
48 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
49 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
50 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
51 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
52 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
53 overflow.
5767ef1b4889 timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
54
5767ef1b4889 timespec: use cast and not conditional, as truncation isn't possible
Paul R. Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
55 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
56
17024
fc5763921b7d timespec: use extern-inline
Paul Eggert <eggert@cs.ucla.edu>
parents: 16976
diff changeset
57 _GL_TIMESPEC_INLINE int
6237
d81a8251a9b0 * MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
58 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
59 {
d81a8251a9b0 * MODULES.html.sh (File system functions): Add stat-time.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
60 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
61 : 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
62 : (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
63 }
3766
6ed5059e1333 new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents: 3765
diff changeset
64
15330
15ab18909697 * lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 14079
diff changeset
65 /* 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
66 nonnegative. */
17024
fc5763921b7d timespec: use extern-inline
Paul Eggert <eggert@cs.ucla.edu>
parents: 16976
diff changeset
67 _GL_TIMESPEC_INLINE int
15330
15ab18909697 * lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 14079
diff changeset
68 timespec_sign (struct timespec a)
15ab18909697 * lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 14079
diff changeset
69 {
15ab18909697 * lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 14079
diff changeset
70 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
71 }
15ab18909697 * lib/timespec.h (timespec_sign): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 14079
diff changeset
72
16976
7b9ea0c1be5a timespec: mark functions with const attributes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
73 struct timespec timespec_add (struct timespec, struct timespec)
7b9ea0c1be5a timespec: mark functions with const attributes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
74 _GL_ATTRIBUTE_CONST;
7b9ea0c1be5a timespec: mark functions with const attributes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
75 struct timespec timespec_sub (struct timespec, struct timespec)
7b9ea0c1be5a timespec: mark functions with const attributes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
76 _GL_ATTRIBUTE_CONST;
7b9ea0c1be5a timespec: mark functions with const attributes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
77 struct timespec dtotimespec (double)
7b9ea0c1be5a timespec: mark functions with const attributes
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
78 _GL_ATTRIBUTE_CONST;
15332
c762e3063952 dtotimespec: new module
Paul Eggert <eggert@cs.ucla.edu>
parents: 15331
diff changeset
79
15331
d214a85acda4 * lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15330
diff changeset
80 /* Return an approximation to A, of type 'double'. */
17024
fc5763921b7d timespec: use extern-inline
Paul Eggert <eggert@cs.ucla.edu>
parents: 16976
diff changeset
81 _GL_TIMESPEC_INLINE double
15331
d214a85acda4 * lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15330
diff changeset
82 timespectod (struct timespec a)
d214a85acda4 * lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15330
diff changeset
83 {
d214a85acda4 * lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15330
diff changeset
84 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
85 }
d214a85acda4 * lib/timespec.h (timespectod): New inline function.
Paul Eggert <eggert@cs.ucla.edu>
parents: 15330
diff changeset
86
5662
42df9db003cc * modules/gethrxtime: New file.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4989
diff changeset
87 void gettime (struct timespec *);
3766
6ed5059e1333 new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents: 3765
diff changeset
88 int settime (struct timespec const *);
6ed5059e1333 new macros/decls, from Paul Eggert
Jim Meyering <jim@meyering.net>
parents: 3765
diff changeset
89
17024
fc5763921b7d timespec: use extern-inline
Paul Eggert <eggert@cs.ucla.edu>
parents: 16976
diff changeset
90 _GL_INLINE_HEADER_END
fc5763921b7d timespec: use extern-inline
Paul Eggert <eggert@cs.ucla.edu>
parents: 16976
diff changeset
91
3765
bcb84a761933 renamed from nanosleep.h
Jim Meyering <jim@meyering.net>
parents:
diff changeset
92 #endif