Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Miliseconds in C 1

Status
Not open for further replies.

chrdesigner

Programmer
Apr 1, 2005
1
BR
Hello, my name is Chris and I'm an intermediate C programmer.
I would like to know how can I get the current time in miliseconds (prefeerable under Visual C++).
There are many fuctions in C for this task, but all of them get the time in hours, minutes and seconds.

Thanks in advance.
 
I tend to use <time.h> - its pure C, and will compile on Linux with GNU GCC, or with Visual C++ ...

Code:
include <time.h>

int start;
int time_taken_millis;

start = clock();

// do some stuff

time_taken_millis = (int)((clock()-start)*1E3/CLOCKS_PER_SEC);

--------------------------------------------------
Free Database Connection Pooling Software
 
VC++, Windows API, milliseconds from January 1, 1601:
Code:
long Millis()
{
 SYSTEMTIME	st;	/* unsigned wMilliseconds; */
 union _t
 {
   FILETIME	ft;
   __int64	 tlong; /* MS extension: 64-bit int */
 } t;
 GetSystemTime(&st);
 SystemTimeToFileTime(&st,&t.ft); /* in 100 nanosecs */
 return long(t.tlong/10000);
}
 
I'm sorry: (silent) integer overflow occurs in Millis() return expression. Use:
Code:
double Millis1970() /* From 1970-01-01T00:00:00 */
{
 SYSTEMTIME	st;	/* unsigned wMilliseconds; */
 union _t
 {
   FILETIME	ft;
   __int64	 tlong;
 } t;
 GetSystemTime(&st);
 SystemTimeToFileTime(&st,&t.ft);
 return (double)((t.tlong-116444736000000000i64)/10000);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top