Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 8 Jan 2006 11:52:23 +0100
From:      Erik Trulsson <ertr1013@student.uu.se>
To:        Edwin Groothuis <edwin@mavetju.org>
Cc:        freebsd-ports@freebsd.org
Subject:   Re: time_t printf()
Message-ID:  <20060108105223.GA19330@falcon.midgard.homeip.net>
In-Reply-To: <20060108103402.GB3806@k7.mavetju>
References:  <20060108103402.GB3806@k7.mavetju>

next in thread | previous in thread | raw e-mail | index | archive | help
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
float.


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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060108105223.GA19330>