By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,375 Members | 1,428 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,375 IT Pros & Developers. It's quick & easy.

retrieving system time

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"marcus" <ma************@koping.net> wrote in message
news:1a**************************@posting.google.c om...
| 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
Jul 23 '05 #2

P: n/a
#include <ctime>

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

Refer to a online help for further information.

-Chris

"marcus" <ma************@koping.net> schrieb im Newsbeitrag
news:1a**************************@posting.google.c om...
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

Jul 23 '05 #3

P: n/a
Christian Meier wrote:
#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++?

-Chris

"marcus" <ma************@koping.net> schrieb im Newsbeitrag
news:1a**************************@posting.google.c om...
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



V
Jul 23 '05 #4

P: n/a
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.

"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:rG*****************@newsread1.mlpsca01.us.to. verio.net...
Christian Meier wrote:
#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++?

-Chris

"marcus" <ma************@koping.net> schrieb im Newsbeitrag
news:1a**************************@posting.google.c om...
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



V

Jul 23 '05 #5

P: n/a
On 19 Apr 2005 05:21:59 -0700, marcus
<ma************@koping.net> wrote:
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
Jul 23 '05 #6

P: n/a
Christian Meier wrote:
"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:rG*****************@newsread1.mlpsca01.us.to. verio.net...
Christian Meier wrote:
"marcus" <ma************@koping.net> schrieb im Newsbeitrag
news:1a**************************@posting.google.c om...

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. #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++?
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.

Jul 23 '05 #7

P: n/a
From
http://msdn.microsoft.com/library/de.../_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."

That sounds like what you're looking for. The time() function is
standard.

Jul 23 '05 #8

P: n/a
On 19 Apr 2005 11:55:33 -0700, jo******@gmail.com
<jo******@gmail.com> wrote:
From
http://msdn.microsoft.com/library/de.../_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
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.