ProtoCore v0.0.2
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
time_compat.h
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file time_compat.h
6 * @brief Reentrant UTC broken-down time, portable across the host and target toolchains.
7 *
8 * Responses are formatted from worker threads, so every conversion must write into caller storage -
9 * never the shared static `tm` that `gmtime()` returns. The toolchains disagree on how to spell
10 * that: newlib and glibc have `gmtime_r`, the Windows CRT has only `gmtime_s`, with the arguments
11 * reversed. This is the one seam that hides the difference, so callers write it once.
12 *
13 * @author Douglas Quigg (dstroy0)
14 * @date 2026
15 */
16
17#ifndef PROTOCORE_TIME_COMPAT_H
18#define PROTOCORE_TIME_COMPAT_H
19
20#include <time.h>
21
22/**
23 * @brief Convert @p epoch to broken-down UTC in caller storage (reentrant).
24 * @param epoch Seconds since the Unix epoch.
25 * @param out Destination `struct tm` (must be non-null).
26 * @return @p out on success, or NULL if @p epoch cannot be represented.
27 */
28inline struct tm *pc_gmtime_r(const time_t *epoch, struct tm *out)
29{
30#if defined(_WIN32)
31 // MS runtime: gmtime_s(tm, time) - arguments reversed vs POSIX, returns errno_t (0 == ok).
32 return gmtime_s(out, epoch) == 0 ? out : NULL;
33#else
34 return gmtime_r(epoch, out);
35#endif
36}
37
38#endif // PROTOCORE_TIME_COMPAT_H
struct tm * pc_gmtime_r(const time_t *epoch, struct tm *out)
Convert epoch to broken-down UTC in caller storage (reentrant).
Definition time_compat.h:28