LibJuno 1.0.4
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
104// @{"req": ["REQ-TIME-001"]}
113
115JUNO_MODULE_RESULT(JUNO_TIMESTAMP_RESULT_T, JUNO_TIMESTAMP_T);
117JUNO_MODULE_RESULT(JUNO_TIME_SECONDS_RESULT_T, JUNO_TIME_SECONDS_T);
119JUNO_MODULE_RESULT(JUNO_TIME_MILLIS_RESULT_T, JUNO_TIME_MILLIS_T);
121JUNO_MODULE_RESULT(JUNO_TIME_MICROS_RESULT_T, JUNO_TIME_MICROS_T);
123JUNO_MODULE_RESULT(JUNO_TIME_NANOS_RESULT_T, JUNO_TIME_NANOS_T);
125JUNO_MODULE_RESULT(JUNO_TIME_SUBSECONDS_RESULT_T, JUNO_TIME_SUBSECONDS_T);
126
128// @{"req": ["REQ-TIME-002"]}
130{
132 JUNO_TIMESTAMP_RESULT_T (*Now)(const JUNO_TIME_ROOT_T *ptTime);
134 JUNO_STATUS_T (*AddTime)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToAdd);
136 JUNO_STATUS_T (*SubtractTime)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToSubtract);
138 JUNO_STATUS_T (*SleepTo)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimeToWakeup);
142 JUNO_TIME_NANOS_RESULT_T (*TimestampToNanos)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
144 JUNO_TIME_MICROS_RESULT_T (*TimestampToMicros)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
146 JUNO_TIME_MILLIS_RESULT_T (*TimestampToMillis)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
148 JUNO_TIMESTAMP_RESULT_T (*NanosToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_NANOS_T iNanos);
150 JUNO_TIMESTAMP_RESULT_T (*MicrosToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MICROS_T iMicros);
152 JUNO_TIMESTAMP_RESULT_T (*MillisToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MILLIS_T iMillis);
154 JUNO_RESULT_F64_T (*TimestampToDouble)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimestamp);
156 JUNO_TIMESTAMP_RESULT_T (*DoubleToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp);
157};
158
159
168
176
178JUNO_TIME_NANOS_RESULT_T JunoTime_TimestampToNanos(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
179JUNO_TIME_MICROS_RESULT_T JunoTime_TimestampToMicros(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
180JUNO_TIME_MILLIS_RESULT_T JunoTime_TimestampToMillis(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime);
181
183JUNO_TIMESTAMP_RESULT_T JunoTime_NanosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_NANOS_T iNanos);
184JUNO_TIMESTAMP_RESULT_T JunoTime_MicrosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MICROS_T iMicros);
185JUNO_TIMESTAMP_RESULT_T JunoTime_MillisToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MILLIS_T iMillis);
186
188JUNO_RESULT_F64_T JunoTime_TimestampToDouble(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimestamp);
189JUNO_TIMESTAMP_RESULT_T JunoTime_DoubleToTimestamp(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp);
190
199#define JunoTime_TimeApiInit(Now, SleepTo, Sleep) \
200{ \
201 Now, \
202 JunoTime_AddTime, \
203 JunoTime_SubtractTime, \
204 SleepTo, \
205 Sleep, \
206 JunoTime_TimestampToNanos, \
207 JunoTime_TimestampToMicros, \
208 JunoTime_TimestampToMillis, \
209 JunoTime_NanosToTimestamp, \
210 JunoTime_MicrosToTimestamp, \
211 JunoTime_MillisToTimestamp, \
212 JunoTime_TimestampToDouble, \
213 JunoTime_DoubleToTimestamp, \
214}
215
218{
219 return (tLeft.iSeconds > tRight.iSeconds) || (tLeft.iSeconds == tRight.iSeconds && tLeft.iSubSeconds > tRight.iSubSeconds);
220}
221
224{
225 return (tLeft.iSeconds < tRight.iSeconds) || (tLeft.iSeconds == tRight.iSeconds && tLeft.iSubSeconds < tRight.iSubSeconds);
226}
227
230{
231 return (tLeft.iSeconds == tRight.iSeconds) && (tLeft.iSubSeconds == tRight.iSubSeconds);
232}
233
235static 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)
236{
237 JUNO_ASSERT_EXISTS(ptTime);
238 ptTime->ptApi = ptApi;
239 ptTime->_pfcnFailureHandler = pfcnFailureHandler;
240 ptTime->_pvFailureUserData = pvUserData;
241 return JUNO_STATUS_SUCCESS;
242}
243
244#ifdef __cplusplus
245}
246#endif
247#endif // JUNO_TIME_API_H
#define JUNO_ASSERT_EXISTS(ptr)
Returns JUNO_STATUS_NULLPTR_ERROR if the expression is falsy.
Definition macros.h:51
#define JUNO_MODULE_EMPTY
Helper for module definitions with no additional members.
Definition module.h:87
#define JUNO_MODULE_RESULT(NAME_T, OK_T)
Define a result type combining a status and a success payload.
Definition module.h:198
#define JUNO_MODULE_ROOT(API_T,...)
Implement a module root struct containing ptApi and failure fields.
Definition module.h:129
#define JUNO_STATUS_SUCCESS
Operation completed successfully.
Definition status.h:59
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Failure handler callback signature.
Definition status.h:110
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:51
void JUNO_USER_DATA_T
Opaque user data type for failure callbacks.
Definition status.h:101
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:229
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:116
JUNO_TIMESTAMP_RESULT_T JunoTime_MicrosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MICROS_T iMicros)
Definition juno_time.c:203
JUNO_TIMESTAMP_RESULT_T JunoTime_MillisToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_MILLIS_T iMillis)
Definition juno_time.c:221
JUNO_TIME_NANOS_RESULT_T JunoTime_TimestampToNanos(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:79
static bool JunoTime_TimestampGreaterThan(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Compare timestamps (tLeft > tRight).
Definition time_api.h:217
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:50
static bool JunoTime_TimestampLessThan(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Compare timestamps (tLeft < tRight).
Definition time_api.h:223
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:235
JUNO_TIMESTAMP_RESULT_T JunoTime_DoubleToTimestamp(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp)
Definition juno_time.c:255
JUNO_RESULT_F64_T JunoTime_TimestampToDouble(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTimestamp)
Definition juno_time.c:240
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:27
JUNO_TIME_MILLIS_RESULT_T JunoTime_TimestampToMillis(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:150
JUNO_TIMESTAMP_RESULT_T JunoTime_NanosToTimestamp(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIME_NANOS_T iNanos)
Definition juno_time.c:185
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:106
JUNO_TIME_SECONDS_T iSeconds
Whole seconds component of time.
Definition time_api.h:108
JUNO_TIME_SUBSECONDS_T iSubSeconds
Definition time_api.h:111
Vtable for time operations and conversions.
Definition time_api.h:130
JUNO_TIMESTAMP_RESULT_T(* DoubleToTimestamp)(const JUNO_TIME_ROOT_T *ptTime, double dTimestamp)
Convert a double to a timestamp.
Definition time_api.h:156
JUNO_TIMESTAMP_RESULT_T(* Now)(const JUNO_TIME_ROOT_T *ptTime)
Get the current time as specified by the implementation.
Definition time_api.h:132
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:138
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:150
JUNO_STATUS_T(* Sleep)(const JUNO_TIME_ROOT_T *ptTime, JUNO_TIMESTAMP_T tDuration)
Sleep this thread for a duration.
Definition time_api.h:140
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:142
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:146
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:134
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:148
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:152
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:144
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:136
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:154
Common module result type aliases used throughout LibJuno.