This commit makes `get_current_time_msec` correctly return milliseconds as opposed to microseconds. It also considers the value of `tv_sec`, so we don't lose occasionally go back in time by one second. Finally, the function is moved into `util/time.cc` so that it can be reused elsewhere without having to consider these pitfalls.master
							parent
							
								
									dcae6f1431
								
							
						
					
					
						commit
						dc13bb827d
					
				| @ -0,0 +1,9 @@ | ||||
| #ifndef UTIL_TIME_H | ||||
| #define UTIL_TIME_H | ||||
| 
 | ||||
| /**
 | ||||
|  * Get the current time, in milliseconds. | ||||
|  */ | ||||
| uint32_t get_current_time_msec(void); | ||||
| 
 | ||||
| #endif | ||||
| @ -0,0 +1,11 @@ | ||||
| #define _POSIX_C_SOURCE 200809L | ||||
| #include <stdint.h> | ||||
| #include <time.h> | ||||
| 
 | ||||
| #include "util/time.h" | ||||
| 
 | ||||
| uint32_t get_current_time_msec(void) { | ||||
| 	struct timespec now; | ||||
| 	clock_gettime(CLOCK_MONOTONIC, &now); | ||||
| 	return now.tv_sec * 1000 + now.tv_nsec / 1000000; | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue