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