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 "SolidSyslogPosixClock.h"
6 : :
7 : : #include <stdbool.h>
8 : : #include <time.h>
9 : : #include <stdint.h>
10 : :
11 : : #include "SolidSyslogTimestamp.h"
12 : :
13 : : struct timespec;
14 : : struct tm;
15 : :
16 : : static inline bool PosixClock_GetBrokenDownTime(struct timespec* now, struct tm* breakdown);
17 : : static inline void PosixClock_PopulateTimestamp(
18 : : struct SolidSyslogTimestamp* timestamp,
19 : : const struct timespec* now,
20 : : const struct tm* breakdown
21 : : );
22 : :
23 : 35 : void SolidSyslogPosix_GetTimestamp(struct SolidSyslogTimestamp* timestamp)
24 : : {
25 : : struct timespec now;
26 : : struct tm breakdown;
27 : :
28 : 35 : *timestamp = (struct SolidSyslogTimestamp) {0};
29 : :
30 [ + + ]: 35 : if (PosixClock_GetBrokenDownTime(&now, &breakdown))
31 : : {
32 : 33 : PosixClock_PopulateTimestamp(timestamp, &now, &breakdown);
33 : : }
34 : 35 : }
35 : :
36 : 35 : static inline bool PosixClock_GetBrokenDownTime(struct timespec* now, struct tm* breakdown)
37 : : {
38 [ + + + + ]: 35 : return (clock_gettime(CLOCK_REALTIME, now) == 0) && (gmtime_r(&now->tv_sec, breakdown) != NULL);
39 : : }
40 : :
41 : 33 : static inline void PosixClock_PopulateTimestamp(
42 : : struct SolidSyslogTimestamp* timestamp,
43 : : const struct timespec* now,
44 : : const struct tm* breakdown
45 : : )
46 : : {
47 : 33 : timestamp->Year = (uint16_t) (breakdown->tm_year + 1900);
48 : 33 : timestamp->Month = (uint8_t) (breakdown->tm_mon + 1);
49 : 33 : timestamp->Day = (uint8_t) breakdown->tm_mday;
50 : 33 : timestamp->Hour = (uint8_t) breakdown->tm_hour;
51 : 33 : timestamp->Minute = (uint8_t) breakdown->tm_min;
52 : 33 : timestamp->Second = (uint8_t) breakdown->tm_sec;
53 : 33 : timestamp->Microsecond = (uint32_t) (now->tv_nsec / 1000);
54 : 33 : }
|