xref: /netbsd-src/external/bsd/ntp/dist/libntp/timetoa.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: timetoa.c,v 1.7 2020/05/25 20:47:24 christos Exp $	*/
28585484eSchristos 
38585484eSchristos /*
48585484eSchristos  * timetoa.c -- time_t related string formatting
58585484eSchristos  *
68585484eSchristos  * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
78585484eSchristos  * The contents of 'html/copyright.html' apply.
88585484eSchristos  *
98585484eSchristos  * Printing a 'time_t' has a lot of portability pitfalls, due to it's
108585484eSchristos  * opaque base type. The only requirement imposed by the standard is
118585484eSchristos  * that it must be a numeric type. For all practical purposes it's a
128585484eSchristos  * signed int, and 32 bits are common.
138585484eSchristos  *
148585484eSchristos  * Since the UN*X time epoch will cause a signed integer overflow for
158585484eSchristos  * 32-bit signed int in the year 2038, implementations slowly move to
168585484eSchristos  * 64bit base types for time_t, even in 32-bit environments.
178585484eSchristos  *
188585484eSchristos  * As the printf() family has no standardised type specifier for time_t,
198585484eSchristos  * guessing the right output format specifier is a bit troublesome and
208585484eSchristos  * best done with the help of the preprocessor and "config.h".
218585484eSchristos  */
228585484eSchristos 
238585484eSchristos #include "config.h"
248585484eSchristos 
258585484eSchristos #include <math.h>
268585484eSchristos #include <stdio.h>
278585484eSchristos 
288585484eSchristos #include "timetoa.h"
298585484eSchristos #include "ntp_assert.h"
308585484eSchristos #include "lib_strbuf.h"
318585484eSchristos 
328585484eSchristos /*
338585484eSchristos  * Formatting to string needs at max 40 bytes (even with 64 bit time_t),
348585484eSchristos  * so we check LIB_BUFLENGTH is big enough for our purpose.
358585484eSchristos  */
368585484eSchristos #if LIB_BUFLENGTH < 40
378585484eSchristos # include "GRONK: LIB_BUFLENGTH is not sufficient"
388585484eSchristos #endif
398585484eSchristos 
408585484eSchristos /*
418585484eSchristos  * general fractional timestamp formatting
428585484eSchristos  *
438585484eSchristos  * Many pieces of ntpd require a machine with two's complement
448585484eSchristos  * representation of signed integers, so we don't go through the whole
458585484eSchristos  * rigamarole of creating fully portable code here. But we have to stay
468585484eSchristos  * away from signed integer overflow, as this might cause trouble even
478585484eSchristos  * with two's complement representation.
488585484eSchristos  */
498585484eSchristos const char *
format_time_fraction(time_t secs,long frac,int prec)508585484eSchristos format_time_fraction(
518585484eSchristos 	time_t	secs,
528585484eSchristos 	long	frac,
538585484eSchristos 	int	prec
548585484eSchristos 	)
558585484eSchristos {
568585484eSchristos 	char *		cp;
578585484eSchristos 	u_int		prec_u;
588585484eSchristos 	u_time		secs_u;
598585484eSchristos 	u_int		u;
608585484eSchristos 	long		fraclimit;
618585484eSchristos 	int		notneg;	/* flag for non-negative value	*/
628585484eSchristos 	ldiv_t		qr;
638585484eSchristos 
648585484eSchristos 	DEBUG_REQUIRE(prec != 0);
658585484eSchristos 
668585484eSchristos 	LIB_GETBUF(cp);
678585484eSchristos 	secs_u = (u_time)secs;
688585484eSchristos 
698585484eSchristos 	/* check if we need signed or unsigned mode */
708585484eSchristos 	notneg = (prec < 0);
718585484eSchristos 	prec_u = abs(prec);
728585484eSchristos 	/* fraclimit = (long)pow(10, prec_u); */
738585484eSchristos 	for (fraclimit = 10, u = 1; u < prec_u; u++) {
748585484eSchristos 		DEBUG_INSIST(fraclimit < fraclimit * 10);
758585484eSchristos 		fraclimit *= 10;
768585484eSchristos 	}
778585484eSchristos 
788585484eSchristos 	/*
798585484eSchristos 	 * Since conversion to string uses lots of divisions anyway,
808585484eSchristos 	 * there's no big extra penalty for normalisation. We do it for
818585484eSchristos 	 * consistency.
828585484eSchristos 	 */
838585484eSchristos 	if (frac < 0 || frac >= fraclimit) {
848585484eSchristos 		qr = ldiv(frac, fraclimit);
858585484eSchristos 		if (qr.rem < 0) {
868585484eSchristos 			qr.quot--;
878585484eSchristos 			qr.rem += fraclimit;
888585484eSchristos 		}
898585484eSchristos 		secs_u += (time_t)qr.quot;
908585484eSchristos 		frac = qr.rem;
918585484eSchristos 	}
928585484eSchristos 
938585484eSchristos 	/* Get the absolute value of the split representation time. */
948585484eSchristos 	notneg = notneg || ((time_t)secs_u >= 0);
95f7fd78b7Sjoerg 	if (!notneg) {
968585484eSchristos 		secs_u = ~secs_u;
978585484eSchristos 		if (0 == frac)
988585484eSchristos 			secs_u++;
998585484eSchristos 		else
1008585484eSchristos 			frac = fraclimit - frac;
1018585484eSchristos 	}
1028585484eSchristos 
1038585484eSchristos 	/* finally format the data and return the result */
104f7fd78b7Sjoerg 	snprintf(cp, LIB_BUFLENGTH, "%s%" UTIME_FORMAT ".%0*ld",
105f7fd78b7Sjoerg 	    notneg? "" : "-", secs_u, prec_u, frac);
1068585484eSchristos 
1078585484eSchristos 	return cp;
1088585484eSchristos }
109