zephyr/lib/posix/clock.c
Ramakrishna Pallala 03a3c992b8 lib: posix: clock: Use k_uptime_get() to compute tv_nsec
Use k_uptime_get() to compute both tv_sec and tv_nsec members
of timespec structure.

Fixes #8009

Signed-off-by: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
2018-06-02 16:00:23 -04:00

32 lines
610 B
C

/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <errno.h>
#include <posix/time.h>
#include <posix/sys/types.h>
/**
* @brief Get clock time specified by clock_id.
*
* See IEEE 1003.1
*/
int clock_gettime(clockid_t clock_id, struct timespec *ts)
{
u64_t elapsed_msecs;
if (clock_id != CLOCK_MONOTONIC) {
errno = EINVAL;
return -1;
}
elapsed_msecs = k_uptime_get();
ts->tv_sec = (s32_t) (elapsed_msecs / MSEC_PER_SEC);
ts->tv_nsec = (s32_t) ((elapsed_msecs % MSEC_PER_SEC) *
USEC_PER_MSEC * NSEC_PER_USEC);
return 0;
}