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