1 /*
2  * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  */
8 
9 /*
10  * Here comes common funcions to correctly compute difference between two
11  * struct timespec values.
12  */
13 
14 #define NSEC_IN_SEC 1000000000
15 
16 /*
17  * Returns difference between two struct timespec values. If difference is
18  * greater that 1 sec, 1 sec is returned.
19  */
timespec_nsec_diff(struct timespec * t1,struct timespec * t2)20 static long timespec_nsec_diff(struct timespec *t1, struct timespec *t2)
21 {
22 	time_t sec_diff;
23 	long nsec_diff;
24 
25 	if (t2->tv_sec > t1->tv_sec) {
26 		struct timespec *tmp;
27 		tmp = t1;
28 		t1  = t2;
29 		t2  = tmp;
30 	}
31 
32 	sec_diff  = t1->tv_sec - t2->tv_sec;
33 	nsec_diff = t1->tv_nsec - t2->tv_nsec;
34 
35 	if (sec_diff > 1 || (sec_diff == 1 && nsec_diff >= 0))
36 		return NSEC_IN_SEC;
37 
38 	return abs(nsec_diff + NSEC_IN_SEC * sec_diff);
39 }
40