Skip to content

tests: posix: move tv_to_ts to posix_clock.h #89063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions lib/posix/options/posix_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <zephyr/sys_clock.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/posix/sys/time.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -38,6 +39,12 @@ static inline int64_t ts_to_ms(const struct timespec *ts)
return ts->tv_sec * MSEC_PER_SEC + ts->tv_nsec / NSEC_PER_MSEC;
}

static inline void tv_to_ts(const struct timeval *tv, struct timespec *ts)
{
ts->tv_sec = tv->tv_sec;
ts->tv_nsec = tv->tv_usec * NSEC_PER_USEC;
}

static inline bool tp_ge(const struct timespec *a, const struct timespec *b)
{
return ts_to_ns(a) >= ts_to_ns(b);
Expand Down
1 change: 1 addition & 0 deletions tests/posix/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ zephyr_include_directories(${ZEPHYR_BASE}/lib/posix)
target_sources(app PRIVATE ${app_sources})

target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/lib/posix/options)
26 changes: 4 additions & 22 deletions tests/posix/common/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,16 @@
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "posix_clock.h"

#include <limits.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

#include <zephyr/ztest.h>
#include <zephyr/logging/log.h>

static inline int64_t ts_to_ns(const struct timespec *ts)
{
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
}

static inline void tv_to_ts(const struct timeval *tv, struct timespec *ts)
{
ts->tv_sec = tv->tv_sec;
ts->tv_nsec = tv->tv_usec * NSEC_PER_USEC;
}

#define _tp_op(_a, _b, _op) (ts_to_ns(_a) _op ts_to_ns(_b))

#define _decl_op(_type, _name, _op) \
static inline _type _name(const struct timespec *_a, const struct timespec *_b) \
{ \
return _tp_op(_a, _b, _op); \
}

_decl_op(bool, tp_ge, >=); /* a >= b */

ZTEST(clock, test_gettimeofday)
{
struct timeval tv;
Expand Down