1 /*        $NetBSD: timetoa.c,v 1.7 2020/05/25 20:47:24 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 *
format_time_fraction(time_t secs,long frac,int prec)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           ldiv_t              qr;
63 
64           DEBUG_REQUIRE(prec != 0);
65 
66           LIB_GETBUF(cp);
67           secs_u = (u_time)secs;
68 
69           /* check if we need signed or unsigned mode */
70           notneg = (prec < 0);
71           prec_u = abs(prec);
72           /* fraclimit = (long)pow(10, prec_u); */
73           for (fraclimit = 10, u = 1; u < prec_u; u++) {
74                     DEBUG_INSIST(fraclimit < fraclimit * 10);
75                     fraclimit *= 10;
76           }
77 
78           /*
79            * Since conversion to string uses lots of divisions anyway,
80            * there's no big extra penalty for normalisation. We do it for
81            * consistency.
82            */
83           if (frac < 0 || frac >= fraclimit) {
84                     qr = ldiv(frac, fraclimit);
85                     if (qr.rem < 0) {
86                               qr.quot--;
87                               qr.rem += fraclimit;
88                     }
89                     secs_u += (time_t)qr.quot;
90                     frac = qr.rem;
91           }
92 
93           /* Get the absolute value of the split representation time. */
94           notneg = notneg || ((time_t)secs_u >= 0);
95           if (!notneg) {
96                     secs_u = ~secs_u;
97                     if (0 == frac)
98                               secs_u++;
99                     else
100                               frac = fraclimit - frac;
101           }
102 
103           /* finally format the data and return the result */
104           snprintf(cp, LIB_BUFLENGTH, "%s%" UTIME_FORMAT ".%0*ld",
105               notneg? "" : "-", secs_u, prec_u, frac);
106 
107           return cp;
108 }
109