Use Linux 6.6 in build-many-glibcs.py
[glibc.git] / support / timespec.h
blob9559836d4c7f7c3dc661c53b5218357c30c4a160
1 /* Useful functions for tests that use struct timespec.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef SUPPORT_TIMESPEC_H
20 #define SUPPORT_TIMESPEC_H
22 #include <stdio.h>
23 #include <time.h>
24 #include <support/check.h>
25 #include <support/xtime.h>
27 static inline struct timespec
28 make_timespec (time_t s, long int ns)
30 struct timespec r;
31 r.tv_sec = s;
32 r.tv_nsec = ns;
33 return r;
36 enum { TIMESPEC_HZ = 1000000000 };
38 #ifndef __USE_TIME_BITS64
39 struct timespec timespec_add (struct timespec, struct timespec)
40 __attribute__((const));
41 struct timespec timespec_sub (struct timespec, struct timespec)
42 __attribute__((const));
44 void test_timespec_before_impl (const char *file, int line,
45 struct timespec left,
46 struct timespec right);
48 void test_timespec_equal_or_after_impl (const char *file, int line,
49 struct timespec left,
50 struct timespec right);
52 time_t support_timespec_ns (struct timespec time);
54 struct timespec support_timespec_normalize (struct timespec time);
56 int support_timespec_check_in_range (struct timespec expected,
57 struct timespec observed,
58 double lower_bound, double upper_bound);
60 struct timespec dtotimespec (double sec) __attribute__((const));
62 #else
63 struct timespec __REDIRECT (timespec_add, (struct timespec, struct timespec),
64 timespec_add_time64);
65 struct timespec __REDIRECT (timespec_sub, (struct timespec, struct timespec),
66 timespec_sub_time64);
67 void __REDIRECT (test_timespec_before_impl, (const char *file, int line,
68 struct timespec left,
69 struct timespec right),
70 test_timespec_before_impl_time64);
71 void __REDIRECT (test_timespec_equal_or_after_impl, (const char *f, int line,
72 struct timespec left,
73 struct timespec right),
74 test_timespec_equal_or_after_impl_time64);
76 time_t __REDIRECT (support_timespec_ns, (struct timespec time),
77 support_timespec_ns_time64);
79 struct timespec __REDIRECT (support_timespec_normalize, (struct timespec time),
80 support_timespec_normalize_time64);
82 int __REDIRECT (support_timespec_check_in_range, (struct timespec expected,
83 struct timespec observed,
84 double lower_bound,
85 double upper_bound),
86 support_timespec_check_in_range_time64);
88 struct timespec __REDIRECT (dtotimespec, (double sec), dtotimespec_time64);
89 #endif
91 /* Check that the timespec on the left represents a time before the
92 time on the right. */
93 #define TEST_TIMESPEC_BEFORE(left, right) \
94 test_timespec_before_impl (__FILE__, __LINE__, (left), (right))
96 #define TEST_TIMESPEC_BEFORE_NOW(left, clockid) \
97 ({ \
98 struct timespec now; \
99 const int saved_errno = errno; \
100 xclock_gettime ((clockid), &now); \
101 TEST_TIMESPEC_BEFORE ((left), now); \
102 errno = saved_errno; \
105 /* Check that the timespec on the left represents a time equal to or
106 after the time on the right. */
107 #define TEST_TIMESPEC_EQUAL_OR_AFTER(left, right) \
108 test_timespec_equal_or_after_impl (__FILE__, __LINE__, left, right)
110 #define TEST_TIMESPEC_NOW_OR_AFTER(clockid, right) \
111 ({ \
112 struct timespec now; \
113 const int saved_errno = errno; \
114 xclock_gettime ((clockid), &now); \
115 TEST_TIMESPEC_EQUAL_OR_AFTER (now, (right)); \
116 errno = saved_errno; \
119 #endif /* SUPPORT_TIMESPEC_H */