Implement time sensor
[khatus.git] / x5 / khatus_lib_time.c
1 #include <errno.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <time.h>
6
7 #include "khatus_lib_log.h"
8 #include "khatus_lib_time.h"
9
10 struct timespec
11 timespec_of_float(double n)
12 {
13 double integral;
14 double fractional;
15 struct timespec t;
16
17 fractional = modf(n, &integral);
18 t.tv_sec = (int) integral;
19 t.tv_nsec = (int) (1E9 * fractional);
20
21 return t;
22 }
23
24 void
25 snooze(struct timespec *t)
26 {
27 struct timespec remainder;
28 int result;
29
30 result = nanosleep(t, &remainder);
31
32 if (result < 0) {
33 if (errno == EINTR) {
34 warn(
35 "nanosleep interrupted. Remainder: "
36 "{ tv_sec = %ld, tv_nsec = %ld }",
37 remainder.tv_sec, remainder.tv_nsec);
38 /* No big deal if we occasionally sleep less,
39 * so not attempting to correct after an interruption.
40 */
41 } else {
42 fatal("nanosleep: %s\n", strerror(errno));
43 }
44 }
45 }
This page took 0.071489 seconds and 4 git commands to generate.