Connecting Tech Pros Worldwide Help | Site Map

System time in C++

  #1  
Old July 22nd, 2005, 04:24 PM
Partho Choudhury
Guest
 
Posts: n/a
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! ]
  #2  
Old July 22nd, 2005, 04:24 PM
marbac
Guest
 
Posts: n/a

re: System time in C++


Partho Choudhury wrote:[color=blue]
> 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.[/color]



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 ("%d\n",n);
wait (1);
}
printf ("FIRE!!!\n");
return 0;
}

Regards marbac
  #3  
Old July 22nd, 2005, 04:24 PM
Victor Bazarov
Guest
 
Posts: n/a

re: System time in C++


Partho Choudhury wrote:[color=blue]
> 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.[/color]

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
  #4  
Old July 22nd, 2005, 04:26 PM
Evan Carew
Guest
 
Posts: n/a

re: System time in C++


-----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/...dex.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! ]
  #5  
Old July 22nd, 2005, 04:26 PM
Alan Johnson
Guest
 
Posts: n/a

re: System time in C++


Partho Choudhury wrote:[color=blue]
> 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.
>[/color]

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! ]
  #6  
Old July 22nd, 2005, 04:26 PM
Daniel Pfeffer
Guest
 
Posts: n/a

re: System time in C++


"Partho Choudhury" <partho@ieee.org> wrote in message
news:3dec3df8.0406280144.982c420@posting.google.co m...[color=blue]
> 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.[/color]

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! ]
  #7  
Old July 22nd, 2005, 04:28 PM
John Potter
Guest
 
Posts: n/a

re: System time in C++


On 29 Jun 2004 05:42:47 -0400, "Daniel Pfeffer" <pfefferd@hotmail.co.il>
wrote:
[color=blue]
> 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.[/color]

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! ]
  #8  
Old July 22nd, 2005, 04:28 PM
Brian McGuinness
Guest
 
Posts: n/a

re: System time in C++


partho@ieee.org (Partho Choudhury) wrote in message news:<3dec3df8.0406280144.982c420@posting.google.c om>...[color=blue]
> 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[/color]


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/...fs/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! ]
  #9  
Old July 22nd, 2005, 04:28 PM
Tommi Mäkitalo
Guest
 
Posts: n/a

re: System time in C++


....[color=blue]
>
> void wait ( int seconds )
> {
> clock_t endwait;
> endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
> while (clock() < endwait) {}
> }
>[/color]
....
Dont't do that! You eat the whole cpu for nothing.
  #10  
Old July 22nd, 2005, 04:28 PM
Julie
Guest
 
Posts: n/a

re: System time in C++


Tommi Mäkitalo wrote:[color=blue]
>
> ...[color=green]
> >
> > void wait ( int seconds )
> > {
> > clock_t endwait;
> > endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
> > while (clock() < endwait) {}
> > }
> >[/color]
> ...
> Dont't do that! You eat the whole cpu for nothing.[/color]

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

Yielding is all hardware/os/thread specific.
  #11  
Old July 22nd, 2005, 04:33 PM
Tommi Mäkitalo
Guest
 
Posts: n/a

re: System time in C++


Julie wrote:
[color=blue]
> Tommi Mäkitalo wrote:[color=green]
>>
>> ...[color=darkred]
>> >
>> > void wait ( int seconds )
>> > {
>> > clock_t endwait;
>> > endwait = clock () + seconds * CLK_TCK ; // or CLOCKS_PER_SEC
>> > while (clock() < endwait) {}
>> > }
>> >[/color]
>> ...
>> Dont't do that! You eat the whole cpu for nothing.[/color]
>
> In C++, there is *no* other way to do it.
>
> Yielding is all hardware/os/thread specific.[/color]
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
--


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to combine date and time in the database Luvin lunch answers 6 August 22nd, 2006 02:15 PM
System time in miliseconds Alex answers 3 March 3rd, 2006 01:45 PM
How do I get system time in VC++ in the .net development environme Kueishiong Tu answers 2 November 17th, 2005 02:41 PM