1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <ksched.h>
9 #include <zephyr/wait_q.h>
10 #include <zephyr/posix/time.h>
11 
12 #ifdef CONFIG_POSIX_CLOCK
timespec_to_timeoutms(const struct timespec * abstime)13 int64_t timespec_to_timeoutms(const struct timespec *abstime)
14 {
15 	int64_t milli_secs, secs, nsecs;
16 	struct timespec curtime;
17 
18 	/* FIXME: Zephyr does have CLOCK_REALTIME to get time.
19 	 * As per POSIX standard time should be calculated wrt CLOCK_REALTIME.
20 	 * Zephyr deviates from POSIX 1003.1 standard on this aspect.
21 	 */
22 	clock_gettime(CLOCK_MONOTONIC, &curtime);
23 	secs = abstime->tv_sec - curtime.tv_sec;
24 	nsecs = abstime->tv_nsec - curtime.tv_nsec;
25 
26 	if (secs < 0 || (secs == 0 && nsecs < NSEC_PER_MSEC)) {
27 		milli_secs = 0;
28 	} else {
29 		milli_secs =  secs * MSEC_PER_SEC + nsecs / NSEC_PER_MSEC;
30 	}
31 
32 	return milli_secs;
33 }
34 #endif	/* CONFIG_POSIX_CLOCK */
35