Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Oct 2005 09:28:46 -0700
From:      Brooks Davis <brooks@one-eyed-alien.net>
To:        Andreas Kohn <andreas.kohn@gmx.net>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: struct timeval: why is tv_sec long?
Message-ID:  <20051007162846.GB12691@odin.ac.hmc.edu>
In-Reply-To: <1128644542.1273.36.camel@klamath.syndrom23.de>
References:  <1128644542.1273.36.camel@klamath.syndrom23.de>

next in thread | previous in thread | raw e-mail | index | archive | help

--eAbsdosE1cNLO4uF
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Oct 07, 2005 at 02:22:22AM +0200, Andreas Kohn wrote:
> Hi,
>=20
> is there any special reason for timeval.tv_sec being long?

tv_sec is presumably long becuase that way 64-bit platforms end up with
timevals that don't suffer from the 2038 bug.  time_t is also long (or
rather synonimous with it) on all but alpha platforms.

> --- sys/_timeval.h
> /*
>  * Structure returned by gettimeofday(2) system call, and used in other
> calls.
>  */
> struct timeval {
>         long            tv_sec;         /* seconds (XXX should be
> time_t) */
>         suseconds_t     tv_usec;        /* and microseconds */
> };
> ---
>=20
> I just stumbled across this code in an application,
> ---
> timeval tv;
> gettimeofday(&tv, 0);
>=20
> char tbuf[64];
> struct tm tmp;
> strftime(tbuf, sizeof(tbuf), "%b %d %H:%M:%S", localtime_r(&tv.tv_sec,
> &tmp));
> ---
>=20
> and this fails to compile on FreeBSD. I fixed the application code now,
> but I do wonder why that XXX in sys/_timeval.h is there.
> _timeval.h came into existence on 31-Dec-2002, before that timeval was
> defined in sys/time.h with both tv_sec and tv_usec as long.
>=20
> Could anyone offer any pointers?

You probably can just cast the tv.tv_sec to a time_t.  Alternativly, you
could allocate a time_t and assing tv_sec to it since that will work on
alpha where the other won't.

-- Brooks

--=20
Any statement of the form "X is the one, true Y" is FALSE.
PGP fingerprint 655D 519C 26A7 82E7 2529  9BF0 5D8E 8BE9 F238 1AD4

--eAbsdosE1cNLO4uF
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFDRqI+XY6L6fI4GtQRAqcVAKCjfDPoGncaMM7SzHu3RnvR8veoggCfV0aS
YYWjw4cY5oz368twFhTMKr8=
=fgr2
-----END PGP SIGNATURE-----

--eAbsdosE1cNLO4uF--



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