Solved

how to get system time for MILLIseconds

Posted on 2001-06-14
19
609 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adjust Mfcapp 29 178
Problem to scan all sheets 3 121
Using popen() and gunzip() to open file in HTTPServer 6 75
delete-remove 14 108
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

733 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