xref: /freebsd/contrib/ntp/libntp/humandate.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
1 /*
2  * humandate - convert an NTP (or the current) time to something readable
3  */
4 #include <stdio.h>
5 #include "ntp_fp.h"
6 #include "ntp_unixtime.h"	/* includes <sys/time.h> and <time.h> */
7 #include "lib_strbuf.h"
8 #include "ntp_stdlib.h"
9 
10 static const char *months[] = {
11 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
12 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
13 };
14 static const char *days[] = {
15 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
16 };
17 
18 char *
19 humandate(
20 	u_long ntptime
21 	)
22 {
23 	char *bp;
24 	struct tm *tm;
25 
26 	tm = ntp2unix_tm(ntptime, 1);
27 
28 	if (!tm)
29 		return "--- --- -- ---- --:--:--";
30 
31 	LIB_GETBUF(bp);
32 
33 	(void) sprintf(bp, "%s, %s %2d %4d %2d:%02d:%02d",
34 		       days[tm->tm_wday], months[tm->tm_mon], tm->tm_mday,
35 		       1900+tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
36 
37 	return bp;
38 }
39 
40 
41 /* This is used in msyslog.c; we don't want to clutter up the log with
42    the year and day of the week, etc.; just the minimal date and time.  */
43 
44 char *
45 humanlogtime(void)
46 {
47 	char *bp;
48 	time_t cursec = time((time_t *) 0);
49 	struct tm *tm;
50 
51 	tm = localtime(&cursec);
52 	if (!tm)
53 		return "-- --- --:--:--";
54 
55 	LIB_GETBUF(bp);
56 
57 	(void) sprintf(bp, "%2d %s %02d:%02d:%02d",
58 		       tm->tm_mday, months[tm->tm_mon],
59 		       tm->tm_hour, tm->tm_min, tm->tm_sec);
60 
61 	return bp;
62 }
63