]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (c) 2003 Nick Clifford (zaf@nrc.co.nz), Jan 25, 2003 |
2 | .\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl), Aug 24, 2003 | |
16fa5781 | 3 | .\" Copyright (c) 2020 Michael Kerrisk <mtk.manpages@gmail.com> |
fea681da | 4 | .\" |
93015253 | 5 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
6 | .\" Permission is granted to make and distribute verbatim copies of this |
7 | .\" manual provided the copyright notice and this permission notice are | |
8 | .\" preserved on all copies. | |
9 | .\" | |
10 | .\" Permission is granted to copy and distribute modified versions of this | |
11 | .\" manual under the conditions for verbatim copying, provided that the | |
12 | .\" entire resulting derived work is distributed under the terms of a | |
13 | .\" permission notice identical to this one. | |
c13182ef | 14 | .\" |
fea681da MK |
15 | .\" Since the Linux kernel and libraries are constantly changing, this |
16 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
17 | .\" responsibility for errors or omissions, or for damages resulting from | |
18 | .\" the use of the information contained herein. The author(s) may not | |
19 | .\" have taken the same level of care in the production of this manual, | |
20 | .\" which is licensed free of charge, as they might when working | |
21 | .\" professionally. | |
c13182ef | 22 | .\" |
fea681da MK |
23 | .\" Formatted or processed versions of this manual, if unaccompanied by |
24 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 25 | .\" %%%LICENSE_END |
fea681da MK |
26 | .\" |
27 | .\" 2003-08-23 Martin Schulze <joey@infodrom.org> improvements | |
28 | .\" 2003-08-24 aeb, large parts rewritten | |
29 | .\" 2004-08-06 Christoph Lameter <clameter@sgi.com>, SMP note | |
30 | .\" | |
e8426ca2 | 31 | .TH CLOCK_GETRES 2 2020-04-11 "" "Linux Programmer's Manual" |
fea681da MK |
32 | .SH NAME |
33 | clock_getres, clock_gettime, clock_settime \- clock and time functions | |
34 | .SH SYNOPSIS | |
35 | .B #include <time.h> | |
68e4db0a | 36 | .PP |
ba1c6b20 | 37 | .BI "int clock_getres(clockid_t " clockid ", struct timespec *" res ); |
dbfe9c70 | 38 | .PP |
ba1c6b20 | 39 | .BI "int clock_gettime(clockid_t " clockid ", struct timespec *" tp ); |
dbfe9c70 | 40 | .PP |
ba1c6b20 | 41 | .BI "int clock_settime(clockid_t " clockid ", const struct timespec *" tp ); |
68e4db0a | 42 | .PP |
02959991 | 43 | Link with \fI\-lrt\fP (only for glibc versions before 2.17). |
68e4db0a | 44 | .PP |
cc4615cc MK |
45 | .in -4n |
46 | Feature Test Macro Requirements for glibc (see | |
47 | .BR feature_test_macros (7)): | |
48 | .in | |
68e4db0a | 49 | .PP |
cc4615cc MK |
50 | .ad l |
51 | .BR clock_getres (), | |
52 | .BR clock_gettime (), | |
53 | .BR clock_settime (): | |
8260c01f | 54 | .RS |
cc4615cc | 55 | _POSIX_C_SOURCE\ >=\ 199309L |
8260c01f | 56 | .RE |
cc4615cc | 57 | .ad b |
fea681da MK |
58 | .SH DESCRIPTION |
59 | The function | |
60 | .BR clock_getres () | |
61 | finds the resolution (precision) of the specified clock | |
ba1c6b20 | 62 | .IR clockid , |
fea681da MK |
63 | and, if |
64 | .I res | |
0c2ec4f1 | 65 | is non-NULL, stores it in the \fIstruct timespec\fP pointed to by |
fea681da MK |
66 | .IR res . |
67 | The resolution of clocks depends on the implementation and cannot be | |
68 | configured by a particular process. | |
69 | If the time value pointed to by the argument | |
70 | .I tp | |
71 | of | |
72 | .BR clock_settime () | |
73 | is not a multiple of | |
74 | .IR res , | |
75 | then it is truncated to a multiple of | |
76 | .IR res . | |
77 | .PP | |
78 | The functions | |
79 | .BR clock_gettime () | |
80 | and | |
81 | .BR clock_settime () | |
82 | retrieve and set the time of the specified clock | |
ba1c6b20 | 83 | .IR clockid . |
fea681da MK |
84 | .PP |
85 | The | |
86 | .I res | |
87 | and | |
88 | .I tp | |
89 | arguments are | |
f19a0f03 | 90 | .I timespec |
4e836144 | 91 | structures, as specified in |
c13182ef | 92 | .IR <time.h> : |
0ffeaeae | 93 | .PP |
3ad4ddcd | 94 | .in +4n |
0ffeaeae | 95 | .EX |
fea681da | 96 | struct timespec { |
cc4615cc MK |
97 | time_t tv_sec; /* seconds */ |
98 | long tv_nsec; /* nanoseconds */ | |
fea681da | 99 | }; |
0ffeaeae | 100 | .EE |
3ad4ddcd | 101 | .in |
fea681da | 102 | .PP |
c13182ef | 103 | The |
ba1c6b20 | 104 | .I clockid |
fea681da MK |
105 | argument is the identifier of the particular clock on which to act. |
106 | A clock may be system-wide and hence visible for all processes, or | |
107 | per-process if it measures time only within a single process. | |
dd3568a1 | 108 | .PP |
6f36deb4 | 109 | All implementations support the system-wide real-time clock, |
fea681da MK |
110 | which is identified by |
111 | .BR CLOCK_REALTIME . | |
112 | Its time represents seconds and nanoseconds since the Epoch. | |
113 | When its time is changed, timers for a relative interval are | |
114 | unaffected, but timers for an absolute point in time are affected. | |
dd3568a1 | 115 | .PP |
c13182ef MK |
116 | More clocks may be implemented. |
117 | The interpretation of the | |
fea681da | 118 | corresponding time values and the effect on timers is unspecified. |
dd3568a1 | 119 | .PP |
5260fe08 | 120 | Sufficiently recent versions of glibc and the Linux kernel |
fea681da MK |
121 | support the following clocks: |
122 | .TP | |
123 | .B CLOCK_REALTIME | |
41043c0b | 124 | A settable system-wide clock that measures real (i.e., wall-clock) time. |
fea681da | 125 | Setting this clock requires appropriate privileges. |
22594a3c MK |
126 | This clock is affected by discontinuous jumps in the system time |
127 | (e.g., if the system administrator manually changes the clock), | |
128 | and by the incremental adjustments performed by | |
65a0c012 | 129 | .BR adjtime (3) |
22594a3c | 130 | and NTP. |
f5546fcd | 131 | .TP |
eb6567fb MK |
132 | .BR CLOCK_REALTIME_ALARM " (since Linux 3.0; Linux-specific)" |
133 | Like | |
134 | .BR CLOCK_REALTIME , | |
135 | but not settable. | |
136 | See | |
137 | .BR timer_create (2) | |
138 | for further details. | |
139 | .TP | |
55f749ae CH |
140 | .BR CLOCK_REALTIME_COARSE " (since Linux 2.6.32; Linux-specific)" |
141 | .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3 | |
1d33f7d7 MK |
142 | A faster but less precise version of |
143 | .BR CLOCK_REALTIME . | |
14df252b | 144 | This clock is not settable. |
55f749ae | 145 | Use when you need very fast, but not fine-grained timestamps. |
c74bc519 MK |
146 | Requires per-architecture support, |
147 | and probably also architecture support for this flag in the | |
148 | .BR vdso (7). | |
55f749ae | 149 | .TP |
3eee7515 | 150 | .BR CLOCK_TAI " (since Linux 3.10; Linux-specific)" |
da9fe87d | 151 | .\" commit 1ff3c9677bff7e468e0c487d0ffefe4e901d33f4 |
9d69bebb MK |
152 | A nonsettable system-wide clock derived from wall-clock time |
153 | but ignoring leap seconds. | |
da9fe87d MK |
154 | This clock does |
155 | not experience discontinuities and backwards jumps caused by NTP | |
156 | inserting leap seconds as | |
3eee7515 BP |
157 | .BR CLOCK_REALTIME |
158 | does. | |
159 | .IP | |
160 | The acronym TAI refers to International Atomic Time. | |
161 | .TP | |
fea681da | 162 | .B CLOCK_MONOTONIC |
a04e44bd MK |
163 | A nonsettable system-wide clock that |
164 | represents monotonic time since\(emas described | |
7d882a4a MK |
165 | by POSIX\(em"some unspecified point in the past". |
166 | On Linux, that point corresponds to the number of seconds that the system | |
167 | has been running since it was booted. | |
168 | .IP | |
169 | The | |
170 | .B CLOCK_MONOTONIC | |
171 | clock is not affected by discontinuous jumps in the system time | |
63f0f3d9 MK |
172 | (e.g., if the system administrator manually changes the clock), |
173 | but is affected by the incremental adjustments performed by | |
65a0c012 | 174 | .BR adjtime (3) |
63f0f3d9 | 175 | and NTP. |
f224d2bc | 176 | This clock does not count time that the system is suspended. |
7b02075b HD |
177 | All |
178 | .B CLOCK_MONOTONIC | |
179 | variants guarantee that the time returned by consecutive calls will not go | |
7b9f3195 MK |
180 | backwards, but successive calls may\(emdepending on the architecture\(emreturn |
181 | identical (not-increased) time values. | |
f5546fcd | 182 | .TP |
55f749ae CH |
183 | .BR CLOCK_MONOTONIC_COARSE " (since Linux 2.6.32; Linux-specific)" |
184 | .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3 | |
1d33f7d7 MK |
185 | A faster but less precise version of |
186 | .BR CLOCK_MONOTONIC . | |
55f749ae | 187 | Use when you need very fast, but not fine-grained timestamps. |
c74bc519 MK |
188 | Requires per-architecture support, |
189 | and probably also architecture support for this flag in the | |
190 | .BR vdso (7). | |
fea681da | 191 | .TP |
1ba3fba8 MK |
192 | .BR CLOCK_MONOTONIC_RAW " (since Linux 2.6.28; Linux-specific)" |
193 | .\" Added in commit 2d42244ae71d6c7b0884b5664cf2eda30fb2ae68, John Stultz | |
194 | Similar to | |
195 | .BR CLOCK_MONOTONIC , | |
196 | but provides access to a raw hardware-based time | |
63f0f3d9 MK |
197 | that is not subject to NTP adjustments or |
198 | the incremental adjustments performed by | |
65a0c012 | 199 | .BR adjtime (3). |
4eae8eb7 | 200 | This clock does not count time that the system is suspended. |
1ba3fba8 | 201 | .TP |
b28712e1 | 202 | .BR CLOCK_BOOTTIME " (since Linux 2.6.39; Linux-specific)" |
a5d9ce65 MK |
203 | .\" commit 7fdd7f89006dd5a4c702fa0ce0c272345fa44ae0 |
204 | .\" commit 70a08cca1227dc31c784ec930099a4417a06e7d0 | |
65ff4e23 | 205 | A nonsettable system-wide clock that is identical to |
b28712e1 | 206 | .BR CLOCK_MONOTONIC , |
2c16f1bc | 207 | except that it also includes any time that the system is suspended. |
a5d9ce65 MK |
208 | This allows applications to get a suspend-aware monotonic clock |
209 | without having to deal with the complications of | |
210 | .BR CLOCK_REALTIME , | |
211 | which may have discontinuities if the time is changed using | |
0bbe872f MK |
212 | .BR settimeofday (2) |
213 | or similar. | |
b28712e1 | 214 | .TP |
eb6567fb MK |
215 | .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.0; Linux-specific)" |
216 | Like | |
217 | .BR CLOCK_BOOTTIME . | |
218 | See | |
219 | .BR timer_create (2) | |
220 | for further details. | |
221 | .TP | |
783d6cb4 | 222 | .BR CLOCK_PROCESS_CPUTIME_ID " (since Linux 2.6.12)" |
ac90b589 | 223 | This is a clock that measures CPU time consumed by this process |
c009a15c | 224 | (i.e., CPU time consumed by all threads in the process). |
71b7e2a5 | 225 | On Linux, this clock is not settable. |
fea681da | 226 | .TP |
783d6cb4 | 227 | .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)" |
ac90b589 | 228 | This is a clock that measures CPU time consumed by this thread. |
71b7e2a5 | 229 | On Linux, this clock is not settable. |
f377393e RC |
230 | .PP |
231 | Linux also implements dynamic clock instances as described below. | |
bd72f280 MK |
232 | .SS Dynamic clocks |
233 | In addition to the hard-coded System-V style clock IDs described above, | |
234 | Linux also supports | |
aab94212 MK |
235 | POSIX clock operations on certain character devices. |
236 | Such devices are | |
bd72f280 MK |
237 | called "dynamic" clocks, and are supported since Linux 2.6.39. |
238 | .PP | |
aab94212 | 239 | Using the appropriate macros, open file |
bd72f280 | 240 | descriptors may be converted into clock IDs and passed to |
f377393e RC |
241 | .BR clock_gettime (), |
242 | .BR clock_settime (), | |
243 | and | |
bd72f280 MK |
244 | .BR clock_adjtime (2). |
245 | The following example shows how to convert a file descriptor into a | |
246 | dynamic clock ID. | |
f377393e RC |
247 | .PP |
248 | .in +4n | |
249 | .EX | |
250 | #define CLOCKFD 3 | |
bd72f280 MK |
251 | #define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD) |
252 | #define CLOCKID_TO_FD(clk) ((unsigned int) ~((clk) >> 3)) | |
f377393e RC |
253 | |
254 | struct timeval tv; | |
255 | clockid_t clkid; | |
256 | int fd; | |
257 | ||
258 | fd = open("/dev/ptp0", O_RDWR); | |
259 | clkid = FD_TO_CLOCKID(fd); | |
260 | clock_gettime(clkid, &tv); | |
261 | .EE | |
262 | .in | |
47297adb | 263 | .SH RETURN VALUE |
e1d6264d | 264 | .BR clock_gettime (), |
cc83f4d1 | 265 | .BR clock_settime (), |
fea681da MK |
266 | and |
267 | .BR clock_getres () | |
268 | return 0 for success, or \-1 for failure (in which case | |
269 | .I errno | |
270 | is set appropriately). | |
271 | .SH ERRORS | |
272 | .TP | |
273 | .B EFAULT | |
274 | .I tp | |
275 | points outside the accessible address space. | |
276 | .TP | |
277 | .B EINVAL | |
c13182ef | 278 | The |
ba1c6b20 | 279 | .I clockid |
aab94212 | 280 | specified is invalid for one of two reasons. |
bd72f280 MK |
281 | Either the System-V style |
282 | hard coded positive value is out of range, or the dynamic clock ID | |
f377393e | 283 | does not refer to a valid instance of a clock object. |
a113440c MK |
284 | .\" Linux also gives this error on attempts to set CLOCK_PROCESS_CPUTIME_ID |
285 | .\" and CLOCK_THREAD_CPUTIME_ID, when probably the proper error should be | |
286 | .\" EPERM. | |
fea681da | 287 | .TP |
9f23d947 MK |
288 | .B EINVAL |
289 | .RB ( clock_settime ()): | |
290 | .I tp.tv_sec | |
291 | is negative or | |
9a82d4d9 | 292 | .I tp.tv_nsec |
9f23d947 MK |
293 | is outside the range [0..999,999,999]. |
294 | .TP | |
238442a2 MK |
295 | .B EINVAL |
296 | The | |
ba1c6b20 | 297 | .I clockid |
238442a2 MK |
298 | specified in a call to |
299 | .BR clock_settime () | |
300 | is not a settable clock. | |
301 | .TP | |
a48d1916 | 302 | .B ENOTSUP |
f873b375 ER |
303 | The operation is not supported by the dynamic POSIX clock device |
304 | specified. | |
305 | .TP | |
bccacb94 MK |
306 | .BR EINVAL " (since Linux 4.3)" |
307 | .\" commit e1d7ba8735551ed79c7a0463a042353574b96da3 | |
308 | A call to | |
309 | .BR clock_settime () | |
310 | with a | |
ba1c6b20 | 311 | .I clockid |
bccacb94 MK |
312 | of |
313 | .B CLOCK_REALTIME | |
314 | attempted to set the time to a value less than | |
315 | the current value of the | |
316 | .B CLOCK_MONOTONIC | |
317 | clock. | |
318 | .TP | |
f377393e | 319 | .B ENODEV |
bd72f280 | 320 | The hot-pluggable device (like USB for example) represented by a |
f377393e RC |
321 | dynamic |
322 | .I clk_id | |
323 | has disappeared after its character device was opened. | |
324 | .TP | |
fea681da MK |
325 | .B EPERM |
326 | .BR clock_settime () | |
327 | does not have permission to set the clock indicated. | |
f873b375 ER |
328 | .TP |
329 | .B EACCES | |
330 | .BR clock_settime () | |
331 | does not have write permission for the dynamic POSIX | |
332 | clock device indicated. | |
e272bb60 | 333 | .SH VERSIONS |
50b25c38 | 334 | These system calls first appeared in Linux 2.6. |
546e17ce ZL |
335 | .SH ATTRIBUTES |
336 | For an explanation of the terms used in this section, see | |
337 | .BR attributes (7). | |
338 | .TS | |
339 | allbox; | |
6a95087e | 340 | lbw32 lb lb |
546e17ce ZL |
341 | l l l. |
342 | Interface Attribute Value | |
343 | T{ | |
344 | .BR clock_getres (), | |
345 | .BR clock_gettime (), | |
346 | .BR clock_settime () | |
347 | T} Thread safety MT-Safe | |
348 | .TE | |
efeece04 | 349 | .sp 1 |
47297adb | 350 | .SH CONFORMING TO |
75ee6a07 | 351 | POSIX.1-2001, POSIX.1-2008, SUSv2. |
bd168648 | 352 | .PP |
2b2581ee MK |
353 | On POSIX systems on which these functions are available, the symbol |
354 | .B _POSIX_TIMERS | |
c84371c6 | 355 | is defined in \fI<unistd.h>\fP to a value greater than 0. |
2b2581ee MK |
356 | The symbols |
357 | .BR _POSIX_MONOTONIC_CLOCK , | |
358 | .BR _POSIX_CPUTIME , | |
359 | .B _POSIX_THREAD_CPUTIME | |
360 | indicate that | |
361 | .BR CLOCK_MONOTONIC , | |
362 | .BR CLOCK_PROCESS_CPUTIME_ID , | |
363 | .B CLOCK_THREAD_CPUTIME_ID | |
364 | are available. | |
365 | (See also | |
366 | .BR sysconf (3).) | |
19c98696 | 367 | .SH NOTES |
f086b6aa MK |
368 | POSIX.1 specifies the following: |
369 | .RS | |
370 | .PP | |
371 | Setting the value of the | |
372 | .B CLOCK_REALTIME | |
373 | clock via | |
e470433f | 374 | .BR clock_settime () |
f086b6aa MK |
375 | shall have no effect on threads that are blocked waiting for a relative time |
376 | service based upon this clock, including the | |
377 | .BR nanosleep () | |
378 | function; nor on the expiration of relative timers based upon this clock. | |
379 | Consequently, these time services shall expire when the requested relative | |
380 | interval elapses, independently of the new or old value of the clock. | |
a3847715 | 381 | .RE |
a2150697 MK |
382 | .PP |
383 | According to POSIX.1-2001, a process with "appropriate privileges" may set the | |
384 | .B CLOCK_PROCESS_CPUTIME_ID | |
385 | and | |
386 | .B CLOCK_THREAD_CPUTIME_ID | |
387 | clocks using | |
388 | .BR clock_settime (). | |
389 | On Linux, these clocks are not settable | |
390 | (i.e., no process has "appropriate privileges"). | |
391 | .\" See http://bugzilla.kernel.org/show_bug.cgi?id=11972 | |
f086b6aa | 392 | .\" |
f8c419a4 MK |
393 | .SS C library/kernel differences |
394 | On some architectures, an implementation of | |
395 | .BR clock_gettime () | |
396 | is provided in the | |
397 | .BR vdso (7). | |
398 | .\" | |
78638aae MK |
399 | .SS Historical note for SMP systems |
400 | Before Linux added kernel support for | |
fea681da MK |
401 | .B CLOCK_PROCESS_CPUTIME_ID |
402 | and | |
78638aae MK |
403 | .BR CLOCK_THREAD_CPUTIME_ID , |
404 | glibc implemented these clocks on many platforms using timer | |
405 | registers from the CPUs | |
fea681da MK |
406 | (TSC on i386, AR.ITC on Itanium). |
407 | These registers may differ between CPUs and as a consequence | |
408 | these clocks may return | |
409 | .B bogus results | |
410 | if a process is migrated to another CPU. | |
411 | .PP | |
f14ae16e | 412 | If the CPUs in an SMP system have different clock sources, then |
fea681da | 413 | there is no way to maintain a correlation between the timer registers since |
c13182ef | 414 | each CPU will run at a slightly different frequency. |
f14ae16e | 415 | If that is the case, then |
fea681da MK |
416 | .I clock_getcpuclockid(0) |
417 | will return | |
418 | .B ENOENT | |
c13182ef | 419 | to signify this condition. |
33a0ccb2 | 420 | The two clocks will then be useful only if it |
fea681da MK |
421 | can be ensured that a process stays on a certain CPU. |
422 | .PP | |
423 | The processors in an SMP system do not start all at exactly the same | |
424 | time and therefore the timer registers are typically running at an offset. | |
425 | Some architectures include code that attempts to limit these offsets on bootup. | |
426 | However, the code cannot guarantee to accurately tune the offsets. | |
427 | Glibc contains no provisions to deal with these offsets (unlike the Linux | |
c13182ef MK |
428 | Kernel). |
429 | Typically these offsets are small and therefore the effects may be | |
fea681da | 430 | negligible in most cases. |
efeece04 | 431 | .PP |
78638aae MK |
432 | Since glibc 2.4, |
433 | the wrapper functions for the system calls described in this page avoid | |
434 | the abovementioned problems by employing the kernel implementation of | |
435 | .B CLOCK_PROCESS_CPUTIME_ID | |
436 | and | |
437 | .BR CLOCK_THREAD_CPUTIME_ID , | |
438 | on systems that provide such an implementation | |
439 | (i.e., Linux 2.6.12 and later). | |
a14af333 | 440 | .SH EXAMPLES |
16fa5781 MK |
441 | The program below demonstrates the use of |
442 | .BR clock_gettime () | |
443 | and | |
444 | .BR clock_getres () | |
445 | with various clocks. | |
446 | This is an example of what we might see when running the program: | |
447 | .PP | |
448 | .in +4n | |
449 | .EX | |
450 | $ \fB./clock_times x\fP | |
bef940ca MK |
451 | CLOCK_REALTIME : 1585985459.446 (18356 days + 7h 30m 59s) |
452 | resolution: 0.000000001 | |
453 | CLOCK_TAI : 1585985496.447 (18356 days + 7h 31m 36s) | |
454 | resolution: 0.000000001 | |
455 | CLOCK_MONOTONIC: 52395.722 (14h 33m 15s) | |
456 | resolution: 0.000000001 | |
457 | CLOCK_BOOTTIME : 72691.019 (20h 11m 31s) | |
458 | resolution: 0.000000001 | |
16fa5781 MK |
459 | .EE |
460 | .in | |
461 | .SS Program source | |
462 | \& | |
463 | .EX | |
464 | /* clock_times.c | |
465 | ||
466 | Licensed under GNU General Public License v2 or later. | |
467 | */ | |
468 | #define _XOPEN_SOURCE 600 | |
469 | #include <time.h> | |
470 | #include <stdio.h> | |
471 | #include <stdlib.h> | |
472 | #include <stdbool.h> | |
473 | #include <unistd.h> | |
474 | ||
475 | #define SECS_IN_DAY (24 * 60 * 60) | |
476 | ||
477 | static void | |
478 | displayClock(clockid_t clock, char *name, bool showRes) | |
479 | { | |
480 | struct timespec ts; | |
481 | ||
482 | if (clock_gettime(clock, &ts) == \-1) { | |
483 | perror("clock_gettime"); | |
484 | exit(EXIT_FAILURE); | |
485 | } | |
486 | ||
bef940ca | 487 | printf("%\-15s: %10ld.%03ld (", name, |
16fa5781 MK |
488 | (long) ts.tv_sec, ts.tv_nsec / 1000000); |
489 | ||
490 | long days = ts.tv_sec / SECS_IN_DAY; | |
491 | if (days > 0) | |
492 | printf("%ld days + ", days); | |
493 | ||
494 | printf("%2ldh %2ldm %2lds", (ts.tv_sec % SECS_IN_DAY) / 3600, | |
495 | (ts.tv_sec % 3600) / 60, ts.tv_sec % 60); | |
496 | printf(")\en"); | |
497 | ||
498 | if (clock_getres(clock, &ts) == \-1) { | |
499 | perror("clock_getres"); | |
500 | exit(EXIT_FAILURE); | |
501 | } | |
502 | ||
503 | if (showRes) | |
bef940ca | 504 | printf(" resolution: %10ld.%09ld\en", |
16fa5781 MK |
505 | (long) ts.tv_sec, ts.tv_nsec); |
506 | } | |
507 | ||
508 | int | |
509 | main(int argc, char *argv[]) | |
510 | { | |
511 | bool showRes = argc > 1; | |
512 | ||
513 | displayClock(CLOCK_REALTIME, "CLOCK_REALTIME", showRes); | |
514 | #ifdef CLOCK_TAI | |
515 | displayClock(CLOCK_TAI, "CLOCK_TAI", showRes); | |
516 | #endif | |
517 | displayClock(CLOCK_MONOTONIC, "CLOCK_MONOTONIC", showRes); | |
518 | #ifdef CLOCK_BOOTTIME | |
519 | displayClock(CLOCK_BOOTTIME, "CLOCK_BOOTTIME", showRes); | |
520 | #endif | |
521 | exit(EXIT_SUCCESS); | |
522 | } | |
523 | .EE | |
47297adb | 524 | .SH SEE ALSO |
fea681da | 525 | .BR date (1), |
fea681da MK |
526 | .BR gettimeofday (2), |
527 | .BR settimeofday (2), | |
528 | .BR time (2), | |
65a0c012 | 529 | .BR adjtime (3), |
edf60acb | 530 | .BR clock_getcpuclockid (3), |
fea681da MK |
531 | .BR ctime (3), |
532 | .BR ftime (3), | |
24ad017d | 533 | .BR pthread_getcpuclockid (3), |
1d7c4d16 | 534 | .BR sysconf (3), |
18688d17 | 535 | .BR time (7), |
68220f1c | 536 | .BR time_namespaces (7), |
1ce611a3 MK |
537 | .BR vdso (7), |
538 | .BR hwclock (8) |