• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TIME_T_MAX
17 #	define TIME_T_MAX	(time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)
18 #endif
19 
20 /*
21  * Returns difference between two struct timespec values. If difference is
22  * greater that 1 sec, 1 sec is returned.
23  */
timespec_nsec_diff(struct timespec * t1,struct timespec * t2)24 static inline long timespec_nsec_diff(struct timespec *t1, struct timespec *t2)
25 {
26 	time_t sec_diff;
27 	long nsec_diff;
28 
29 	if (t2->tv_sec > t1->tv_sec) {
30 		struct timespec *tmp;
31 		tmp = t1;
32 		t1  = t2;
33 		t2  = tmp;
34 	}
35 
36 	sec_diff  = t1->tv_sec - t2->tv_sec;
37 	nsec_diff = t1->tv_nsec - t2->tv_nsec;
38 
39 	if (sec_diff > 1 || (sec_diff == 1 && nsec_diff >= 0))
40 		return NSEC_IN_SEC;
41 
42 	return labs(nsec_diff + NSEC_IN_SEC * sec_diff);
43 }
44