summaryrefslogtreecommitdiffstats
path: root/lib/time/day_to_str.h
blob: 96cec6e1fe878a6824330db2d84fd4a82e0535a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-FileCopyrightText: 2021-2024, Alejandro Colomar <alx@kernel.org>
// SPDX-FileCopyrightText: 2024, Tobias Stoeckmann <tobias@stoeckmann.org>
// SPDX-License-Identifier: BSD-3-Clause


#ifndef SHADOW_INCLUDE_LIB_TIME_DAY_TO_STR_H_
#define SHADOW_INCLUDE_LIB_TIME_DAY_TO_STR_H_


#include <config.h>

#include <time.h>

#include "defines.h"
#include "sizeof.h"
#include "string/strtcpy.h"


#define DAY_TO_STR(str, day)   day_to_str(NITEMS(str), str, day)


inline void day_to_str(size_t size, char buf[size], long day);


inline void
day_to_str(size_t size, char buf[size], long day)
{
	time_t     date;
	struct tm  tm;

	if (day < 0) {
		strtcpy(buf, "never", size);
		return;
	}

	if (__builtin_mul_overflow(day, DAY, &date)) {
		strtcpy(buf, "future", size);
		return;
	}

	if (gmtime_r(&date, &tm) == NULL) {
		strtcpy(buf, "future", size);
		return;
	}

	if (strftime(buf, size, "%Y-%m-%d", &tm) == 0)
		strtcpy(buf, "future", size);
}


#endif  // include guard