1*2b15cb3dSCy Schubert /* 2*2b15cb3dSCy Schubert * timetoa.h -- time_t related string formatting 3*2b15cb3dSCy Schubert * 4*2b15cb3dSCy Schubert * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project. 5*2b15cb3dSCy Schubert * The contents of 'html/copyright.html' apply. 6*2b15cb3dSCy Schubert * 7*2b15cb3dSCy Schubert * Printing a 'time_t' has some portability pitfalls, due to it's opaque 8*2b15cb3dSCy Schubert * base type. The only requirement imposed by the standard is that it 9*2b15cb3dSCy Schubert * must be a numeric type. For all practical purposes it's a signed int, 10*2b15cb3dSCy Schubert * and 32 bits are common. 11*2b15cb3dSCy Schubert * 12*2b15cb3dSCy Schubert * Since the UN*X time epoch will cause a signed integer overflow for 13*2b15cb3dSCy Schubert * 32-bit signed int values in the year 2038, implementations slowly 14*2b15cb3dSCy Schubert * move to 64bit base types for time_t, even in 32-bit environments. In 15*2b15cb3dSCy Schubert * such an environment sizeof(time_t) could be bigger than sizeof(long) 16*2b15cb3dSCy Schubert * and the commonly used idiom of casting to long leads to truncation. 17*2b15cb3dSCy Schubert * 18*2b15cb3dSCy Schubert * As the printf() family has no standardised type specifier for time_t, 19*2b15cb3dSCy Schubert * guessing the right output format specifier is a bit troublesome and 20*2b15cb3dSCy Schubert * best done with the help of the preprocessor and "config.h". 21*2b15cb3dSCy Schubert */ 22*2b15cb3dSCy Schubert #ifndef TIMETOA_H 23*2b15cb3dSCy Schubert #define TIMETOA_H 24*2b15cb3dSCy Schubert 25*2b15cb3dSCy Schubert #include "ntp_fp.h" 26*2b15cb3dSCy Schubert #include "ntp_stdlib.h" 27*2b15cb3dSCy Schubert #include "ntp_unixtime.h" 28*2b15cb3dSCy Schubert 29*2b15cb3dSCy Schubert /* 30*2b15cb3dSCy Schubert * Given the size of time_t, guess what can be used as an unsigned value 31*2b15cb3dSCy Schubert * to hold a time_t and the printf() format specifcation. 32*2b15cb3dSCy Schubert * 33*2b15cb3dSCy Schubert * These should be used with the string constant concatenation feature 34*2b15cb3dSCy Schubert * of the compiler like this: 35*2b15cb3dSCy Schubert * 36*2b15cb3dSCy Schubert * printf("a time stamp: %" TIME_FORMAT " and more\n", a_time_t_value); 37*2b15cb3dSCy Schubert * 38*2b15cb3dSCy Schubert * It's not exactly nice, but there's not much leeway once we want to 39*2b15cb3dSCy Schubert * use the printf() family on time_t values. 40*2b15cb3dSCy Schubert */ 41*2b15cb3dSCy Schubert 42*2b15cb3dSCy Schubert #if SIZEOF_TIME_T <= SIZEOF_INT 43*2b15cb3dSCy Schubert 44*2b15cb3dSCy Schubert typedef unsigned int u_time; 45*2b15cb3dSCy Schubert #define TIME_FORMAT "d" 46*2b15cb3dSCy Schubert #define UTIME_FORMAT "u" 47*2b15cb3dSCy Schubert 48*2b15cb3dSCy Schubert #elif SIZEOF_TIME_T <= SIZEOF_LONG 49*2b15cb3dSCy Schubert 50*2b15cb3dSCy Schubert typedef unsigned long u_time; 51*2b15cb3dSCy Schubert #define TIME_FORMAT "ld" 52*2b15cb3dSCy Schubert #define UTIME_FORMAT "lu" 53*2b15cb3dSCy Schubert 54*2b15cb3dSCy Schubert #elif defined(SIZEOF_LONG_LONG) && SIZEOF_TIME_T <= SIZEOF_LONG_LONG 55*2b15cb3dSCy Schubert 56*2b15cb3dSCy Schubert typedef unsigned long long u_time; 57*2b15cb3dSCy Schubert #define TIME_FORMAT "lld" 58*2b15cb3dSCy Schubert #define UTIME_FORMAT "llu" 59*2b15cb3dSCy Schubert 60*2b15cb3dSCy Schubert #else 61*2b15cb3dSCy Schubert #include "GRONK: what size has a time_t here?" 62*2b15cb3dSCy Schubert #endif 63*2b15cb3dSCy Schubert 64*2b15cb3dSCy Schubert /* 65*2b15cb3dSCy Schubert * general fractional time stamp formatting. 66*2b15cb3dSCy Schubert * 67*2b15cb3dSCy Schubert * secs - integral seconds of time stamp 68*2b15cb3dSCy Schubert * frac - fractional units 69*2b15cb3dSCy Schubert * prec - log10 of units per second (3=milliseconds, 6=microseconds,..) 70*2b15cb3dSCy Schubert * or in other words: the count of decimal digits required. 71*2b15cb3dSCy Schubert * If prec is < 0, abs(prec) is taken for the precision and secs 72*2b15cb3dSCy Schubert * is treated as an unsigned value. 73*2b15cb3dSCy Schubert * 74*2b15cb3dSCy Schubert * The function will eventually normalise the fraction and adjust the 75*2b15cb3dSCy Schubert * seconds accordingly. 76*2b15cb3dSCy Schubert * 77*2b15cb3dSCy Schubert * This function uses the string buffer library for the return value, 78*2b15cb3dSCy Schubert * so do not keep the resulting pointers around. 79*2b15cb3dSCy Schubert */ 80*2b15cb3dSCy Schubert extern const char * 81*2b15cb3dSCy Schubert format_time_fraction(time_t secs, long frac, int prec); 82*2b15cb3dSCy Schubert 83*2b15cb3dSCy Schubert #endif /* !defined(TIMETOA_H) */ 84