ErrorLib 1.0.8-master SHA: 810228da25
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//-----------------------------------------------------------------------------
25// Defines
26//-----------------------------------------------------------------------------
27
29typedef __PACKED_STRUCT
30{
31 uint32_t ui32Seconds;
32 uint32_t ui32NanoSeconds;
34
35typedef __PACKED_STRUCT
36{
37 int32_t i32Year;
38 int32_t i32Month;
39 int32_t i32Day;
40 int32_t i32Hour;
41 int32_t i32Minute;
42 int32_t i32Second;
44
45//-----------------------------------------------------------------------------
46// Function Prototypes
47//-----------------------------------------------------------------------------
48/* Declare getTimestamp here but implementation is device specific */
50uint32_t DateTimeToUtc(Type_DateTime tDateTime);
51Type_DateTime UtcToDateTime(uint32_t UnixTime);
52
53//-----------------------------------------------------------------------------
54// Function bodies
55//-----------------------------------------------------------------------------
56
57/* Function inlining is only possible within the same module (C-File).
58 By placing them in the header and define as static we can workaround this */
59
60
66__INLINE static bool TimestampOlder(Type_Timestamp tTimestamp1, Type_Timestamp tTimestamp2)
67{
68 if (tTimestamp1.ui32Seconds < tTimestamp2.ui32Seconds)
69 return true;
70
71 if (tTimestamp1.ui32Seconds > tTimestamp2.ui32Seconds)
72 return false;
73
74 if (tTimestamp1.ui32NanoSeconds < tTimestamp2.ui32NanoSeconds)
75 return true;
76
77 return false;
78}
79
80
86__INLINE static Type_Timestamp TimestampDifference(Type_Timestamp tTimestamp1, Type_Timestamp tTimestamp2)
87{
88 Type_Timestamp tDiff;
89 int32_t i32DiffNanoseconds;
90
91 tDiff.ui32Seconds = tTimestamp1.ui32Seconds - tTimestamp2.ui32Seconds;
92 i32DiffNanoseconds = tTimestamp1.ui32NanoSeconds - tTimestamp2.ui32NanoSeconds;
93
94 if (i32DiffNanoseconds < 0)
95 {
96 //Signum Correction Nanoseconds
97 tDiff.ui32Seconds -= 1;
98 tDiff.ui32NanoSeconds = i32DiffNanoseconds + 1000000000;
99 }
100 else
101 {
102 tDiff.ui32NanoSeconds = i32DiffNanoseconds;
103 }
104 return tDiff;
105}
106
107#endif // TIMESTAMP_h
Definition: timestamp.h:36
int32_t i32Year
Definition: timestamp.h:37
int32_t i32Month
Definition: timestamp.h:38
int32_t i32Second
Definition: timestamp.h:42
int32_t i32Minute
Definition: timestamp.h:41
int32_t i32Hour
Definition: timestamp.h:40
int32_t i32Day
Definition: timestamp.h:39
Definition of a timestamp type.
Definition: timestamp.h:30
uint32_t ui32NanoSeconds
Nanoseconds of the timestamp.
Definition: timestamp.h:32
uint32_t ui32Seconds
Seconds of the timestamp.
Definition: timestamp.h:31
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:66
static __INLINE Type_Timestamp TimestampDifference(Type_Timestamp tTimestamp1, Type_Timestamp tTimestamp2)
This functions subtract two Type_Timestamp.
Definition: timestamp.h:86