xref: /freebsd/contrib/ntp/libntp/timetoa.c (revision 416ba5c74546f32a993436a99516d35008e9f384)
1*2b15cb3dSCy Schubert /*
2*2b15cb3dSCy Schubert  * timetoa.c -- time_t related string formatting
3*2b15cb3dSCy Schubert  *
4*2b15cb3dSCy Schubert  * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5*2b15cb3dSCy Schubert  * The contents of 'html/copyright.html' apply.
6*2b15cb3dSCy Schubert  *
7*2b15cb3dSCy Schubert  * Printing a 'time_t' has a lot of portability pitfalls, due to it's
8*2b15cb3dSCy Schubert  * opaque base type. The only requirement imposed by the standard is
9*2b15cb3dSCy Schubert  * that it must be a numeric type. For all practical purposes it's a
10*2b15cb3dSCy Schubert  * signed int, and 32 bits are common.
11*2b15cb3dSCy Schubert  *
12*2b15cb3dSCy Schubert  * Since the UN*X time epoch will cause a signed integer overflow for
13*2b15cb3dSCy Schubert  * 32-bit signed int in the year 2038, implementations slowly move to
14*2b15cb3dSCy Schubert  * 64bit base types for time_t, even in 32-bit environments.
15*2b15cb3dSCy Schubert  *
16*2b15cb3dSCy Schubert  * As the printf() family has no standardised type specifier for time_t,
17*2b15cb3dSCy Schubert  * guessing the right output format specifier is a bit troublesome and
18*2b15cb3dSCy Schubert  * best done with the help of the preprocessor and "config.h".
19*2b15cb3dSCy Schubert  */
20*2b15cb3dSCy Schubert 
21*2b15cb3dSCy Schubert #include "config.h"
22*2b15cb3dSCy Schubert 
23*2b15cb3dSCy Schubert #include <math.h>
24*2b15cb3dSCy Schubert #include <stdio.h>
25*2b15cb3dSCy Schubert 
26*2b15cb3dSCy Schubert #include "timetoa.h"
27*2b15cb3dSCy Schubert #include "ntp_assert.h"
28*2b15cb3dSCy Schubert #include "lib_strbuf.h"
29*2b15cb3dSCy Schubert 
30*2b15cb3dSCy Schubert /*
31*2b15cb3dSCy Schubert  * Formatting to string needs at max 40 bytes (even with 64 bit time_t),
32*2b15cb3dSCy Schubert  * so we check LIB_BUFLENGTH is big enough for our purpose.
33*2b15cb3dSCy Schubert  */
34*2b15cb3dSCy Schubert #if LIB_BUFLENGTH < 40
35*2b15cb3dSCy Schubert # include "GRONK: LIB_BUFLENGTH is not sufficient"
36*2b15cb3dSCy Schubert #endif
37*2b15cb3dSCy Schubert 
38*2b15cb3dSCy Schubert /*
39*2b15cb3dSCy Schubert  * general fractional timestamp formatting
40*2b15cb3dSCy Schubert  *
41*2b15cb3dSCy Schubert  * Many pieces of ntpd require a machine with two's complement
42*2b15cb3dSCy Schubert  * representation of signed integers, so we don't go through the whole
43*2b15cb3dSCy Schubert  * rigamarole of creating fully portable code here. But we have to stay
44*2b15cb3dSCy Schubert  * away from signed integer overflow, as this might cause trouble even
45*2b15cb3dSCy Schubert  * with two's complement representation.
46*2b15cb3dSCy Schubert  */
47*2b15cb3dSCy Schubert const char *
format_time_fraction(time_t secs,long frac,int prec)48*2b15cb3dSCy Schubert format_time_fraction(
49*2b15cb3dSCy Schubert 	time_t	secs,
50*2b15cb3dSCy Schubert 	long	frac,
51*2b15cb3dSCy Schubert 	int	prec
52*2b15cb3dSCy Schubert 	)
53*2b15cb3dSCy Schubert {
54*2b15cb3dSCy Schubert 	char *		cp;
55*2b15cb3dSCy Schubert 	u_int		prec_u;
56*2b15cb3dSCy Schubert 	u_time		secs_u;
57*2b15cb3dSCy Schubert 	u_int		u;
58*2b15cb3dSCy Schubert 	long		fraclimit;
59*2b15cb3dSCy Schubert 	int		notneg;	/* flag for non-negative value	*/
60*2b15cb3dSCy Schubert 	ldiv_t		qr;
61*2b15cb3dSCy Schubert 
62*2b15cb3dSCy Schubert 	DEBUG_REQUIRE(prec != 0);
63*2b15cb3dSCy Schubert 
64*2b15cb3dSCy Schubert 	LIB_GETBUF(cp);
65*2b15cb3dSCy Schubert 	secs_u = (u_time)secs;
66*2b15cb3dSCy Schubert 
67*2b15cb3dSCy Schubert 	/* check if we need signed or unsigned mode */
68*2b15cb3dSCy Schubert 	notneg = (prec < 0);
69*2b15cb3dSCy Schubert 	prec_u = abs(prec);
70*2b15cb3dSCy Schubert 	/* fraclimit = (long)pow(10, prec_u); */
71*2b15cb3dSCy Schubert 	for (fraclimit = 10, u = 1; u < prec_u; u++) {
72*2b15cb3dSCy Schubert 		DEBUG_INSIST(fraclimit < fraclimit * 10);
73*2b15cb3dSCy Schubert 		fraclimit *= 10;
74*2b15cb3dSCy Schubert 	}
75*2b15cb3dSCy Schubert 
76*2b15cb3dSCy Schubert 	/*
77*2b15cb3dSCy Schubert 	 * Since conversion to string uses lots of divisions anyway,
78*2b15cb3dSCy Schubert 	 * there's no big extra penalty for normalisation. We do it for
79*2b15cb3dSCy Schubert 	 * consistency.
80*2b15cb3dSCy Schubert 	 */
81*2b15cb3dSCy Schubert 	if (frac < 0 || frac >= fraclimit) {
82*2b15cb3dSCy Schubert 		qr = ldiv(frac, fraclimit);
83*2b15cb3dSCy Schubert 		if (qr.rem < 0) {
84*2b15cb3dSCy Schubert 			qr.quot--;
85*2b15cb3dSCy Schubert 			qr.rem += fraclimit;
86*2b15cb3dSCy Schubert 		}
87*2b15cb3dSCy Schubert 		secs_u += (time_t)qr.quot;
88*2b15cb3dSCy Schubert 		frac = qr.rem;
89*2b15cb3dSCy Schubert 	}
90*2b15cb3dSCy Schubert 
91*2b15cb3dSCy Schubert 	/* Get the absolute value of the split representation time. */
92*2b15cb3dSCy Schubert 	notneg = notneg || ((time_t)secs_u >= 0);
93*2b15cb3dSCy Schubert 	if (!notneg) {
94*2b15cb3dSCy Schubert 		secs_u = ~secs_u;
95*2b15cb3dSCy Schubert 		if (0 == frac)
96*2b15cb3dSCy Schubert 			secs_u++;
97*2b15cb3dSCy Schubert 		else
98*2b15cb3dSCy Schubert 			frac = fraclimit - frac;
99*2b15cb3dSCy Schubert 	}
100*2b15cb3dSCy Schubert 
101*2b15cb3dSCy Schubert 	/* finally format the data and return the result */
102*2b15cb3dSCy Schubert 	snprintf(cp, LIB_BUFLENGTH, "%s%" UTIME_FORMAT ".%0*ld",
103*2b15cb3dSCy Schubert 	    notneg? "" : "-", secs_u, prec_u, frac);
104*2b15cb3dSCy Schubert 
105*2b15cb3dSCy Schubert 	return cp;
106*2b15cb3dSCy Schubert }
107