Branch data Line data Source code
1 : : /* SPDX-FileCopyrightText: Copyright 2026 Cozens Software Solutions Limited
2 : : * SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 OR LicenseRef-PolyForm-Internal-Use-1.0.0 OR LicenseRef-COSOSO-Commercial
3 : : */
4 : :
5 : : #include "SolidSyslogTimestampFormatter.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <stdint.h>
9 : :
10 : : #include "SolidSyslogFormatter.h"
11 : : #include "SolidSyslogTimestamp.h"
12 : :
13 : : struct SolidSyslogFormatter;
14 : :
15 : : static inline bool TimestampFormatter_IsValid(const struct SolidSyslogTimestamp* ts);
16 : : static inline void TimestampFormatter_FormatValid(
17 : : struct SolidSyslogFormatter* formatter,
18 : : const struct SolidSyslogTimestamp* ts
19 : : );
20 : : static inline void TimestampFormatter_FormatUtcOffset(struct SolidSyslogFormatter* formatter, int16_t offsetMinutes);
21 : : static inline void TimestampFormatter_FormatNonZeroUtcOffset(
22 : : struct SolidSyslogFormatter* formatter,
23 : : int16_t offsetMinutes
24 : : );
25 : : static inline int16_t TimestampFormatter_AbsoluteInt16(int16_t value);
26 : :
27 : 6661 : void SolidSyslogTimestampFormatter_Format(
28 : : struct SolidSyslogFormatter* formatter,
29 : : const struct SolidSyslogTimestamp* timestamp
30 : : )
31 : : {
32 [ + + ]: 6661 : if (TimestampFormatter_IsValid(timestamp))
33 : : {
34 : 23 : TimestampFormatter_FormatValid(formatter, timestamp);
35 : : }
36 : : else
37 : : {
38 : 6638 : SolidSyslogFormatter_NilValue(formatter);
39 : : }
40 : 6661 : }
41 : :
42 : 6661 : static inline bool TimestampFormatter_IsValid(const struct SolidSyslogTimestamp* ts)
43 : : {
44 [ + + + + ]: 6661 : bool valid = (ts->Month >= 1U) && (ts->Month <= 12U);
45 : :
46 [ + + + + : 6661 : valid = valid && (ts->Day >= 1U) && (ts->Day <= 31U);
+ + ]
47 [ + + + + ]: 6661 : valid = valid && (ts->Hour <= 23U);
48 [ + + + + ]: 6661 : valid = valid && (ts->Minute <= 59U);
49 [ + + + + ]: 6661 : valid = valid && (ts->Second <= 59U);
50 [ + + + + ]: 6661 : valid = valid && (ts->Microsecond <= 999999U);
51 [ + + + + : 6661 : valid = valid && (ts->UtcOffsetMinutes >= -720) && (ts->UtcOffsetMinutes <= 840);
+ + ]
52 : :
53 : 6661 : return valid;
54 : : }
55 : :
56 : 23 : static inline void TimestampFormatter_FormatValid(
57 : : struct SolidSyslogFormatter* formatter,
58 : : const struct SolidSyslogTimestamp* ts
59 : : )
60 : : {
61 : 23 : SolidSyslogFormatter_FourDigit(formatter, ts->Year);
62 : 23 : SolidSyslogFormatter_AsciiCharacter(formatter, '-');
63 : 23 : SolidSyslogFormatter_TwoDigit(formatter, ts->Month);
64 : 23 : SolidSyslogFormatter_AsciiCharacter(formatter, '-');
65 : 23 : SolidSyslogFormatter_TwoDigit(formatter, ts->Day);
66 : 23 : SolidSyslogFormatter_AsciiCharacter(formatter, 'T');
67 : 23 : SolidSyslogFormatter_TwoDigit(formatter, ts->Hour);
68 : 23 : SolidSyslogFormatter_AsciiCharacter(formatter, ':');
69 : 23 : SolidSyslogFormatter_TwoDigit(formatter, ts->Minute);
70 : 23 : SolidSyslogFormatter_AsciiCharacter(formatter, ':');
71 : 23 : SolidSyslogFormatter_TwoDigit(formatter, ts->Second);
72 : 23 : SolidSyslogFormatter_AsciiCharacter(formatter, '.');
73 : 23 : SolidSyslogFormatter_SixDigit(formatter, ts->Microsecond);
74 : 23 : TimestampFormatter_FormatUtcOffset(formatter, ts->UtcOffsetMinutes);
75 : 23 : }
76 : :
77 : 23 : static inline void TimestampFormatter_FormatUtcOffset(struct SolidSyslogFormatter* formatter, int16_t offsetMinutes)
78 : : {
79 [ + + ]: 23 : if (offsetMinutes == 0)
80 : : {
81 : 18 : SolidSyslogFormatter_AsciiCharacter(formatter, 'Z');
82 : : }
83 : : else
84 : : {
85 : 5 : TimestampFormatter_FormatNonZeroUtcOffset(formatter, offsetMinutes);
86 : : }
87 : 23 : }
88 : :
89 : 5 : static inline void TimestampFormatter_FormatNonZeroUtcOffset(
90 : : struct SolidSyslogFormatter* formatter,
91 : : int16_t offsetMinutes
92 : : )
93 : : {
94 : 5 : uint32_t absoluteMinutes = (uint32_t) TimestampFormatter_AbsoluteInt16(offsetMinutes);
95 : :
96 [ + + ]: 5 : SolidSyslogFormatter_AsciiCharacter(formatter, (offsetMinutes > 0) ? '+' : '-');
97 : 5 : SolidSyslogFormatter_TwoDigit(formatter, absoluteMinutes / 60U);
98 : 5 : SolidSyslogFormatter_AsciiCharacter(formatter, ':');
99 : 5 : SolidSyslogFormatter_TwoDigit(formatter, absoluteMinutes % 60U);
100 : 5 : }
101 : :
102 : 5 : static inline int16_t TimestampFormatter_AbsoluteInt16(int16_t value)
103 : : {
104 : 5 : int16_t result = value;
105 : :
106 [ + + ]: 5 : if (value < 0)
107 : : {
108 : 2 : result = (int16_t) (-value);
109 : : }
110 : :
111 : 5 : return result;
112 : : }
|