• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

how to get system time for MILLIseconds

I've used localtime to get the number of seconds past 1970.

Is there a C function to get MILLIseconds precision?
THANK!
0
happyloman
Asked:
happyloman
  • 7
  • 5
  • 2
  • +5
1 Solution
 
griesshCommented:
WHat OS are you using?
0
 
AxterCommented:
There is no ANSI C fuction, but there are OS and compiler specific functions.

Please state what OS and what compiler.
0
 
elcapitanCommented:
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
obgCommented:
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
 
makerpCommented:
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
 
obgCommented:
makerp and elcapitan: Are you sure that your time does not get quantized to 55 ms?
0
 
makerpCommented:
not acording to the documentation ,
0
 
obgCommented:
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
 
makerpCommented:
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
 
obgCommented:
Ok, that's interresting! What if you sleep for 1 ms?
0
 
obgCommented:
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
 
makerpCommented:
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
 
obgCommented:
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
 
makerpCommented:
not yet, i will
0
 
happylomanAuthor Commented:
HI everyone, the OS i am using is UNIX SOLARIS
0
 
happylomanAuthor Commented:
THe Compiler I am using is


/opt/SUNWspro/SC5.0/bin/cc
0
 
Chandra VConsultantCommented:
I think you can use


struct timeb tp;
ftime(&tp);

and acess ->  tp.millitm

 
0
 
obgCommented:
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
 
jimmy007Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 7
  • 5
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now