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