Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static struct timespec rt_clock_base;
*/
int z_impl_clock_gettime(clockid_t clock_id, struct timespec *ts)
{
uint64_t elapsed_msecs;
uint64_t elapsed_nsecs;
struct timespec base;

switch (clock_id) {
Expand All @@ -43,10 +43,9 @@ int z_impl_clock_gettime(clockid_t clock_id, struct timespec *ts)
return -1;
}

elapsed_msecs = k_uptime_get();
ts->tv_sec = (int32_t) (elapsed_msecs / MSEC_PER_SEC);
ts->tv_nsec = (int32_t) ((elapsed_msecs % MSEC_PER_SEC) *
USEC_PER_MSEC * NSEC_PER_USEC);
elapsed_nsecs = k_ticks_to_ns_floor64(k_uptime_ticks());
ts->tv_sec = (int32_t) (elapsed_nsecs / NSEC_PER_SEC);
ts->tv_nsec = (int32_t) (elapsed_nsecs % NSEC_PER_SEC);

ts->tv_sec += base.tv_sec;
ts->tv_nsec += base.tv_nsec;
Expand Down Expand Up @@ -84,9 +83,9 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp)
return -1;
}

uint64_t elapsed_msecs = k_uptime_get();
uint64_t elapsed_nsecs = k_ticks_to_ns_floor64(k_uptime_ticks());
int64_t delta = (int64_t)NSEC_PER_SEC * tp->tv_sec + tp->tv_nsec
- elapsed_msecs * USEC_PER_MSEC * NSEC_PER_USEC;
- elapsed_nsecs;

base.tv_sec = delta / NSEC_PER_SEC;
base.tv_nsec = delta % NSEC_PER_SEC;
Expand Down