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