xref: /freebsd/contrib/ntp/libntp/humandate.c (revision 9207b4cff7b8d483f4dd3c62266c2b58819eb7f9)
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 	time_t sec;
26 
27 	LIB_GETBUF(bp);
28 
29 	sec = ntptime - JAN_1970;
30 	tm = localtime(&sec);
31 
32 	(void) sprintf(bp, "%s, %s %2d %4d %2d:%02d:%02d",
33 		       days[tm->tm_wday], months[tm->tm_mon], tm->tm_mday,
34 		       1900+tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
35 
36 	return bp;
37 }
38 
39 
40 /* This is used in msyslog.c; we don't want to clutter up the log with
41    the year and day of the week, etc.; just the minimal date and time.  */
42 
43 char *
44 humanlogtime(void)
45 {
46 	char *bp;
47 	time_t cursec = time((time_t *) 0);
48 	struct tm *tm = localtime(&cursec);
49 
50 	LIB_GETBUF(bp);
51 
52 	(void) sprintf(bp, "%2d %s %02d:%02d:%02d",
53 		       tm->tm_mday, months[tm->tm_mon],
54 		       tm->tm_hour, tm->tm_min, tm->tm_sec);
55 
56 	return bp;
57 }
58