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