summaryrefslogtreecommitdiffstats
path: root/libmisc/date_to_str.c
blob: a3a65af51a5d4a081380b506e5c7b6eb6e451525 (plain) (blame)
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
/*
 * SPDX-FileCopyrightText: 2021, Alejandro Colomar <alx@kernel.org>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <string.h>
#include <time.h>

#ident "$Id$"

#include "prototypes.h"

void
date_to_str(size_t size, char buf[size], long date)
{
	time_t           t;
	const struct tm  *tm;

	t = date;
	if (date < 0) {
		(void) strlcpy(buf, "never", size);
		return;
	}

	tm = gmtime(&t);
	if (tm == NULL) {
		(void) strlcpy(buf, "future", size);
		return;
	}

	(void) strftime(buf, size, "%Y-%m-%d", tm);
	buf[size - 1] = '\0';
}