xref: /freebsd/contrib/ntp/libntp/prettydate.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 static const char *months[] = {
12   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
13   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
14 };
15 
16 static const char *days[] = {
17   "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
18 };
19 
20 char *
21 prettydate(
22 	l_fp *ts
23 	)
24 {
25 	char *bp;
26 	struct tm *tm;
27 	time_t sec;
28 	u_long msec;
29 
30 	LIB_GETBUF(bp);
31 
32 	sec = ts->l_ui - JAN_1970;
33 	msec = ts->l_uf / 4294967;	/* fract / (2 ** 32 / 1000) */
34 
35 	tm = localtime(&sec);
36 
37 	(void) sprintf(bp, "%08lx.%08lx  %s, %s %2d %4d %2d:%02d:%02d.%03lu",
38 		       (u_long)ts->l_ui, (u_long)ts->l_uf, days[tm->tm_wday],
39 		       months[tm->tm_mon], tm->tm_mday, 1900 + tm->tm_year,
40 		       tm->tm_hour,tm->tm_min, tm->tm_sec, msec);
41 
42 	return bp;
43 }
44 
45 char *
46 gmprettydate(
47 	l_fp *ts
48 	)
49 {
50 	char *bp;
51 	struct tm *tm;
52 	time_t sec;
53 	u_long msec;
54 
55 	LIB_GETBUF(bp);
56 
57 	sec = ts->l_ui - JAN_1970;
58 	msec = ts->l_uf / 4294967;	/* fract / (2 ** 32 / 1000) */
59 
60 	tm = gmtime(&sec);
61 
62 	(void) sprintf(bp, "%08lx.%08lx  %s, %s %2d %4d %2d:%02d:%02d.%03lu UTC",
63 		       (u_long)ts->l_ui, (u_long)ts->l_uf, days[tm->tm_wday],
64 		       months[tm->tm_mon], tm->tm_mday, 1900 + tm->tm_year,
65 		       tm->tm_hour,tm->tm_min, tm->tm_sec, msec);
66 
67 	return bp;
68 }
69