time_t printf()

Erik Trulsson ertr1013 at student.uu.se
Sun Jan 8 02:52:26 PST 2006

On Sun, Jan 08, 2006 at 09:34:02PM +1100, Edwin Groothuis wrote:
> Trying to printf() a time_t value, I tried the following permutations:
>     time_t days = 0, hours = 0, minutes = 0, seconds = 0;
>     sprintf(buffer, "%2d %2u %2ld %2lu", days, hours, minutes, seconds);
> 4.11 said:
>     misc.c:117: warning: int format, time_t arg (arg 3)
>     misc.c:117: warning: unsigned int format, long unsigned int arg (arg 4)
> 6.0 said:
>     misc.c:117: warning: long int format, time_t arg (arg 5)
>     misc.c:117: warning: long unsigned int format, time_t arg (arg 6)
> So between 4.11 and 6.0 there is no uniform way to printf() time_t
> values...

Yes, there is.  Cast the argument to [unsigned] long (or possibly
[u]intmax_t when using a C99 compiler) and print it as such.  Should be
portable to all Unix-like systems.

If you try to make the code as portable as possible you should keep in mind
that the only requirement the C standard puts on time_t is that it must be
an arithmetic type, so some implementations may define it as a double or a

<Insert your favourite quote here.>
Erik Trulsson
ertr1013 at student.uu.se

More information about the freebsd-ports mailing list