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