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