Microsecond time

From Tom's notes
Jump to navigation Jump to search

I needed this one on Solaris to display microseconds (µseconds):

time_ms.c

#include <sys/time.h>
main()
{
    struct timeval tv;
    gettimeofday(&tv, (void*)0);
    // Print time with milliseconds and decimal separator
    // printf("%d.%03d\n", tv.tv_sec, tv.tv_usec/1000);
    // Print time with microseconds without decimal separator
    printf("%d%06d\n", tv.tv_sec, tv.tv_usec);
}

Compile:

gcc time_ms.c -o time_ms

Credits: https://superuser.com/questions/178330/milliseconds-from-solaris-command-line