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