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