Solved

how to get system time for MILLIseconds

Posted on 2001-06-14
19
575 Views
Last Modified: 2007-12-19
I've used localtime to get the number of seconds past 1970.

Is there a C function to get MILLIseconds precision?
THANK!
0
Comment
Question by:happyloman
  • 7
  • 5
  • 2
  • +5
19 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 6193672
WHat OS are you using?
0
 
LVL 30

Expert Comment

by:Axter
ID: 6193807
There is no ANSI C fuction, but there are OS and compiler specific functions.

Please state what OS and what compiler.
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 6194012
for vc 6.0 use _ftime. here is small example from microsoft:

#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>

void main( void )
{
   struct _timeb timebuffer;
   char *timeline;

   _ftime( &timebuffer );
   timeline = ctime( & ( timebuffer.time ) );

   printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );
}

Output

The time is Tue Mar 21 15:26:41.341 1995



0
 
LVL 2

Expert Comment

by:obg
ID: 6194284
The HW PC timer initially ticks in 18.2 Hz, and I think it still does in Windoze. Therefore, each time reference has at least 55 ms quantization. There are a few ways to bypass this, depending on what you want to do (for timing purpose, you might be able to do the same thing like 1000 times, and if you want a delay function, you can read the timer's counter which counts in 1.19 MHz...), on what machine and OS you're working.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6194604
DWORD GetTickCount(VOID), gets the number of millis since windows was started

OR

VOID GetSystemTime( LPSYSTEMTIME lpSystemTime
 // address of system time structure
);

typedef struct _SYSTEMTIME {  // st
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME;
 
0
 
LVL 2

Expert Comment

by:obg
ID: 6194645
makerp and elcapitan: Are you sure that your time does not get quantized to 55 ms?
0
 
LVL 10

Expert Comment

by:makerp
ID: 6194697
not acording to the documentation ,
0
 
LVL 2

Expert Comment

by:obg
ID: 6194864
Do you mind trying to do a couple of sequential calls, and see what pops out? - Could be interresting to know... (Personally I am very lucky to have gotten out of Windoze programming.)
0
 
LVL 10

Expert Comment

by:makerp
ID: 6194921
windows programmin aint all bad, i wrote a load sharing system on windows, it was quite good

for(int i=0;i<10;i++)
{
   printf("GetTickCount() %d\n",GetTickCount());
   Sleep(100L);
}
Sleep(10000L);

100 mills apart on the dot. i shut all webservers/ftp and smtp services down etc and run it
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Expert Comment

by:obg
ID: 6194960
Ok, that's interresting! What if you sleep for 1 ms?
0
 
LVL 2

Expert Comment

by:obg
ID: 6194971
And you're right, it is not ALL bad. There are usually 100 ways to solve a problem where 99 does not work. - You just need some luck...
0
 
LVL 10

Expert Comment

by:makerp
ID: 6194983
programming is programming, i dont belive in having favourites as i belive this aint good, its all about fitness for purpose, windows is good for somethings and unix others, like languages are good for different things, for example you would not write a web server in Windows Scripting Host, although some crazy fool might
0
 
LVL 2

Expert Comment

by:obg
ID: 6195046
I don't believe in having favourites either. I just don't like crap, and I still have not found one area where windows would be my best choice, except for playing games.

Did you try 1 ms?
0
 
LVL 10

Expert Comment

by:makerp
ID: 6195100
not yet, i will
0
 

Author Comment

by:happyloman
ID: 6195508
HI everyone, the OS i am using is UNIX SOLARIS
0
 

Author Comment

by:happyloman
ID: 6195510
THe Compiler I am using is


/opt/SUNWspro/SC5.0/bin/cc
0
 
LVL 2

Expert Comment

by:Chandra V
ID: 6197520
I think you can use


struct timeb tp;
ftime(&tp);

and acess ->  tp.millitm

 
0
 
LVL 2

Expert Comment

by:obg
ID: 6198034
Then, the easiest way is to use the clock() function:

#include <time.h>
.
.
.
   clock_t t0, t1;
   t0 = clock()
  /* do some stuff... */
  t1 = clock();
  printf("Time: %d ms\n", (t1 - t0) / (CLOCKS_PER_SEC / 1000));

If you really need the time of day in ms precision, use ftime as already suggested.
0
 
LVL 1

Accepted Solution

by:
jimmy007 earned 200 total points
ID: 6200724
Under Unix, you have gettimeofday()

#include <sys/time.h>

struct timeval current;

if(gettimeofday(&current, NULL) != 0)
   printf("Error\n");

current.tv_sec --> give you seconds since 1970
current.tv_usec --> give you remaining microseconds


0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now