xref: /freebsd/contrib/ntp/libntp/uglydate.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*
2  * uglydate - convert a time stamp to something barely readable
3  *	      The string returned is 37 characters long.
4  */
5 #include <config.h>
6 #include <stdio.h>
7 
8 #include "ntp_fp.h"
9 #include "ntp_unixtime.h"
10 #include "lib_strbuf.h"
11 #include "ntp_stdlib.h"
12 
13 
14 char *
15 uglydate(
16 	l_fp *ts
17 	)
18 {
19 	char *bp;
20 	char *timep;
21 	struct tm *tm;
22 	time_t sec;
23 	long msec;
24 	int year;
25 
26 	timep = ulfptoa(ts, 6);		/* returns max 17 characters */
27 	LIB_GETBUF(bp);
28 	sec = ts->l_ui - JAN_1970;
29 	msec = ts->l_uf / 4294967;	/* fract / (2**32/1000) */
30 	tm = gmtime(&sec);
31 	if (ts->l_ui == 0) {
32 		/*
33 		 * Probably not a real good thing to do.  Oh, well.
34 		 */
35 		year = 0;
36 		tm->tm_yday = 0;
37 		tm->tm_hour = 0;
38 		tm->tm_min = 0;
39 		tm->tm_sec = 0;
40 	} else {
41 		year = tm->tm_year;
42 		while (year >= 100)
43 		    year -= 100;
44 	}
45 	snprintf(bp, LIB_BUFLENGTH,
46 		 "%17s %02d:%03d:%02d:%02d:%02d.%03ld", timep, year,
47 		 tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec,
48 		 msec);
49 
50 	return bp;
51 }
52