xref: /netbsd-src/external/bsd/ntp/dist/libntp/humandate.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /*	$NetBSD: humandate.c,v 1.10 2024/08/18 20:47:13 christos Exp $	*/
2 
3 /*
4  * humandate.c - convert an NTP (or the current) time to something readable
5  */
6 #include <config.h>
7 #include <stdio.h>
8 
9 #include "ntp_fp.h"
10 #include "ntp_unixtime.h"	/* includes <sys/time.h> and <time.h> */
11 #include "ntp_stdlib.h"
12 
13 
14 /* This is used in msyslog.c; we don't want to clutter up the log with
15    the year and day of the week, etc.; just the minimal date and time.  */
16 
17 const char *
18 humanlogtime(void)
19 {
20 	char *		bp;
21 	time_t		cursec;
22 	struct tm *	tm;
23 
24 	cursec = time(NULL);
25 	tm = localtime(&cursec);
26 	if (!tm)
27 		return "-- --- --:--:--";
28 
29 	LIB_GETBUF(bp);
30 
31 	snprintf(bp, LIB_BUFLENGTH, "%2d %s %02d:%02d:%02d",
32 		 tm->tm_mday, months[tm->tm_mon],
33 		 tm->tm_hour, tm->tm_min, tm->tm_sec);
34 
35 	return bp;
36 }
37 
38 
39 /*
40  * humantime() -- like humanlogtime() but without date, and with the
41  *		  time to display given as an argument.
42  */
43 const char *
44 humantime(
45 	time_t cursec
46 	)
47 {
48 	char *		bp;
49 	struct tm *	tm;
50 
51 	tm = localtime(&cursec);
52 	if (!tm)
53 		return "--:--:--";
54 
55 	LIB_GETBUF(bp);
56 
57 	snprintf(bp, LIB_BUFLENGTH, "%02d:%02d:%02d",
58 		 tm->tm_hour, tm->tm_min, tm->tm_sec);
59 
60 	return bp;
61 }
62