22#ifndef ZEPHYR_INCLUDE_SYS_TIMEUTIL_H_
23#define ZEPHYR_INCLUDE_SYS_TIMEUTIL_H_
44#define SYS_TIME_T_MAX ((((time_t)1 << (8 * sizeof(time_t) - 2)) - 1) * 2 + 1)
45#define SYS_TIME_T_MIN (-SYS_TIME_T_MAX - 1)
48#define SYS_TICKS_TO_SECS(ticks) \
49 (((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) ? SYS_TIME_T_MAX \
50 : k_ticks_to_sec_floor64(ticks))
53#define SYS_TICKS_TO_NSECS(ticks) \
54 (((uint64_t)(ticks) >= (uint64_t)K_TICKS_FOREVER) \
55 ? (NSEC_PER_SEC - 1) \
56 : k_ticks_to_ns_floor32((uint64_t)(ticks) % CONFIG_SYS_CLOCK_TICKS_PER_SEC))
59#define SYS_TIMESPEC(sec, nsec) \
61 .tv_sec = (time_t)CLAMP((int64_t)(sec), SYS_TIME_T_MIN, SYS_TIME_T_MAX), \
62 .tv_nsec = (long)(nsec), \
66#define SYS_TICKS_TO_TIMESPEC(ticks) SYS_TIMESPEC(SYS_TICKS_TO_SECS(ticks), \
67 SYS_TICKS_TO_NSECS(ticks))
70#define SYS_TIMESPEC_NO_WAIT SYS_TICKS_TO_TIMESPEC(0)
73#define SYS_TIMESPEC_MIN SYS_TICKS_TO_TIMESPEC(K_TICK_MIN)
76#define SYS_TIMESPEC_MAX SYS_TICKS_TO_TIMESPEC(K_TICK_MAX)
79#define SYS_TIMESPEC_FOREVER SYS_TIMESPEC(SYS_TIME_T_MAX, NSEC_PER_SEC - 1)
90#define TIME_UTILS_BASE_YEAR 1900
377 __ASSERT_NO_MSG(ts !=
NULL);
420 __ASSERT_NO_MSG(ts !=
NULL);
422#if defined(CONFIG_SPEED_OPTIMIZATIONS) && HAS_BUILTIN(__builtin_add_overflow)
442 bool overflow = __builtin_add_overflow(ts->
tv_sec, sign * sec, &ts->
tv_sec);
515#if defined(CONFIG_SPEED_OPTIMIZATIONS) && HAS_BUILTIN(__builtin_add_overflow)
557#if defined(CONFIG_SPEED_OPTIMIZATIONS) && HAS_BUILTIN(__builtin_sub_overflow)
559 return !__builtin_sub_overflow(0LL, ts->
tv_sec, &ts->
tv_sec) &&
594 __ASSERT_NO_MSG(a !=
NULL);
595 __ASSERT_NO_MSG(b !=
NULL);
638 __ASSERT_NO_MSG(a !=
NULL);
639 __ASSERT_NO_MSG(b !=
NULL);
667 __ASSERT_NO_MSG(ts !=
NULL);
668 __ASSERT_NO_MSG(Z_IS_TIMEOUT_RELATIVE(timeout) ||
742 timeout.
ticks = K_TICK_MAX;
747 K_TICK_MIN, K_TICK_MAX);
_TIME_T_ time_t
Definition _timespec.h:14
#define NSEC_PER_SEC
number of nanoseconds per second
Definition clock.h:113
#define K_TICKS_FOREVER
Definition clock.h:51
#define K_TIMEOUT_EQ(a, b)
Compare timeouts for equality.
Definition clock.h:80
#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 k_timeout_t timespec_to_timeout(const struct timespec *req, struct timespec *rem)
Convert a timespec to a kernel timeout.
Definition timeutil.h:716
static void timespec_from_timeout(k_timeout_t timeout, struct timespec *ts)
Convert a kernel timeout to a timespec.
Definition timeutil.h:665
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:375
static bool timespec_negate(struct timespec *ts)
Negate a timespec object.
Definition timeutil.h:553
static bool timespec_normalize(struct timespec *ts)
Normalize a timespec so that the tv_nsec field is in valid range.
Definition timeutil.h:418
static bool timespec_add(struct timespec *a, const struct timespec *b)
Add one timespec to another.
Definition timeutil.h:510
static bool timespec_sub(struct timespec *a, const struct timespec *b)
Subtract one timespec from another.
Definition timeutil.h:592
static bool timespec_equal(const struct timespec *a, const struct timespec *b)
Check if two timespec objects are equal.
Definition timeutil.h:636
static int timespec_compare(const struct timespec *a, const struct timespec *b)
Compare two timespec objects.
Definition timeutil.h:614
#define k_ns_to_ticks_ceil64(t)
Convert nanoseconds to ticks.
Definition time_units.h:1115
#define k_sec_to_ticks_ceil64(t)
Convert seconds to ticks.
Definition time_units.h:539
#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
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
#define INT64_MIN
Definition stdint.h:25
__INT64_TYPE__ int64_t
Definition stdint.h:75
#define INT64_MAX
Definition stdint.h:19
Kernel timeout type.
Definition clock.h:65
k_ticks_t ticks
Definition 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:135
uint32_t ref_Hz
The nominal instance counter rate in Hz.
Definition timeutil.h:143
uint32_t local_Hz
The nominal local counter rate in Hz.
Definition timeutil.h:156
Representation of an instant in two time scales.
Definition timeutil.h:166
uint64_t ref
An instant in the reference time scale.
Definition timeutil.h:172
uint64_t local
The corresponding instance in the local time scale.
Definition timeutil.h:178
State required to convert instants between time scales.
Definition timeutil.h:191
const struct timeutil_sync_config * cfg
Pointer to reference and local rate information.
Definition timeutil.h:193
float skew
The scale factor used to correct for clock skew.
Definition timeutil.h:219
struct timeutil_sync_instant latest
The most recent instant in both time scales.
Definition timeutil.h:202
struct timeutil_sync_instant base
The base instant in both time scales.
Definition timeutil.h:196
#define SYS_TIME_T_MAX
Definition timeutil.h:44
#define SYS_TIMESPEC_FOREVER
Definition timeutil.h:79
#define SYS_TIMESPEC_MAX
Definition timeutil.h:76
#define SYS_TIME_T_MIN
Definition timeutil.h:45
#define SYS_TICKS_TO_TIMESPEC(ticks)
Definition timeutil.h:66
#define SYS_TIMESPEC_NO_WAIT
Definition timeutil.h:70