time in microseconds

Hi

I want to print the current local time in microseconds

How ?

struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
    tm->tm_sec, tv.tv_usec);

Thank you very much

i get these errors when i try to compile it. I must admit i do not know C.

OS=sol8
platform=E3500 (sparc)


$ cat milli.c

#include <stdlib.h>
#include <sys/time.h>

struct timeval tv;
struct timezone tz;
struct tm *tm;
gettimeofday(&tv, &tz);
tm=localtime(&tv.tv_sec);
printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min
,m->tm_sec, tv.tv_usec);


$ gcc milli.c
milli.c:7: parse error before '&' token
milli.c:7: warning: data definition has no type or storage class
milli.c:8: conflicting types for `tm'
milli.c:6: previous declaration of `tm'
milli.c:8: warning: initialization makes integer from pointer without a cast
milli.c:8: initializer element is not constant
milli.c:8: warning: data definition has no type or storage class
milli.c:9: parse error before string constant
milli.c:10: warning: conflicting types for built-in function `printf'
milli.c:10: warning: data definition has no type or storage class

Try this...

#include <stdlib.h>
#include <sys/time.h>
main()
{
     struct timeval tv;
     struct timezone tz;
     struct tm *tm;
     gettimeofday(&tv, &tz);
     tm=localtime(&tv.tv_sec);
     printf(" %d:%02d:%02d %d \n", tm->tm_hour, tm->tm_min,
              m->tm_sec, tv.tv_usec);
     exit(0);
}
1 Like

and once again you prove im not worthy!!

now 1 last question.

$ ./a.out
17:30:40 806214

$ date
Mon Jul 29 17:30:43 GMT 2002

what is the 806214???

That's the microseconds! :rolleyes: In your example the gettimeofday system call occurred 806,214 microseconds after 17:30:40.

my brain is not working today. alas another monday couldnt go any faster.

do you know how i can get it to display 10th and 100ths of a second also?

Change
tm->tm_sec, tv.tv_usec);
to be
tm->sec, tv.tv_usec/100000);
or
tm->sec, tv.tv_usec/10000);

and even a basterd like myself must bow down to the awsome powers of Perderabo

well folks the following code is just a complete listing of the code Perderabo wrote.

This will print out the 10ths 100ths microseconds (1,000,0000th) of a second.

IE:
$./a.out
9 99 999999

#include <stdlib.h>
#include <sys/time.h>
main()
{
     struct timeval tv;
     struct timezone tz;
     struct tm *tm;
     gettimeofday(&tv, &tz);
     tm=localtime(&tv.tv_sec);
printf("%d %d %d \n", tv.tv_usec/100000, tv.tv_usec/10000, tv.tv_usec);
     exit(0);
}

This is for you people that get compiling errors reguarding the TM variable. On some OSes (AIX 4.3. && 4.3.3 in my case) tm is not defined in the header so you can add this snipit to the top of the sys/time.h headerfile and then compile. (DISCLAIMER NOR I OR UNIX.COM TAKE ANY RESPONSABILITY FOR ANY OF THE CODE SHOWN IN THIS POST)

/* I added this to make the microseconds program work needed to define TM */

 #include <sys/types.h>
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>

 +#if !defined(TM_IN_SYS_TIME)
 +#include <time.h>
 +#endif

 #else
 #include <time.h>
 #endif

 /* ENTER YOUR NAME HERE added this on ENTER THE CURRENT DATE HERE */

Duh! :rolleyes:

:smiley:

I don't understand. I need to measure how long it takes something to run in microseconds. Like this:

start

RUN CODE

end

display number of microseconds that it took to "RUN CODE"

Please help.

Call gettimeofday() before and after the sequence in question. Then subtract the "before" value from the "after" value to get elapsed time. The only tricky part is that there are two integers (seconds and microseconds) to specify the time of day. So might need to "borrow" just like subtracting month and day to get days.

yeah try that...it should work.

Hi guys,

when I compile the above code to show time in microseconds, I get an error saying

"storage size of tz is not known".

Can anyone help me out. Sorry for the easy question...I am new to C.

The compiler doesn't know what size the structure 'timezone' is.

Have you included sys/time.h ?