Overview #
The gettimeofday() function returns the current calendar time with microsecond precision.
Unlike time(), which returns time in seconds, gettimeofday() provides both seconds and microseconds, making it suitable for applications that require higher timing precision.
Header File #
#include <sys/time.h>
Man Pages #
man 2 gettimeofday
Function Prototype #
int gettimeofday(struct timeval *tv,
struct timezone *tz);
Parameters #
| Parameter | Description |
|---|---|
tv |
Pointer to a struct timeval object that receives the current time. |
tz |
Pointer to a struct timezone object. This parameter is obsolete and should always be NULL. |
Return Value #
On success, the function returns:
0
On failure, it returns:
-1
If an error occurs, errno is set appropriately.
Data Types #
struct timeval #
The current time is returned using a timeval structure.
struct timeval
{
time_t tv_sec;
suseconds_t tv_usec;
};
| Member | Description |
|---|---|
tv_sec |
Number of seconds elapsed since the Unix Epoch. |
tv_usec |
Number of microseconds elapsed within the current second. |
Examples #
Example 1 โ Get the Current Time #
main.c #
#include <stdio.h>
#include <sys/time.h>
int main(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1)
{
perror("gettimeofday");
return 1;
}
printf("Seconds : %ld\n", tv.tv_sec);
printf("Microseconds : %ld\n", tv.tv_usec);
return 0;
}
Output #
Seconds : 1753642158
Microseconds : 487523
The output will be different on your system.
Explanation #
struct timeval tv;
Declares a timeval structure to store the current time.
gettimeofday(&tv, NULL);
Retrieves the current calendar time.
The function stores the result in the tv structure.
printf("Seconds : %ld\n", tv.tv_sec);
Displays the number of seconds elapsed since the Unix Epoch.
printf("Microseconds : %ld\n", tv.tv_usec);
Displays the microsecond portion of the current second.
Example 2 โ Calculate Elapsed Time #
main.c #
#include <stdio.h>
#include <sys/time.h>
int main(void)
{
struct timeval start, end;
gettimeofday(&start, NULL);
for (volatile long i = 0; i < 100000000; i++);
gettimeofday(&end, NULL);
long elapsed_us =
(end.tv_sec - start.tv_sec) * 1000000L +
(end.tv_usec - start.tv_usec);
printf("Elapsed Time = %ld microseconds\n", elapsed_us);
return 0;
}
Output #
Elapsed Time = 184352 microseconds
The output will vary depending on your system.
Explanation #
gettimeofday(&start, NULL);
Stores the starting time.
gettimeofday(&end, NULL);
Stores the ending time.
(end.tv_sec - start.tv_sec) * 1000000L +
(end.tv_usec - start.tv_usec);
Calculates the elapsed time in microseconds.
Notes #
- Provides microsecond precision.
- Returns the current calendar time, not CPU execution time.
- The
tzparameter is obsolete and should always beNULL. - On failure, the function returns
-1and setserrno. - For new Linux applications,
clock_gettime()is generally preferred because it offers higher precision and supports multiple clocks.
Related Functions #
time()clock_gettime()clock()ctime()localtime()gmtime()