2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/impl/timer/timepoint.h"
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
#define WFP_TIMER_MSEC_PER_SEC ((wfp_timer_timepoint) 1000)
|
|
|
|
#define WFP_TIMER_NSEC_PER_MSEC ((wfp_timer_timepoint) 1000 * 1000)
|
2020-02-29 20:06:40 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_timepoint wfp_timer_timepoint_now(void)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
|
|
|
struct timespec tp;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &tp);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_timepoint const now = (tp.tv_sec * WFP_TIMER_MSEC_PER_SEC) + (tp.tv_nsec / WFP_TIMER_NSEC_PER_MSEC);
|
2020-02-29 20:06:40 +00:00
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_timepoint wfp_timer_timepoint_in_msec(wfp_timer_timediff value)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_timepoint const now = wfp_timer_timepoint_now();
|
|
|
|
wfp_timer_timepoint result = now + ((wfp_timer_timepoint) value);
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
bool wfp_timer_timepoint_is_elapsed(wfp_timer_timepoint tp)
|
2020-02-29 20:06:40 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_timepoint const now = wfp_timer_timepoint_now();
|
|
|
|
wfp_timer_timediff const diff = (wfp_timer_timediff) (tp - now);
|
2020-02-29 20:06:40 +00:00
|
|
|
|
|
|
|
return (0 > diff);
|
|
|
|
}
|