]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/monotonic.c
2 * Please, don't add this file to libcommon because clock_gettime() requires
3 * -lrt on systems with old libc.
5 * No copyright is claimed. This code is in the public domain; do with
11 #include <sys/sysinfo.h>
16 #include "monotonic.h"
18 int get_boot_time(struct timeval
*boot_time
)
21 struct timespec hires_uptime
;
22 struct timeval lores_uptime
;
29 if (gettimeofday(&now
, NULL
) != 0)
32 if (clock_gettime(CLOCK_BOOTTIME
, &hires_uptime
) == 0) {
33 TIMESPEC_TO_TIMEVAL(&lores_uptime
, &hires_uptime
);
34 timersub(&now
, &lores_uptime
, boot_time
);
40 if (sysinfo(&info
) != 0)
43 boot_time
->tv_sec
= now
.tv_sec
- info
.uptime
;
44 boot_time
->tv_usec
= 0;
51 time_t get_suspended_time(void)
53 #if defined(CLOCK_BOOTTIME) && defined(CLOCK_MONOTONIC)
54 struct timespec boot
, mono
;
56 if (clock_gettime(CLOCK_BOOTTIME
, &boot
) == 0 &&
57 clock_gettime(CLOCK_MONOTONIC
, &mono
) == 0)
58 return boot
.tv_sec
- mono
.tv_sec
;
63 int gettime_monotonic(struct timeval
*tv
)
65 #ifdef CLOCK_MONOTONIC
66 /* Can slew only by ntp and adjtime */
70 /* Linux specific, can't slew */
71 if (!(ret
= clock_gettime(UL_CLOCK_MONOTONIC
, &ts
))) {
72 tv
->tv_sec
= ts
.tv_sec
;
73 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
77 return gettimeofday(tv
, NULL
);