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