LibJuno 1.0.1
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
time_api.h
Go to the documentation of this file.
1/*
2 MIT License
3
4 Copyright (c) 2025 Robin A. Onsay
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16*/
55#ifndef JUNO_TIME_API_H
56#define JUNO_TIME_API_H
57#include "juno/macros.h"
58#include "juno/status.h"
59#include "juno/module.h"
60#include "juno/types.h"
61#include <stddef.h>
62#include <stdint.h>
63#ifdef __cplusplus
64extern "C"
65{
66#endif
67
69typedef struct JUNO_TIME_ROOT_TAG JUNO_TIME_ROOT_T;
71
73typedef uint32_t JUNO_TIME_SECONDS_T;
75typedef uint64_t JUNO_TIME_MILLIS_T;
77typedef uint64_t JUNO_TIME_MICROS_T;
79typedef uint64_t JUNO_TIME_NANOS_T;
81typedef uint32_t JUNO_TIME_SUBSECONDS_T;
82
100
112
114JUNO_MODULE_RESULT(JUNO_TIMESTAMP_RESULT_T, JUNO_TIMESTAMP_T);
116JUNO_MODULE_RESULT(JUNO_TIME_SECONDS_RESULT_T, JUNO_TIME_SECONDS_T);
118JUNO_MODULE_RESULT(JUNO_TIME_MILLIS_RESULT_T, JUNO_TIME_MILLIS_T);
120JUNO_MODULE_RESULT(JUNO_TIME_MICROS_RESULT_T, JUNO_TIME_MICROS_T);
122JUNO_MODULE_RESULT(JUNO_TIME_NANOS_RESULT_T, JUNO_TIME_NANOS_T);
124JUNO_MODULE_RESULT(JUNO_TIME_SUBSECONDS_RESULT_T, JUNO_TIME_SUBSECONDS_T);
125
128{
130 JUNO_TIMESTAMP_RESULT_T (*Now)(const JUNO_TIME_ROOT_T *ptTime);
132 JUNO_STATUS_T (*AddTime)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToAdd);
134 JUNO_STATUS_T (*SubtractTime)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToSubtract);
136 JUNO_STATUS_T (*SleepTo)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimeToWakeup);
140 JUNO_TIME_NANOS_RESULT_T (*TimestampToNanos)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
142 JUNO_TIME_MICROS_RESULT_T (*TimestampToMicros)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
144 JUNO_TIME_MILLIS_RESULT_T (*TimestampToMillis)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
146 JUNO_TIMESTAMP_RESULT_T (*NanosToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_NANOS_T iNanos);
148 JUNO_TIMESTAMP_RESULT_T (*MicrosToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MICROS_T iMicros);
150 JUNO_TIMESTAMP_RESULT_T (*MillisToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MILLIS_T iMillis);
152 JUNO_RESULT_F64_T (*TimestampToDouble)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimestamp);
154 JUNO_TIMESTAMP_RESULT_T (*DoubleToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp);
155};
156
157
166
174
176JUNO_TIME_NANOS_RESULT_T JunoTime_TimestampToNanos(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
177JUNO_TIME_MICROS_RESULT_T JunoTime_TimestampToMicros(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
178JUNO_TIME_MILLIS_RESULT_T JunoTime_TimestampToMillis(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
179
181JUNO_TIMESTAMP_RESULT_T JunoTime_NanosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_NANOS_T iNanos);
182JUNO_TIMESTAMP_RESULT_T JunoTime_MicrosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MICROS_T iMicros);
183JUNO_TIMESTAMP_RESULT_T JunoTime_MillisToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MILLIS_T iMillis);
184
186JUNO_RESULT_F64_T JunoTime_TimestampToDouble(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimestamp);
187JUNO_TIMESTAMP_RESULT_T JunoTime_DoubleToTimestamp(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp);
188
197#define JunoTime_TimeApiInit(Now, SleepTo, Sleep) \
198{ \
199 Now, \
200 JunoTime_AddTime, \
201 JunoTime_SubtractTime, \
202 SleepTo, \
203 Sleep, \
204 JunoTime_TimestampToNanos, \
205 JunoTime_TimestampToMicros, \
206 JunoTime_TimestampToMillis, \
207 JunoTime_NanosToTimestamp, \
208 JunoTime_MicrosToTimestamp, \
209 JunoTime_MillisToTimestamp, \
210 JunoTime_TimestampToDouble, \
211 JunoTime_DoubleToTimestamp, \
212}
213
216{
217 return (tLeft.iSeconds > tRight.iSeconds) || (tLeft.iSeconds == tRight.iSeconds && tLeft.iSubSeconds > tRight.iSubSeconds);
218}
219
222{
223 return (tLeft.iSeconds < tRight.iSeconds) || (tLeft.iSeconds == tRight.iSeconds && tLeft.iSubSeconds < tRight.iSubSeconds);
224}
225
228{
229 return (tLeft.iSeconds == tRight.iSeconds) && (tLeft.iSubSeconds == tRight.iSubSeconds);
230}
231
233static inline JUNO_STATUS_T JunoTime_TimeInit(JUNO_TIME_ROOT_T *ptTime, const JUNO_TIME_API_T *ptApi, JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvUserData)
234{
235 JUNO_ASSERT_EXISTS(ptTime);
236 ptTime->ptApi = ptApi;
237 ptTime->_pfcnFailureHandler = pfcnFailureHandler;
238 ptTime->_pvFailureUserData = pvUserData;
239 return JUNO_STATUS_SUCCESS;
240}
241
242#ifdef __cplusplus
243}
244#endif
245#endif // JUNO_TIME_API_H
#define JUNO_ASSERT_EXISTS(ptr)
Returns JUNO_STATUS_NULLPTR_ERROR if the expression is falsy.
Definition macros.h:50
#define JUNO_MODULE_EMPTY
Helper for module definitions with no additional members.
Definition module.h:86
#define JUNO_MODULE_RESULT(NAME_T, OK_T)
Define a result type combining a status and a success payload.
Definition module.h:193
#define JUNO_MODULE_ROOT(API_T,...)
Implement a module root struct containing ptApi and failure fields.
Definition module.h:126
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:56
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Failure handler callback signature.
Definition status.h:104
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:49
void JUNO_USER_DATA_T
Opaque user data type for failure callbacks.
Definition status.h:96
uint64_t JUNO_TIME_NANOS_T
Nanoseconds count type.
Definition time_api.h:79
static bool JunoTime_TimestampEquals(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Compare timestamps for equality.
Definition time_api.h:227
uint64_t JUNO_TIME_MICROS_T
Microseconds count type.
Definition time_api.h:77
JUNO_TIME_MICROS_RESULT_T JunoTime_TimestampToMicros(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:112
JUNO_TIMESTAMP_RESULT_T JunoTime_MicrosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MICROS_T iMicros)
Definition juno_time.c:196
JUNO_TIMESTAMP_RESULT_T JunoTime_MillisToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MILLIS_T iMillis)
Definition juno_time.c:213
JUNO_TIME_NANOS_RESULT_T JunoTime_TimestampToNanos(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:76
static bool JunoTime_TimestampGreaterThan(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Compare timestamps (tLeft > tRight).
Definition time_api.h:215
uint64_t JUNO_TIME_MILLIS_T
Milliseconds count type.
Definition time_api.h:75
uint32_t JUNO_TIME_SECONDS_T
Whole seconds component type.
Definition time_api.h:73
JUNO_STATUS_T JunoTime_SubtractTime(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToSubtract)
Subtract a duration from a timestamp in-place. If the subtraction would underflow (negative time),...
Definition juno_time.c:48
static bool JunoTime_TimestampLessThan(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Compare timestamps (tLeft < tRight).
Definition time_api.h:221
static JUNO_STATUS_T JunoTime_TimeInit(JUNO_TIME_ROOT_T *ptTime, const JUNO_TIME_API_T *ptApi, JUNO_FAILURE_HANDLER_T pfcnFailureHandler, JUNO_USER_DATA_T *pvUserData)
Initialize the time module root with API and failure handler.
Definition time_api.h:233
JUNO_TIMESTAMP_RESULT_T JunoTime_DoubleToTimestamp(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp)
Definition juno_time.c:245
JUNO_RESULT_F64_T JunoTime_TimestampToDouble(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimestamp)
Definition juno_time.c:231
JUNO_STATUS_T JunoTime_AddTime(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToAdd)
Add a duration to a timestamp in-place.
Definition juno_time.c:26
JUNO_TIME_MILLIS_RESULT_T JunoTime_TimestampToMillis(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:145
JUNO_TIMESTAMP_RESULT_T JunoTime_NanosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_NANOS_T iNanos)
Definition juno_time.c:179
uint32_t JUNO_TIME_SUBSECONDS_T
Subsecond fixed-point fraction type.
Definition time_api.h:81
struct JUNO_TIME_ROOT_TAG JUNO_TIME_ROOT_T
Definition time_api.h:69
Common assertion and helper macros for LibJuno modules.
Module system and dependency injection primitives for LibJuno.
Status codes and failure-handling helpers for LibJuno.
Timestamp consisting of whole seconds and fixed-point subseconds.
Definition time_api.h:105
JUNO_TIME_SECONDS_T iSeconds
Whole seconds component of time.
Definition time_api.h:107
JUNO_TIME_SUBSECONDS_T iSubSeconds
Definition time_api.h:110
Vtable for time operations and conversions.
Definition time_api.h:128
JUNO_TIMESTAMP_RESULT_T(* DoubleToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp)
Convert a double to a timestamp.
Definition time_api.h:154
JUNO_TIMESTAMP_RESULT_T(* Now)(const JUNO_TIME_ROOT_T *ptTime)
Get the current time as specified by the implementation.
Definition time_api.h:130
JUNO_STATUS_T(* SleepTo)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimeToWakeup)
Sleep this thread until a specific time.
Definition time_api.h:136
JUNO_TIMESTAMP_RESULT_T(* MicrosToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MICROS_T iMicros)
Convert microseconds to a timestamp.
Definition time_api.h:148
JUNO_STATUS_T(* Sleep)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tDuration)
Sleep this thread for a duration.
Definition time_api.h:138
JUNO_TIME_NANOS_RESULT_T(* TimestampToNanos)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Convert a timestamp to nanoseconds (round to nearest, half-up)
Definition time_api.h:140
JUNO_TIME_MILLIS_RESULT_T(* TimestampToMillis)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Convert a timestamp to milliseconds (round to nearest, half-up)
Definition time_api.h:144
JUNO_STATUS_T(* AddTime)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToAdd)
Perform addition with time (in-place add to ptRetTime)
Definition time_api.h:132
JUNO_TIMESTAMP_RESULT_T(* NanosToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_NANOS_T iNanos)
Convert nanoseconds to a timestamp.
Definition time_api.h:146
JUNO_TIMESTAMP_RESULT_T(* MillisToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MILLIS_T iMillis)
Convert milliseconds to a timestamp.
Definition time_api.h:150
JUNO_TIME_MICROS_RESULT_T(* TimestampToMicros)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Convert a timestamp to microseconds (round to nearest, half-up)
Definition time_api.h:142
JUNO_STATUS_T(* SubtractTime)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToSubtract)
Perform subtraction with time (in-place subtract from ptRetTime)
Definition time_api.h:134
JUNO_RESULT_F64_T(* TimestampToDouble)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimestamp)
Convert a timestamp to a double.
Definition time_api.h:152
Common module result type aliases used throughout LibJuno.