22#ifndef ZEPHYR_INCLUDE_SYS_TIMEUTIL_H_
23#define ZEPHYR_INCLUDE_SYS_TIMEUTIL_H_
52#define TIME_UTILS_BASE_YEAR 1900
339 __ASSERT_NO_MSG(ts !=
NULL);
382 __ASSERT_NO_MSG(ts !=
NULL);
384#if defined(CONFIG_SPEED_OPTIMIZATIONS) && HAS_BUILTIN(__builtin_add_overflow)
391 bool overflow = __builtin_add_overflow(ts->
tv_sec, sign * sec, &ts->
tv_sec);
471#if defined(CONFIG_SPEED_OPTIMIZATIONS) && HAS_BUILTIN(__builtin_add_overflow)
513#if defined(CONFIG_SPEED_OPTIMIZATIONS) && HAS_BUILTIN(__builtin_sub_overflow)
515 return !__builtin_sub_overflow(0LL, ts->
tv_sec, &ts->
tv_sec) &&
552 __ASSERT_NO_MSG(a !=
NULL);
553 __ASSERT_NO_MSG(b !=
NULL);
596 __ASSERT_NO_MSG(a !=
NULL);
597 __ASSERT_NO_MSG(b !=
NULL);
622 __ASSERT_NO_MSG(ts !=
NULL);
624#if defined(CONFIG_SPEED_OPTIMIZATIONS)
637 if (timeout.
ticks == 0) {
643 ts->tv_sec = (time_t)INT64_MAX;
644 ts->tv_nsec = NSEC_PER_SEC - 1;
646 uint64_t ns = k_ticks_to_ns_ceil64(timeout.ticks);
648 ts->tv_sec = ns / NSEC_PER_SEC;
649 ts->tv_nsec = ns - ts->tv_sec * NSEC_PER_SEC;
678#if defined(CONFIG_SPEED_OPTIMIZATIONS)
_TIME_T_ time_t
Definition _timespec.h:14
#define NSEC_PER_SEC
number of nanoseconds per second
Definition sys_clock.h:113
#define NSEC_PER_USEC
number of nanoseconds per microsecond
Definition sys_clock.h:83
#define K_TICKS_FOREVER
Definition sys_clock.h:51
#define USEC_PER_SEC
number of microseconds per second
Definition sys_clock.h:110
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:148
#define CLAMP(val, low, high)
Clamp a value to a given range.
Definition util.h:418
#define DIV_ROUND_UP(n, d)
Divide and round up.
Definition util.h:353
time_t timeutil_timegm(const struct tm *tm)
Convert broken-down time to a POSIX epoch offset in seconds.
static void timespec_from_timeout(k_timeout_t timeout, struct timespec *ts)
Convert a kernel timeout to a timespec.
Definition timeutil.h:620
static k_timeout_t timespec_to_timeout(const struct timespec *ts)
Convert a timespec to a kernel timeout.
Definition timeutil.h:674
int64_t timeutil_timegm64(const struct tm *tm)
Convert broken-down time to a POSIX epoch offset in seconds.
int timeutil_sync_state_set_skew(struct timeutil_sync_state *tsp, float skew, const struct timeutil_sync_instant *base)
Update the state with a new skew and possibly base value.
int timeutil_sync_ref_from_local(const struct timeutil_sync_state *tsp, uint64_t local, uint64_t *refp)
Interpolate a reference timescale instant from a local instant.
int timeutil_sync_state_update(struct timeutil_sync_state *tsp, const struct timeutil_sync_instant *inst)
Record a new instant in the time synchronization state.
int32_t timeutil_sync_skew_to_ppb(float skew)
Convert from a skew to an error in parts-per-billion.
float timeutil_sync_estimate_skew(const struct timeutil_sync_state *tsp)
Estimate the skew based on current state.
int timeutil_sync_local_from_ref(const struct timeutil_sync_state *tsp, uint64_t ref, int64_t *localp)
Interpolate a local timescale instant from a reference instant.
static bool timespec_is_valid(const struct timespec *ts)
Check if a timespec is valid.
Definition timeutil.h:337
static bool timespec_negate(struct timespec *ts)
Negate a timespec object.
Definition timeutil.h:509
static bool timespec_normalize(struct timespec *ts)
Normalize a timespec so that the tv_nsec field is in valid range.
Definition timeutil.h:380
static bool timespec_add(struct timespec *a, const struct timespec *b)
Add one timespec to another.
Definition timeutil.h:466
static bool timespec_sub(struct timespec *a, const struct timespec *b)
Subtract one timespec from another.
Definition timeutil.h:550
static bool timespec_equal(const struct timespec *a, const struct timespec *b)
Check if two timespec objects are equal.
Definition timeutil.h:594
static int timespec_compare(const struct timespec *a, const struct timespec *b)
Compare two timespec objects.
Definition timeutil.h:572
#define k_ticks_to_ns_ceil64(t)
Convert ticks to nanoseconds.
Definition time_units.h:1979
#define k_ns_to_ticks_floor64(t)
Convert nanoseconds to ticks.
Definition time_units.h:1051
#define k_sec_to_ticks_floor64(t)
Convert seconds to ticks.
Definition time_units.h:475
#define NULL
Definition iar_missing_defs.h:20
#define LONG_MAX
Definition limits.h:41
#define LONG_MIN
Definition limits.h:46
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
#define INT32_MAX
Definition stdint.h:18
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
#define INT32_MIN
Definition stdint.h:24
#define INT64_MIN
Definition stdint.h:25
#define UINT32_MAX
Definition stdint.h:29
__INT64_TYPE__ int64_t
Definition stdint.h:75
#define INT64_MAX
Definition stdint.h:19
Kernel timeout type.
Definition sys_clock.h:65
k_ticks_t ticks
Definition sys_clock.h:66
Definition _timespec.h:22
long tv_nsec
Definition _timespec.h:24
time_t tv_sec
Definition _timespec.h:23
Immutable state for synchronizing two clocks.
Definition timeutil.h:97
uint32_t ref_Hz
The nominal instance counter rate in Hz.
Definition timeutil.h:105
uint32_t local_Hz
The nominal local counter rate in Hz.
Definition timeutil.h:118
Representation of an instant in two time scales.
Definition timeutil.h:128
uint64_t ref
An instant in the reference time scale.
Definition timeutil.h:134
uint64_t local
The corresponding instance in the local time scale.
Definition timeutil.h:140
State required to convert instants between time scales.
Definition timeutil.h:153
const struct timeutil_sync_config * cfg
Pointer to reference and local rate information.
Definition timeutil.h:155
float skew
The scale factor used to correct for clock skew.
Definition timeutil.h:181
struct timeutil_sync_instant latest
The most recent instant in both time scales.
Definition timeutil.h:164
struct timeutil_sync_instant base
The base instant in both time scales.
Definition timeutil.h:158
Variables needed for system clock.