[LC++]gettimeofday and C++

Chris Vine chris at cvine.freeserve.co.uk
Sun Nov 11 05:08:45 UTC 2001


On Friday 09 November 2001 11:22 pm, Chris Vine wrote:
> On Friday 09 November 2001 6:15 am, Mark Phillips wrote:
> > Hi,
> >
> > I want to get access to the time from C++ (along with various other
> > system type things like detecting kill signals).  A little searching
> > and it seems the function "gettimeofday" is what I want.  In order that
> > I may use this function, I did:
> >
> > #include <ctime>
> >
> > but it couldn't find gettimeofday.  I changed it to:
> >
> > #include <sys/time.h>
> >
> > and it seems to work.  But this is not the preferred way of doing things
> > in C++.  It means things in sys/time.h are not in the std namespace ---
> > not a major problem, but it would be nice to do things the right way.
> >
> > Any ideas on why #include <ctime> wouldn't work?
>
> gettimeofday() is a Unix and not an ANSI-C function, so there isn't a
> Std-C++ ctime header file and it is not in std namespace.
>
> In fact, it is not even POSIX -- the POSIX function time() is in time.h,
> the BSD gettimeofday() is in sys/time.h in Linux/BSD systems.
>
> If you are going to use POSIX time(), look also at gmtime() and localtime()
> to make it useful (strftime() is also helpful).

Just to clarify my remarks about time(), gmtime() and localtime(), these are 
POSIX _and_ ANSI-C, so if the ctime header (rather than time.h) is included, 
they will be in std namespace.  gettimeofday() will not, for the reasons 
mentioned above.

Chris.



More information about the tuxCPProgramming mailing list