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