Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
timelib.c
Go to the documentation of this file.
1 
11 /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h>
14 #include <sys/time.h>
15 /* project libraries */
16 #include "timelib.h"
17 
18 /* -- Defines -- */
19 
20 /* -- Functions -- */
21 
22 
28 int timelib_timer_set(struct timeval *tv)
29 {
30  // Set time to now
31  gettimeofday(tv, NULL);
32 
33  // Check error!!!
34 
35  return 0;
36 }
37 
43 double timelib_timer_get(struct timeval tv)
44 {
45  struct timeval tv_now;
46  double time_elapsed;
47 
48  // Get current time
49  gettimeofday(&tv_now, NULL);
50 
51  time_elapsed = (tv_now.tv_sec - tv.tv_sec) * 1000.0; // sec to ms
52  time_elapsed += (tv_now.tv_usec - tv.tv_usec) / 1000.0; // us to ms
53 
54  // Check error!!!
55 
56  return time_elapsed;
57 }
58 
64 double timelib_timer_reset(struct timeval *tv)
65 {
66  struct timeval tv_now;
67  double time_elapsed;
68 
69  // Get current time
70  gettimeofday(&tv_now, NULL);
71 
72  time_elapsed = (tv_now.tv_sec - tv->tv_sec) * 1000.0; // sec to ms
73  time_elapsed += (tv_now.tv_usec - tv->tv_usec) / 1000.0; // us to ms
74 
75  // Set current time to timeval
76  *tv = tv_now;
77 
78  // Check error!!!
79 
80  return time_elapsed;
81 }
82 
83 
90 void timelib_timer_add_ms(struct timeval *tv, unsigned int ms)
91 {
92  // Increase time
93  tv->tv_usec = tv->tv_usec + (ms * 1000);
94  // Normalize tv_usec if required
95  if(tv->tv_usec >= 1000000)
96  {
97  tv->tv_sec++;
98  tv->tv_usec -= 1000000;
99  }
100 }
101 
102 
109 double timelib_timer_diff(struct timeval tv1, struct timeval tv2)
110 {
111  double time_difference;
112 
113  time_difference = (tv2.tv_sec - tv1.tv_sec) * 1000.0; // sec to ms
114  time_difference += (tv2.tv_usec - tv1.tv_usec) / 1000.0; // us to ms
115 
116  return time_difference;
117 }
118 
124 {
125  struct timeval tv_now;
126  double time_now;
127 
128  // Get current time
129  gettimeofday(&tv_now, NULL);
130 
131  time_now = (double)tv_now.tv_sec * 1000; // sec to ms
132  time_now += (double)tv_now.tv_usec / 1000; // us to ms
133 
134  // Check error!!!
135 
136  return time_now;
137 }
138 
double timelib_timer_get(struct timeval tv)
Definition: timelib.c:43
int timelib_timer_set(struct timeval *tv)
Definition: timelib.c:28
double timelib_unix_timestamp()
Definition: timelib.c:123
double timelib_timer_reset(struct timeval *tv)
Definition: timelib.c:64
double timelib_timer_diff(struct timeval tv1, struct timeval tv2)
Definition: timelib.c:109
void timelib_timer_add_ms(struct timeval *tv, unsigned int ms)
Definition: timelib.c:90