Line data Source code
1 : #include "timerlib.h" 2 : #include <stdio.h> 3 : 4 0 : void timerlib_timespec_add(timerlib_timespec_t * a, const timerlib_timespec_t * b) 5 : { 6 0 : a->tv_sec += b->tv_sec; 7 0 : a->tv_nsec += b->tv_nsec; 8 0 : if (a->tv_nsec > TIMERLIB_BILLION_L) { 9 0 : a->tv_nsec -= TIMERLIB_BILLION_L; 10 0 : a->tv_sec++; 11 : } 12 0 : } 13 : 14 0 : void timerlib_timespec_subtract(timerlib_timespec_t * a, const timerlib_timespec_t * b) 15 : { 16 0 : a->tv_sec -= b->tv_sec; 17 0 : if (a->tv_nsec < b->tv_nsec) { 18 0 : a->tv_sec--; 19 0 : a->tv_nsec += TIMERLIB_BILLION_L - b->tv_nsec; 20 : } else { 21 0 : a->tv_nsec -= b->tv_nsec; 22 : } 23 0 : } 24 : 25 54 : void timerlib_timespec_from_double(timerlib_timespec_t * dest, double source) 26 : { 27 : double fractional, integral; 28 54 : if (source < 0) { 29 0 : dest->tv_sec = dest->tv_nsec = 0; 30 0 : return; 31 : } 32 : 33 54 : fractional = modf(source, &integral); 34 54 : dest->tv_sec = (time_t)integral; 35 54 : dest->tv_nsec = (long)(fractional * 1e9); 36 54 : if (dest->tv_nsec >= TIMERLIB_BILLION_L) { 37 0 : dest->tv_nsec -= TIMERLIB_BILLION_L; 38 0 : dest->tv_sec ++; 39 : } 40 : } 41 : 42 0 : int timerlib_timer_start_oneshot(timerlib_timer_t *timer, timerlib_timespec_t *duration) 43 : { 44 0 : timerlib_timespec_t period = {0}; 45 0 : return timerlib_timer_start(timer, &period, duration); 46 : } 47 : 48 0 : int timerlib_timer_start_periodic(timerlib_timer_t *timer, timerlib_timespec_t *period) 49 : { 50 0 : return timerlib_timer_start(timer, period, period); 51 : }