retrieving system time

M

marcus

Is their a method in c++ or c that gives me the system time in
milliseconds or higher resolution? I need it to be platform
independent. Today I use timeGetTime which is windows specific.

thanks
 
C

Chris \( Val \)

| Is their a method in c++ or c that gives me the system time in
| milliseconds or higher resolution? I need it to be platform
| independent. Today I use timeGetTime which is windows specific.

The resolution is implementation dependant AFAIK.

Check out the following macros to determine what you can achieve:

CLK_TCK
CLOCKS_PER_SEC

Cheers,
Chris Val
 
C

Christian Meier

#include <ctime>

timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);

Refer to a online help for further information.

-Chris
 
V

Victor Bazarov

Christian said:
#include <ctime>

timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);

Refer to a online help for further information.

Doesn't compile on my machine. And online help doesn't help either.
What's 'timeval'? What's 'timezone'? What's 'gettimeofday'? I don't
see those in the Standard anywhere? Are they ANSI/ISO C++?

V
 
C

Christian Meier

Don't know if it is in the standard. If you don't know it, then it is not.
But perhaps it helps for marcus' problem.
It works at least on SUN Solaris, IBM AIX and Linux.
 
C

Chris Croughton

Is their a method in c++ or c that gives me the system time in
milliseconds or higher resolution? I need it to be platform
independent. Today I use timeGetTime which is windows specific.

There is nothing platform-independent because there is nothing in the
standard which says that a platform even has a timer at all. The time()
and clock() functions may return -1 to indicate that they aren't
implemented.

Best is to write a separate module of your own which you can then
implement specifically for particular platforms, the rest of your code
then doesn't need to bother about it.

Chris C
 
S

simont

Don't know if it is in the standard. If you don't know it, then it is
not. But perhaps it helps for marcus' problem. It works at least on
SUN Solaris, IBM AIX and Linux.

gettimeofday is part of the POSIX standard, but isn't part of the C or
C++ standards.

You might try a cross-platform library such as:
- boost (see http://www.boost.org/doc/html/date_time.html ),
- ACE (see http://www.cs.wustl.edu/~schmidt/ACE.html ), etc.

which should be able to use the right time API on each platform.

HTH,
Simon.
 
C

Chris Croughton

From
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_time.asp

"The time function returns the number of seconds elapsed since midnight
(00:00:00), January 1, 1970, coordinated universal time, according to
the system clock."

On a Microsoft system. Or on a POSIX one possibly (the POSIX standard
says that the time is returned in seconds "since the Epoch", which is
not necessarily 1970).
That sounds like what you're looking for. The time() function is
standard.

But what it returns isn't:

7.23.2.5 The time function

Synopsis
1 #include <time.h>
time_t time(time_t *timer);

Description
2 The time function determines the current calendar time. The encoding
of the value is unspecified.

time_t is an "arithmetic type capable of representing times", but there
is no guarantee that it is even linear (it could be bit fields in a
structure with year, month, day, hours, minutes, seconds and jiffies for
instance) or even an integer (a double precision number in Julian days
with a fractional component, for example).

For that matter, time(NULL) might return (time_t)-1 as a constant value
if the calendar time isn't available, even POSIX doesn't guarantee that
it is available.

Chris C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top