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