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/posix/time.h>
10 
timespec_to_timeoutms(const struct timespec * abstime)11 int64_t timespec_to_timeoutms(const struct timespec *abstime)
12 {
13 	int64_t milli_secs, secs, nsecs;
14 	struct timespec curtime;
15 
16 	/* FIXME: Zephyr does have CLOCK_REALTIME to get time.
17 	 * As per POSIX standard time should be calculated wrt CLOCK_REALTIME.
18 	 * Zephyr deviates from POSIX 1003.1 standard on this aspect.
19 	 */
20 	clock_gettime(CLOCK_MONOTONIC, &curtime);
21 	secs = abstime->tv_sec - curtime.tv_sec;
22 	nsecs = abstime->tv_nsec - curtime.tv_nsec;
23 
24 	if (secs < 0 || (secs == 0 && nsecs < NSEC_PER_MSEC)) {
25 		milli_secs = 0;
26 	} else {
27 		milli_secs =  secs * MSEC_PER_SEC + nsecs / NSEC_PER_MSEC;
28 	}
29 
30 	return milli_secs;
31 }
32