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