ErrorLib 1.0.12-master SHA: f73ac03fda
timestamp.h
Go to the documentation of this file.
1
14#ifndef TIMESTAMP_h
15#define TIMESTAMP_h
16
17//-----------------------------------------------------------------------------
18// Includes
19//-----------------------------------------------------------------------------
20#include <stdbool.h>
21#include <stdint.h>
22#include "cmsis_compiler.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27//-----------------------------------------------------------------------------
28// Defines
29//-----------------------------------------------------------------------------
30
32typedef __PACKED_STRUCT
33{
34 uint32_t ui32Seconds;
35 uint32_t ui32NanoSeconds;
37
38typedef __PACKED_STRUCT
39{
40 int32_t i32Year;
41 int32_t i32Month;
42 int32_t i32Day;
43 int32_t i32Hour;
44 int32_t i32Minute;
45 int32_t i32Second;
47
48//-----------------------------------------------------------------------------
49// Function Prototypes
50//-----------------------------------------------------------------------------
51/* Declare getTimestamp here but implementation is device specific */
53uint32_t DateTimeToUtc(Type_DateTime tDateTime);
54Type_DateTime UtcToDateTime(uint32_t UnixTime);
55
56//-----------------------------------------------------------------------------
57// Function bodies
58//-----------------------------------------------------------------------------
59
60/* Function inlining is only possible within the same module (C-File).
61 By placing them in the header and define as static we can workaround this */
62
63
69__INLINE static bool TimestampOlder(Type_Timestamp tTimestamp1, Type_Timestamp tTimestamp2)
70{
71 if (tTimestamp1.ui32Seconds < tTimestamp2.ui32Seconds)
72 return true;
73
74 if (tTimestamp1.ui32Seconds > tTimestamp2.ui32Seconds)
75 return false;
76
77 if (tTimestamp1.ui32NanoSeconds < tTimestamp2.ui32NanoSeconds)
78 return true;
79
80 return false;
81}
82
83
89__INLINE static Type_Timestamp TimestampDifference(Type_Timestamp tTimestamp1, Type_Timestamp tTimestamp2)
90{
91 Type_Timestamp tDiff;
92 int32_t i32DiffNanoseconds;
93
94 tDiff.ui32Seconds = tTimestamp1.ui32Seconds - tTimestamp2.ui32Seconds;
95 i32DiffNanoseconds = tTimestamp1.ui32NanoSeconds - tTimestamp2.ui32NanoSeconds;
96
97 if (i32DiffNanoseconds < 0)
98 {
99 //Signum Correction Nanoseconds
100 tDiff.ui32Seconds -= 1;
101 tDiff.ui32NanoSeconds = i32DiffNanoseconds + 1000000000;
102 }
103 else
104 {
105 tDiff.ui32NanoSeconds = i32DiffNanoseconds;
106 }
107 return tDiff;
108}
109
110#ifdef __cplusplus
111}
112#endif
113#endif // TIMESTAMP_h
Definition: timestamp.h:39
int32_t i32Year
Definition: timestamp.h:40
int32_t i32Month
Definition: timestamp.h:41
int32_t i32Second
Definition: timestamp.h:45
int32_t i32Minute
Definition: timestamp.h:44
int32_t i32Hour
Definition: timestamp.h:43
int32_t i32Day
Definition: timestamp.h:42
Definition of a timestamp type.
Definition: timestamp.h:33
uint32_t ui32NanoSeconds
Nanoseconds of the timestamp.
Definition: timestamp.h:35
uint32_t ui32Seconds
Seconds of the timestamp.
Definition: timestamp.h:34
Type_DateTime UtcToDateTime(uint32_t UnixTime)
Converts seconds that have elapsed since January 1, 2020 to structured UTC.
Definition: timestamp.c:57
Type_Timestamp getTimestamp(void)
uint32_t DateTimeToUtc(Type_DateTime tDateTime)
Converts structured UTC to seconds that have elapsed since January 1, 2020.
Definition: timestamp.c:33
static __INLINE bool TimestampOlder(Type_Timestamp tTimestamp1, Type_Timestamp tTimestamp2)
This functions compares two Type_Timestamp.
Definition: timestamp.h:69
static __INLINE Type_Timestamp TimestampDifference(Type_Timestamp tTimestamp1, Type_Timestamp tTimestamp2)
This functions subtract two Type_Timestamp.
Definition: timestamp.h:89