File: timerlib_common.c

package info (click to toggle)
php-excimer 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: ansic: 2,513; xml: 233; php: 82; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,264 bytes parent folder | download
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
#include "timerlib.h"
#include <stdio.h>

void timerlib_timespec_add(timerlib_timespec_t * a, const timerlib_timespec_t * b)
{
	a->tv_sec += b->tv_sec;
	a->tv_nsec += b->tv_nsec;
	if (a->tv_nsec > TIMERLIB_BILLION_L) {
		a->tv_nsec -= TIMERLIB_BILLION_L;
		a->tv_sec++;
	}
}

void timerlib_timespec_subtract(timerlib_timespec_t * a, const timerlib_timespec_t * b)
{
	a->tv_sec -= b->tv_sec;
	if (a->tv_nsec < b->tv_nsec) {
		a->tv_sec--;
		a->tv_nsec += TIMERLIB_BILLION_L - b->tv_nsec;
	} else {
		a->tv_nsec -= b->tv_nsec;
	}
}

void timerlib_timespec_from_double(timerlib_timespec_t * dest, double source)
{
	double fractional, integral;
	if (source < 0) {
		dest->tv_sec = dest->tv_nsec = 0;
		return;
	}

	fractional = modf(source, &integral);
	dest->tv_sec = (time_t)integral;
	dest->tv_nsec = (long)(fractional * 1e9);
	if (dest->tv_nsec >= TIMERLIB_BILLION_L) {
		dest->tv_nsec -= TIMERLIB_BILLION_L;
		dest->tv_sec ++;
	}
}

int timerlib_timer_start_oneshot(timerlib_timer_t *timer, timerlib_timespec_t *duration)
{
	timerlib_timespec_t period = {0};
	return timerlib_timer_start(timer, &period, duration);
}

int timerlib_timer_start_periodic(timerlib_timer_t *timer, timerlib_timespec_t *period)
{
	return timerlib_timer_start(timer, period, period);
}