LibJuno 0.42.0
LibJuno is a lightweight C99 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*/
21#ifndef JUNO_TIME_API_H
22#define JUNO_TIME_API_H
23#include "juno/status.h"
24#include "juno/module.h"
25#include "juno/types.h"
26#include <stddef.h>
27#include <stdint.h>
28#ifdef __cplusplus
29extern "C"
30{
31#endif
32
34typedef union JUNO_TIME_TAG JUNO_TIME_T;
35typedef struct JUNO_TIME_ROOT_TAG JUNO_TIME_ROOT_T;
37
38typedef uint64_t JUNO_TIME_SECONDS_T;
39typedef uint64_t JUNO_TIME_MILLIS_T;
40typedef uint64_t JUNO_TIME_MICROS_T;
41typedef uint64_t JUNO_TIME_NANOS_T;
42typedef uint64_t JUNO_TIME_SUBSECONDS_T;
43
61
70
71JUNO_MODULE_RESULT(JUNO_TIMESTAMP_RESULT_T, JUNO_TIMESTAMP_T);
72JUNO_MODULE_RESULT(JUNO_TIME_SECONDS_RESULT_T, JUNO_TIME_SECONDS_T);
73JUNO_MODULE_RESULT(JUNO_TIME_MILLIS_RESULT_T, JUNO_TIME_MILLIS_T);
74JUNO_MODULE_RESULT(JUNO_TIME_MICROS_RESULT_T, JUNO_TIME_MICROS_T);
75JUNO_MODULE_RESULT(JUNO_TIME_NANOS_RESULT_T, JUNO_TIME_NANOS_T);
76JUNO_MODULE_RESULT(JUNO_TIME_SUBSECONDS_RESULT_T, JUNO_TIME_SUBSECONDS_T);
77
79{
81 JUNO_TIMESTAMP_RESULT_T (*Now)(JUNO_TIME_T *ptTime);
91 JUNO_TIME_NANOS_RESULT_T (*TimestampToNanos)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime);
93 JUNO_TIME_MICROS_RESULT_T (*TimestampToMicros)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime);
95 JUNO_TIME_MILLIS_RESULT_T (*TimestampToMillis)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime);
97 JUNO_TIMESTAMP_RESULT_T (*NanosToTimestamp)(JUNO_TIME_T *ptTime, JUNO_TIME_NANOS_T iNanos);
99 JUNO_TIMESTAMP_RESULT_T (*MicrosToTimestamp)(JUNO_TIME_T *ptTime, JUNO_TIME_MICROS_T iMicros);
101 JUNO_TIMESTAMP_RESULT_T (*MillisToTimestamp)(JUNO_TIME_T *ptTime, JUNO_TIME_MILLIS_T iMillis);
103 JUNO_RESULT_F64_T (*TimestampToDouble)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTimestamp);
105 JUNO_TIMESTAMP_RESULT_T (*DoubleToTimestamp)(JUNO_TIME_T *ptTime, double dTimestamp);
106};
107
108
111JUNO_TIME_NANOS_RESULT_T JunoTime_TimestampToNanos(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime);
112JUNO_TIME_MICROS_RESULT_T JunoTime_TimestampToMicros(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime);
113JUNO_TIME_MILLIS_RESULT_T JunoTime_TimestampToMillis(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime);
114JUNO_TIMESTAMP_RESULT_T JunoTime_NanosToTimestamp(JUNO_TIME_T *ptTime, JUNO_TIME_NANOS_T iNanos);
115JUNO_TIMESTAMP_RESULT_T JunoTime_MicrosToTimestamp(JUNO_TIME_T *ptTime, JUNO_TIME_MICROS_T iMicros);
116JUNO_TIMESTAMP_RESULT_T JunoTime_MillisToTimestamp(JUNO_TIME_T *ptTime, JUNO_TIME_MILLIS_T iMillis);
117JUNO_RESULT_F64_T JunoTime_TimestampToDouble(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTimestamp);
118JUNO_TIMESTAMP_RESULT_T JunoTime_DoubleToTimestamp(JUNO_TIME_T *ptTime, double dTimestamp);
119
120#define JUNO_TIME_NEW_API(Now, SleepTo, Sleep) \
121{ \
122 Now, \
123 JunoTime_AddTime, \
124 JunoTime_SubtractTime, \
125 SleepTo, \
126 Sleep, \
127 JunoTime_TimestampToNanos, \
128 JunoTime_TimestampToMicros, \
129 JunoTime_TimestampToMillis, \
130 JunoTime_NanosToTimestamp, \
131 JunoTime_MicrosToTimestamp, \
132 JunoTime_MillisToTimestamp, \
133 JunoTime_TimestampToDouble, \
134 JunoTime_DoubleToTimestamp, \
135}
136
138{
139 return (tLeft.iSeconds > tRight.iSeconds) || (tLeft.iSeconds == tRight.iSeconds && tLeft.iSubSeconds > tRight.iSubSeconds);
140}
141
143{
144 return (tLeft.iSeconds < tRight.iSeconds) || (tLeft.iSeconds == tRight.iSeconds && tLeft.iSubSeconds < tRight.iSubSeconds);
145}
146
148{
149 return (tLeft.iSeconds == tRight.iSeconds) && (tLeft.iSubSeconds == tRight.iSubSeconds);
150}
151
152#ifdef __cplusplus
153}
154#endif
155#endif // JUNO_TIME_API_H
156
#define JUNO_MODULE_EMPTY
Definition module.h:126
#define JUNO_MODULE_RESULT(NAME_T, OK_T)
Defines a result type combining a status and a success payload.
Definition module.h:223
#define JUNO_MODULE_ROOT(API_T,...)
Definition module.h:182
int32_t JUNO_STATUS_T
Definition status.h:23
Definition time_api.h:63
JUNO_TIME_SECONDS_T iSeconds
Seconds component of time.
Definition time_api.h:65
JUNO_TIME_SUBSECONDS_T iSubSeconds
Definition time_api.h:68
Definition time_api.h:79
JUNO_TIMESTAMP_RESULT_T(* NanosToTimestamp)(JUNO_TIME_T *ptTime, JUNO_TIME_NANOS_T iNanos)
Convert nanoseconds to a timestamp.
Definition time_api.h:97
JUNO_STATUS_T(* SleepTo)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTimeToWakeup)
Sleep this thread until a specific time.
Definition time_api.h:87
JUNO_TIMESTAMP_RESULT_T(* DoubleToTimestamp)(JUNO_TIME_T *ptTime, double dTimestamp)
Convert a double to a timestamp.
Definition time_api.h:105
JUNO_TIMESTAMP_RESULT_T(* Now)(JUNO_TIME_T *ptTime)
Get the current time as specified by the implementation.
Definition time_api.h:81
JUNO_TIMESTAMP_RESULT_T(* MillisToTimestamp)(JUNO_TIME_T *ptTime, JUNO_TIME_MILLIS_T iMillis)
Convert milliseconds to a timestamp.
Definition time_api.h:101
JUNO_STATUS_T(* SubtractTime)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToSubtract)
Perform subtraction with time.
Definition time_api.h:85
JUNO_TIME_NANOS_RESULT_T(* TimestampToNanos)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime)
Convert a timestamp to nanoseconds.
Definition time_api.h:91
JUNO_TIMESTAMP_RESULT_T(* MicrosToTimestamp)(JUNO_TIME_T *ptTime, JUNO_TIME_MICROS_T iMicros)
Convert microseconds to a timestamp.
Definition time_api.h:99
JUNO_RESULT_F64_T(* TimestampToDouble)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTimestamp)
Convert a timestamp to a double.
Definition time_api.h:103
JUNO_STATUS_T(* AddTime)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToAdd)
Perform addition with time.
Definition time_api.h:83
JUNO_STATUS_T(* Sleep)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tDuration)
Sleep this thread for a duration.
Definition time_api.h:89
JUNO_TIME_MILLIS_RESULT_T(* TimestampToMillis)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime)
Convert a timestamp to milliseconds.
Definition time_api.h:95
JUNO_TIME_MICROS_RESULT_T(* TimestampToMicros)(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime)
Convert a timestamp to microsconds.
Definition time_api.h:93
uint64_t JUNO_TIME_NANOS_T
Definition time_api.h:41
static bool JunoTime_TimestampEquals(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Definition time_api.h:147
JUNO_TIME_MICROS_RESULT_T JunoTime_TimestampToMicros(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:82
uint64_t JUNO_TIME_MICROS_T
Definition time_api.h:40
JUNO_TIMESTAMP_RESULT_T JunoTime_MicrosToTimestamp(JUNO_TIME_T *ptTime, JUNO_TIME_MICROS_T iMicros)
Definition juno_time.c:146
JUNO_TIMESTAMP_RESULT_T JunoTime_MillisToTimestamp(JUNO_TIME_T *ptTime, JUNO_TIME_MILLIS_T iMillis)
Definition juno_time.c:162
JUNO_TIME_NANOS_RESULT_T JunoTime_TimestampToNanos(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:58
static bool JunoTime_TimestampGreaterThan(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Definition time_api.h:137
JUNO_STATUS_T JunoTime_AddTime(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToAdd)
Definition juno_time.c:8
uint64_t JUNO_TIME_MILLIS_T
Definition time_api.h:39
static bool JunoTime_TimestampLessThan(JUNO_TIMESTAMP_T tLeft, JUNO_TIMESTAMP_T tRight)
Definition time_api.h:142
uint64_t JUNO_TIME_SECONDS_T
Definition time_api.h:38
JUNO_STATUS_T JunoTime_SubtractTime(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T *ptRetTime, JUNO_TIMESTAMP_T tTimeToSubtract)
Definition juno_time.c:30
uint64_t JUNO_TIME_SUBSECONDS_T
Definition time_api.h:42
union JUNO_TIME_TAG JUNO_TIME_T
Definition time_api.h:34
JUNO_TIMESTAMP_RESULT_T JunoTime_DoubleToTimestamp(JUNO_TIME_T *ptTime, double dTimestamp)
Definition juno_time.c:193
JUNO_TIMESTAMP_RESULT_T JunoTime_NanosToTimestamp(JUNO_TIME_T *ptTime, JUNO_TIME_NANOS_T iNanos)
Definition juno_time.c:130
JUNO_RESULT_F64_T JunoTime_TimestampToDouble(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTimestamp)
Definition juno_time.c:179
JUNO_TIME_MILLIS_RESULT_T JunoTime_TimestampToMillis(JUNO_TIME_T *ptTime, JUNO_TIMESTAMP_T tTime)
Definition juno_time.c:106
struct JUNO_TIME_ROOT_TAG JUNO_TIME_ROOT_T
Definition time_api.h:35