1275970Scy/*
2275970Scy * timetoa.h -- time_t related string formatting
3275970Scy *
4275970Scy * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5275970Scy * The contents of 'html/copyright.html' apply.
6275970Scy *
7275970Scy * Printing a 'time_t' has some portability pitfalls, due to it's opaque
8275970Scy * base type. The only requirement imposed by the standard is that it
9275970Scy * must be a numeric type. For all practical purposes it's a signed int,
10275970Scy * and 32 bits are common.
11275970Scy *
12275970Scy * Since the UN*X time epoch will cause a signed integer overflow for
13275970Scy * 32-bit signed int values in the year 2038, implementations slowly
14275970Scy * move to 64bit base types for time_t, even in 32-bit environments. In
15275970Scy * such an environment sizeof(time_t) could be bigger than sizeof(long)
16275970Scy * and the commonly used idiom of casting to long leads to truncation.
17275970Scy *
18275970Scy * As the printf() family has no standardised type specifier for time_t,
19275970Scy * guessing the right output format specifier is a bit troublesome and
20275970Scy * best done with the help of the preprocessor and "config.h".
21275970Scy */
22275970Scy#ifndef TIMETOA_H
23275970Scy#define TIMETOA_H
24275970Scy
25275970Scy#include "ntp_fp.h"
26275970Scy#include "ntp_stdlib.h"
27275970Scy#include "ntp_unixtime.h"
28275970Scy
29275970Scy/*
30275970Scy * Given the size of time_t, guess what can be used as an unsigned value
31275970Scy * to hold a time_t and the printf() format specifcation.
32275970Scy *
33275970Scy * These should be used with the string constant concatenation feature
34275970Scy * of the compiler like this:
35275970Scy *
36275970Scy * printf("a time stamp: %" TIME_FORMAT " and more\n", a_time_t_value);
37275970Scy *
38275970Scy * It's not exactly nice, but there's not much leeway once we want to
39275970Scy * use the printf() family on time_t values.
40275970Scy */
41275970Scy
42275970Scy#if SIZEOF_TIME_T <= SIZEOF_INT
43275970Scy
44275970Scytypedef unsigned int u_time;
45275970Scy#define TIME_FORMAT "d"
46275970Scy#define UTIME_FORMAT "u"
47275970Scy
48275970Scy#elif SIZEOF_TIME_T <= SIZEOF_LONG
49275970Scy
50275970Scytypedef unsigned long u_time;
51275970Scy#define TIME_FORMAT "ld"
52275970Scy#define UTIME_FORMAT "lu"
53275970Scy
54275970Scy#elif defined(SIZEOF_LONG_LONG) && SIZEOF_TIME_T <= SIZEOF_LONG_LONG
55275970Scy
56275970Scytypedef unsigned long long u_time;
57275970Scy#define TIME_FORMAT "lld"
58275970Scy#define UTIME_FORMAT "llu"
59275970Scy
60275970Scy#else
61275970Scy#include "GRONK: what size has a time_t here?"
62275970Scy#endif
63275970Scy
64275970Scy/*
65275970Scy * general fractional time stamp formatting.
66275970Scy *
67275970Scy * secs - integral seconds of time stamp
68275970Scy * frac - fractional units
69275970Scy * prec - log10 of units per second (3=milliseconds, 6=microseconds,..)
70275970Scy *	  or in other words: the count of decimal digits required.
71275970Scy *	  If prec is < 0, abs(prec) is taken for the precision and secs
72275970Scy *	  is treated as an unsigned value.
73275970Scy *
74275970Scy * The function will eventually normalise the fraction and adjust the
75275970Scy * seconds accordingly.
76275970Scy *
77275970Scy * This function uses the string buffer library for the return value,
78275970Scy * so do not keep the resulting pointers around.
79275970Scy */
80275970Scyextern const char *
81275970Scyformat_time_fraction(time_t secs, long frac, int prec);
82275970Scy
83275970Scy#endif /* !defined(TIMETOA_H) */
84