c gettimeofday example
#include <stdio.h>
#include <sys/time.h>
int main() {
struct timeval tv;
gettimeofday(&tv, NULL);
printf("Seconds since January 1, 1970: %ld\n", tv.tv_sec);
printf("Microseconds: %ld\n", tv.tv_usec);
return 0;
}
This is an example of using the gettimeofday
function in C to get the current time. The gettimeofday
function is defined in the sys/time.h
header file. It takes two arguments: a pointer to a struct timeval
variable and a pointer to a struct timezone
variable. In this example, we pass NULL
as the second argument because we don't need to get the timezone information.
The struct timeval
structure has two members: tv_sec
and tv_usec
. tv_sec
represents the number of seconds since January 1, 1970, and tv_usec
represents the number of microseconds. We use these members to print the current time in seconds and microseconds.
Note that the gettimeofday
function returns 0 on success and -1 on failure. In this example, we don't check the return value for simplicity.
Please let me know if you need further assistance!