xref: /freebsd/contrib/ntp/libntp/prettydate.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*
2  * prettydate - convert a time stamp to something readable
3  */
4 #include <stdio.h>
5 
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 #ifndef TM_IN_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 
20 static const char *days[] = {
21   "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
22 };
23 
24 char *
25 prettydate(
26 	l_fp *ts
27 	)
28 {
29 	char *bp;
30 	struct tm *tm;
31 	time_t sec;
32 	u_long msec;
33 
34 	LIB_GETBUF(bp);
35 
36 	sec = ts->l_ui - JAN_1970;
37 	msec = ts->l_uf / 4294967;	/* fract / (2 ** 32 / 1000) */
38 
39 	tm = localtime(&sec);
40 
41 	(void) sprintf(bp, "%08lx.%08lx  %s, %s %2d %4d %2d:%02d:%02d.%03lu",
42 		       (u_long)ts->l_ui, (u_long)ts->l_uf, days[tm->tm_wday],
43 		       months[tm->tm_mon], tm->tm_mday, 1900 + tm->tm_year,
44 		       tm->tm_hour,tm->tm_min, tm->tm_sec, msec);
45 
46 	return bp;
47 }
48 
49 char *
50 gmprettydate(
51 	l_fp *ts
52 	)
53 {
54 	char *bp;
55 	struct tm *tm;
56 	time_t sec;
57 	u_long msec;
58 
59 	LIB_GETBUF(bp);
60 
61 	sec = ts->l_ui - JAN_1970;
62 	msec = ts->l_uf / 4294967;	/* fract / (2 ** 32 / 1000) */
63 
64 	tm = gmtime(&sec);
65 
66 	(void) sprintf(bp, "%08lx.%08lx  %s, %s %2d %4d %2d:%02d:%02d.%03lu UTC",
67 		       (u_long)ts->l_ui, (u_long)ts->l_uf, days[tm->tm_wday],
68 		       months[tm->tm_mon], tm->tm_mday, 1900 + tm->tm_year,
69 		       tm->tm_hour,tm->tm_min, tm->tm_sec, msec);
70 
71 	return bp;
72 }
73