File: timecalc.c

package info (click to toggle)
lsm 1.0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: ansic: 3,883; sh: 318; makefile: 62
file content (56 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (3)
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
52
53
54
55
56
/*

  (C) 2011 Mika Ilmaranta

*/

#include <syslog.h>

#include "defs.h"
#include "timecalc.h"

int timeval_diff_cmp(struct timeval *a, struct timeval *b, int operation, time_t sec, suseconds_t usec) {
	time_t diff_sec;
	suseconds_t diff_usec;

	diff_sec = a->tv_sec - b->tv_sec;
	diff_usec = a->tv_usec - b->tv_usec;

	if(diff_usec < 0) {
		diff_sec--;
		diff_usec = 1000000L + diff_usec;
	}

	switch(operation) {
	case TIMEVAL_DIFF_CMP_GT:
		if(diff_sec > sec) return(TRUE);
		if(diff_sec == sec && diff_usec > usec) return(TRUE);
		return(FALSE);
		break;
	case TIMEVAL_DIFF_CMP_LT:
		if(diff_sec < sec) return(TRUE);
		if(diff_sec == sec && diff_usec < usec) return(TRUE);
		return(FALSE);
		break;
	default:
		syslog(LOG_ERR, "%s: %s: warning: unknown timeval_diff_cmp operation requested %d", __FILE__, __FUNCTION__, operation);
		return(FALSE);
	}
}

long timeval_diff(struct timeval *a, struct timeval *b)
{
	return( ((a->tv_sec - b->tv_sec) * 1000000L) + (a->tv_usec - b->tv_usec) );
}

void timeval_add(struct timeval *a, time_t sec, suseconds_t usec)
{
	a->tv_sec += sec;
	a->tv_usec += usec;
	if(a->tv_usec >= 1000000L) {
		a->tv_sec++;
		a->tv_usec -= 1000000L;
	}
}

/* EOF */