Skip to content

Commit 0388a90

Browse files
Chris Friedtcfriedt
authored andcommitted
posix: clock: fix seconds calculation
The previous method used to calculate seconds in `clock_gettime()` seemed to have an inaccuracy that grew with time causing the seconds to be off by an order of magnitude when ticks would roll over. This change fixes the method used to calculate seconds. Signed-off-by: Chris Friedt <[email protected]>
1 parent 4c62d76 commit 0388a90

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/posix/clock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ int z_impl_clock_gettime(clockid_t clock_id, struct timespec *ts)
4848
}
4949

5050
uint64_t ticks = k_uptime_ticks();
51-
uint64_t elapsed_secs = k_ticks_to_ms_floor64(ticks) / MSEC_PER_SEC;
52-
uint64_t nremainder = ticks - k_ms_to_ticks_floor64(MSEC_PER_SEC * elapsed_secs);
51+
uint64_t elapsed_secs = ticks / CONFIG_SYS_CLOCK_TICKS_PER_SEC;
52+
uint64_t nremainder = ticks - elapsed_secs * CONFIG_SYS_CLOCK_TICKS_PER_SEC;
5353

5454
ts->tv_sec = (time_t) elapsed_secs;
5555
/* For ns 32 bit conversion can be used since its smaller than 1sec. */

0 commit comments

Comments
 (0)