Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions include/zephyr/sys/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ static inline bool sys_timepoint_expired(k_timepoint_t timepoint)
/** @cond INTERNAL_HIDDEN */
/* forward declaration as workaround for time.h */
struct timespec;

/* Convert a POSIX clock (cast to int) to a sys_clock identifier */
int sys_clock_from_clockid(int clock_id);
/** @endcond INTERNAL_HIDDEN */

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/os/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ static void timespec_from_ticks(uint64_t ticks, struct timespec *ts)
};
}

int sys_clock_from_clockid(int clock_id)
{
switch (clock_id) {
#if defined(CLOCK_REALTIME) || defined(_POSIX_C_SOURCE)
case (int)CLOCK_REALTIME:
return SYS_CLOCK_REALTIME;
#endif
#if defined(CLOCK_MONOTONIC) || defined(_POSIX_MONOTONIC_CLOCK)
case (int)CLOCK_MONOTONIC:
return SYS_CLOCK_MONOTONIC;
#endif
Comment on lines +56 to +63
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey are this right? traced a regression in a downstream application down to this neither of the two options are complied in, is this missing an #include <zephyr/posix/time.h> possibly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right , but I have an inquiry: which libc are you using, and does it actually define CLOCK_REALTIME and CLOCK_MONOTONIC?
From my side, I see the include path in lib/os/clock.c as:

lib/os/clock.c → zephyr/sys/timeutil.h → zephyr/posix/time.h → libc <time.h>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the picolibc one that ships with Zephyr SDK, building on an STM32 not doing really anything special.

Looking at clock.c.i on my build I see zephyr/include/zephyr/sys/timeutil.h but no zephyr/posix/time.h

Copy link
Member

@cfriedt cfriedt Sep 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiobaltieri - can you provide a repro recipe?

We shouldn't necessarily need to include posix/time.h if the libc time.h is included assuming the libc time.h defines either of these two.

It might require a #define _POSIX_C_SOURCE before including time.h though.

Also, please see if #95226 helps.

If there is a corner case where we need to define CLOCK_REALTIME or CLOCK_MONOTONIC, we may be able to do that locally within this file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Chris, yeah looks like #95226 fixes it, let's get on with it. Thanks!

default:
return -EINVAL;
}
}

int sys_clock_gettime(int clock_id, struct timespec *ts)
{
if (!is_valid_clock_id(clock_id)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/posix/options/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts)
{
int ret;

ret = sys_clock_gettime((int)clock_id, ts);
ret = sys_clock_gettime(sys_clock_from_clockid((int)clock_id), ts);
if (ret < 0) {
errno = -ret;
return -1;
Expand Down Expand Up @@ -61,7 +61,7 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp)
{
int ret;

ret = sys_clock_settime((int)clock_id, tp);
ret = sys_clock_settime(sys_clock_from_clockid((int)clock_id), tp);
if (ret < 0) {
errno = -ret;
return -1;
Expand Down
2 changes: 1 addition & 1 deletion lib/posix/options/clock_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
return -1;
}

ret = sys_clock_nanosleep((int)clock_id, flags, rqtp, rmtp);
ret = sys_clock_nanosleep(sys_clock_from_clockid((int)clock_id), flags, rqtp, rmtp);
if (ret < 0) {
errno = -ret;
return -1;
Expand Down
2 changes: 1 addition & 1 deletion lib/posix/options/timespec_to_timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ uint32_t timespec_to_timeoutms(int clock_id, const struct timespec *abstime)
{
struct timespec curtime;

if (sys_clock_gettime(clock_id, &curtime) < 0) {
if (sys_clock_gettime(sys_clock_from_clockid(clock_id), &curtime) < 0) {
return 0;
}

Expand Down