Home | History | Annotate | Line # | Download | only in libntp
      1 /*	$NetBSD: uglydate.c,v 1.6 2024/08/18 20:47:13 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 "ntp_stdlib.h"
     13 
     14 
     15 char *
     16 uglydate(
     17 	l_fp *ts
     18 	)
     19 {
     20 	char *bp;
     21 	char *timep;
     22 	struct tm *tm;
     23 	time_t sec;
     24 	long msec;
     25 	int year;
     26 
     27 	timep = ulfptoa(ts, 6);		/* returns max 17 characters */
     28 	LIB_GETBUF(bp);
     29 	sec = ts->l_ui - JAN_1970;
     30 	msec = ts->l_uf / 4294967;	/* fract / (2**32/1000) */
     31 	tm = gmtime(&sec);
     32 	if (ts->l_ui == 0) {
     33 		/*
     34 		 * Probably not a real good thing to do.  Oh, well.
     35 		 */
     36 		year = 0;
     37 		tm->tm_yday = 0;
     38 		tm->tm_hour = 0;
     39 		tm->tm_min = 0;
     40 		tm->tm_sec = 0;
     41 	} else {
     42 		year = tm->tm_year;
     43 		while (year >= 100)
     44 		    year -= 100;
     45 	}
     46 	snprintf(bp, LIB_BUFLENGTH,
     47 		 "%17s %02d:%03d:%02d:%02d:%02d.%03ld", timep, year,
     48 		 tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec,
     49 		 msec);
     50 
     51 	return bp;
     52 }
     53