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