forked from ros2/freertps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
53 lines (49 loc) · 1.29 KB
/
time.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "freertps/time.h"
#include "freertps/freertps.h"
#include <limits.h>
fr_duration_t fr_time_diff(const fr_time_t *end, const fr_time_t *start)
{
// FUTURE: this can probably be simplified.
fr_duration_t d;
if (end->seconds >= start->seconds)
{
d.fraction = end->fraction - start->fraction;
if (end->fraction >= start->fraction)
d.seconds = end->seconds - start->seconds;
else
d.seconds = end->seconds - start->seconds - 1;
}
else
{
d.fraction = start->fraction - end->fraction;
if (end->fraction > start->fraction)
d.seconds = end->seconds - start->seconds + 1;
else
d.seconds = end->seconds - start->seconds;
}
return d;
}
double fr_time_double(const fr_time_t *t)
{
if (t->seconds >= 0)
return t->seconds + t->fraction / (double)UINT_MAX;
else
{
FREERTPS_ERROR("invalid fr_time: (%d, %u)\n",
(int)t->seconds, (unsigned)t->fraction);
return 0;
}
return t->seconds - t->fraction / (double)UINT_MAX;
}
double fr_duration_double(const fr_duration_t *t)
{
if (t->seconds >= 0)
return t->seconds + t->fraction / (double)UINT_MAX;
else
return t->seconds - t->fraction / (double)UINT_MAX;
}
double fr_time_now_double(void)
{
fr_time_t t = fr_time_now();
return fr_duration_double(&t);
}