-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_utils.c
More file actions
83 lines (78 loc) · 1.98 KB
/
time_utils.c
File metadata and controls
83 lines (78 loc) · 1.98 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**************************************************
* FILENAME: time_utils.c
*
* DESCRIPTION:
* Implementations of different utility functions that are useful.
*
* PUBLIC FUNCTIONS:
* float nano_to_sec(unsigned long nanos)
* unsigned long sec_to_nano(float secs)
* unsigned long nano_time(void)
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
#include <sys/time.h>
#include <time.h>
/**************************************************
* NAME: float nano_to_sec(unsigned long nanos)
*
* DESCRIPTION:
* Converts from nanoseconds to seconds.
*
* INPUTS:
* PARAMETERS:
* unsigned long nanos: The time in nanoseconds.
*
* OUTPUTS:
* RETURN:
* float: The time in seconds.
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
float nano_to_sec(unsigned long nanos)
{
float secs = nanos / 1000000000.0;
return secs;
}
/**************************************************
* NAME: unsigned long sec_to_nano(float secs)
*
* DESCRIPTION:
* Converts from seconds to nanoseconds.
*
* INPUTS:
* PARAMETERS:
* float secs: The time in seconds.
*
* OUTPUTS:
* RETURN:
* unsigned long: The time in nanoseconds.
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
unsigned long sec_to_nano(float secs)
{
unsigned long nanos = secs * 1000000000UL;
return nanos;
}
/**************************************************
* NAME: unsigned long nano_time(void)
*
* DESCRIPTION:
* Returns the current time in nanoseconds.
*
* INPUTS:
* none
*
* OUTPUTS:
* RETURN:
* unsigned long: The current time in nanoseconds.
*
* AUTHOR: Jan Henrik Lenes LAST CHANGE: 20.03.2017
**************************************************/
unsigned long nano_time(void)
{
struct timespec timeSpec;
clock_gettime(CLOCK_MONOTONIC, &timeSpec);
return (unsigned long) (sec_to_nano(timeSpec.tv_sec) + timeSpec.tv_nsec);
}