C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

System time in C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Partho Choudhury
Guest





PostPosted: Mon Jun 28, 2004 12:18 pm    Post subject: System time in C++ Reply with quote



Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Any help will be appreciated.

Partho

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
marbac
Guest





PostPosted: Mon Jun 28, 2004 12:40 pm    Post subject: Re: System time in C++ Reply with quote



Partho Choudhury wrote:
Quote:
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.



Hi,

In time.h there should be something like this:

/* This defines CLOCKS_PER_SEC, which is the number of processor clock
ticks per second. */
# include <bits/time.h>

/* This is the obsolete POSIX.1-1988 name for the same constant. */
# if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
# ifndef CLK_TCK
# define CLK_TCK CLOCKS_PER_SEC
# endif
# endif

with clock () you can get the clock ticks since start. As an example
(source:cplusplus.com):


/* clock example: countdown */
#include <stdio.h>
#include <time.h>

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
while (clock() < endwait) {}
}

int main ()
{
int n;
printf ("Starting countdown...n");
for (n=10; n>0; n--)
{
printf ("%dn",n);
wait (1);
}
printf ("FIRE!!!n");
return 0;
}

Regards marbac

Back to top
Victor Bazarov
Guest





PostPosted: Mon Jun 28, 2004 1:24 pm    Post subject: Re: System time in C++ Reply with quote



Partho Choudhury wrote:
Quote:
I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

So, your question is essentially this: I know that the language doesn't
have a mechanism to do A, but is there a way in the language to do A?

BTW, there is no standard symbol 'CTime'. Perhaps you're confusing C++
with MFC (which you cannot use for various reasons).

Repeat after me: there is no standard C++ way to access system time
except by using the functions in <ctime> (or <time.h>). If you need
a millisecond resolution (or better), you _have_ to use OS-specific
mechanisms if they are available.

V

Back to top
Evan Carew
Guest





PostPosted: Mon Jun 28, 2004 9:34 pm    Post subject: Re: System time in C++ Reply with quote

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Partho,

You could try using the boost::osix_time::ptime interface. it is quite
simple.

http://www.boost.org/libs/date_time/doc/index.html#usage
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFA4BbEoo/Prlj9GScRAkdvAJ455/WvFDB3qy3to8hvF9vnWcJZZACdFGMU
uzE86LIai4MH49d6+rdyhgs=
=HWtn
-----END PGP SIGNATURE-----

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alan Johnson
Guest





PostPosted: Tue Jun 29, 2004 9:37 am    Post subject: Re: System time in C++ Reply with quote

Partho Choudhury wrote:
Quote:
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Any help will be appreciated.


As others have said, this isn't possible. [Offtopic: A somewhat
portable way would be to use the POSIX ftime function, which I believe
exists on Windows as _ftime.]

Alan

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daniel Pfeffer
Guest





PostPosted: Tue Jun 29, 2004 9:42 am    Post subject: Re: System time in C++ Reply with quote

"Partho Choudhury" <partho (AT) ieee (DOT) org> wrote

Quote:
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Nothing in the Standard guarantees that _any_ clock is available. For
example, it is within the Standard to have clock() return (time_t)0 for all
calls. I agree that such an implementation would be a lousy one, but that is
a matter of quality of implementation, not the Standard.

Most C++ systems will provide a clock with resolution of 1 second, but
support of millisecond resolution is far from universal.

The CLOCKS_PER_SEC macro (defined in <time.h>) gives the number of ticks per
second, so you can give a compile-time message that the environment does not
meet the minimum requirements.

If you are willing to use other standards in addition to the C++ standard,
then IIRC - POSIX mandates a clock with millisecond resolution.


HTH
Daniel Pfeffer




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Tue Jun 29, 2004 7:05 pm    Post subject: Re: System time in C++ Reply with quote

On 29 Jun 2004 05:42:47 -0400, "Daniel Pfeffer" <pfefferd (AT) hotmail (DOT) co.il>
wrote:

Quote:
The CLOCKS_PER_SEC macro (defined in <time.h>) gives the number of ticks per
second, so you can give a compile-time message that the environment does not
meet the minimum requirements.

The macro only gives the unit of recording not the unit of change. It
is common to have CLOCKS_PER_SEC 1000000 indicating that the unit of
measurement is microseconds and actual changes in steps of 10000 giving
measurements in centiseconds. The macro tells nothing about accuracy.
It is only useful for converting the value to seconds.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Brian McGuinness
Guest





PostPosted: Tue Jun 29, 2004 7:22 pm    Post subject: Re: System time in C++ Reply with quote

[email]partho (AT) ieee (DOT) org[/email] (Partho Choudhury) wrote in message news:<3dec3df8.0406280144.982c420 (AT) posting (DOT) google.com>...
Quote:
Hi all:

I need to add a snippet which access the system time (upto atleast
milliseconds) using ANSI std. C++. I cannot use MFC and Win32 APIs in
my program for now for various reasons. Is there something like this
in ANSI C++ which allows access up to milliseconds. I know that time.h
and CTime allow access up to the second level, but I need to work in
the msec level.

Any help will be appreciated.

Partho


I don't know how widely supported it is, but you could try the
clock_gettime() function. This supports resolutions up to nanoseconds,
though of course this depends on the quality of the system clock. See
http://www.opengroup.org/onlinepubs/009695399/basedefs/time.h.html

I see this function defined in the Solaris 7 time.h file.

--- Brian

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tommi Mäkitalo
Guest





PostPosted: Tue Jun 29, 2004 9:24 pm    Post subject: Re: System time in C++ Reply with quote

....
Quote:

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
while (clock() < endwait) {}
}

....

Dont't do that! You eat the whole cpu for nothing.

Back to top
Julie
Guest





PostPosted: Tue Jun 29, 2004 9:59 pm    Post subject: Re: System time in C++ Reply with quote

Tommi Mäkitalo wrote:
Quote:

...

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
while (clock() < endwait) {}
}

...
Dont't do that! You eat the whole cpu for nothing.

In C++, there is *no* other way to do it.

Yielding is all hardware/os/thread specific.

Back to top
Tommi Mäkitalo
Guest





PostPosted: Wed Jun 30, 2004 7:03 am    Post subject: Re: System time in C++ Reply with quote

Julie wrote:

Quote:
Tommi Mäkitalo wrote:

...

void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
while (clock() < endwait) {}
}

...
Dont't do that! You eat the whole cpu for nothing.

In C++, there is *no* other way to do it.

Yielding is all hardware/os/thread specific.
Yes there are other ways to do it in c++. You can call system-specific

routines very easyly. ::sleep(unsigned int) conforms to POSIX as I read in
my man-page. It is just a little less portable than ::clock(), which is
part of ANSI-C.

Tommi
--



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.