lib: posix: clock: Implement clock_settime

Set a base time, using the current uptime.

Signed-off-by: David Brown <david.brown@linaro.org>
This commit is contained in:
David Brown 2018-06-25 16:43:54 -06:00 committed by Anas Nashif
parent 9921eb329d
commit 9d6f1c600f
2 changed files with 35 additions and 11 deletions

View file

@ -49,18 +49,8 @@ static inline s32_t _ts_to_ms(const struct timespec *to)
return (to->tv_sec * MSEC_PER_SEC) + (to->tv_nsec / NSEC_PER_MSEC);
}
/**
* @brief Set clock time.
*
* See IEEE 1003.1
*/
static inline int clock_settime(clockid_t clock_id, const struct timespec *ts)
{
errno = ENOSYS;
return -1;
}
int clock_gettime(clockid_t clock_id, struct timespec *ts);
int clock_settime(clockid_t clock_id, const struct timespec *ts);
/* Timer APIs */
int timer_create(clockid_t clockId, struct sigevent *evp, timer_t *timerid);
int timer_delete(timer_t timerid);

View file

@ -57,6 +57,40 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts)
return 0;
}
/**
* @brief Set the time of the specified clock.
*
* See IEEE 1003.1.
*
* Note that only the `CLOCK_REALTIME` clock can be set using this
* call.
*/
int clock_settime(clockid_t clock_id, const struct timespec *tp)
{
struct timespec base;
int res;
if (clock_id != CLOCK_REALTIME) {
errno = EINVAL;
return -1;
}
res = clock_gettime(clock_id, &base);
if (res != 0) {
return res;
}
s64_t delta = (s64_t)NSEC_PER_SEC * (tp->tv_sec - base.tv_sec) +
(tp->tv_nsec - base.tv_nsec);
base.tv_sec = delta / NSEC_PER_SEC;
base.tv_nsec = delta % NSEC_PER_SEC;
rt_clock_base = base;
return 0;
}
/**
* @brief Get current real time.
*