C Programming

gettimeofday and settimeofday in C

“Many of us will know about the two functions related to the time in Linux operating system. They are gettimeofday () function and settimeofday () function. Both these functions help us to determine the actual time in the Linux platform. In today’s conversation, we will explore the characteristics and functional mechanisms of these functions.

At first, we will understand the gettimeofday () function.”

gettimeofday ()

The gettimeofday () function is a standard function whose definition is written in a header file in the C library named sys/time.h.

Syntax
The syntax of the gettimeofday () function is described below

Int gettimeofday (struct timeval * tm_vl, struct timezone * tm_zn)

gettimeofday () function takes two parameters as arguments inside its parenthesis. One of them is the tm_vl variable which contains timeval datatype, and another is the tm_zn variable which contains time zone datatype.

There are two standard data members that exist in the timeval structure. The first one is tv_sec, and another one is tv_usec. tv_sec represents the amount of time in seconds, and tv_usec represents the amount of time in a microsecond.

At present, the time zone structure is not used as it is diminished in its value over a period of time. For this, we pass NULL as a value of the time zone structure.

Programming Example 1
This is a program to get the time taken for a particular function to execute.

#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
float tm_com (struct timeval *str, struct timeval *lst)
{
    return (lst->tv_sec - str->tv_sec) + 1e-6*(lst->tv_usec - str->tv_usec);
}

void iteration(size_t count)
{
    for (int j = 0; j  %d) time taken: %0.8f sec\n",
           count1, tm_com(&str, &lst));


    gettimeofday (&str, NULL);
    iteration(count2);
    gettimeofday (&lst, NULL);

    printf ("
Iterator Function (loop count -> %d) time taken: %0.8f sec\n",
           count2, tm_com(&str, &lst));

    exit(EXIT_SUCCESS);
}

Output

Explanation
Inside the main () function, we will create two variables named “str” and “lst” of a structure named “timeval.” We will also create two variables named “count1” and “count2”.

Inside the gettimeofday () function, we will pass “str” and NULL. Then we will call a function named “iteration,” which definition is written above the main () function. Inside the iteration () function, we will pass the “count1”. Inside the gettimeofday () function, we will pass “lst” and NULL.

After that, from “lst” and “str,” we will get the execution time for function “iteration” for value “count1” as a first output.

The above process is done again for another bigger value passed through the variable “count2”.

That’s why we have observed that the execution time for “count2” is more than “count1”.

Programming Example 2
This program shows the return value of gettimeofday on success or failure.

#include <stdio.h>
#include <sys/time.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    struct timeval current;
    int ld;

    ld=gettimeofday(&current, NULL);
    if(ld==0) {
        printf(" This statement is executing . This means gettimeofday is successful. \n");
        printf(" Second : %lu \n Microsecond : %06lu\n",
                current.tv_sec, current.tv_usec);
    }
    else {
        printf("This statement is executing . This means gettimeofday is unsuccessful!! errno = %d\n",
                errno);
        return -1;
    }
    return 0;
}

Output

Explanation
Here we create a variable named “current” under the timeval structure and an integer type variable named “Id. Now we will call the gettimeofday () function, and we will pass two parameters as arguments. The two arguments are “current” and NULL. Whichever value will return this function; it will be assigned to the Id variable.

If the “Id” value is zero, the gettimeofday () function executes successfully and prints the time in second and microsecond; otherwise not execute successfully. 

settimeofday ()

The settimeofday () function is a standard function whose definition is mentioned in a header file in the C library named sys/time.h. This function helps us to make the clock time to the Universal Time Standard. 

Syntax 

The syntax of the settimeofday () function is described below.

int settimeofday (struct timeval * tm_vl, struct timezone * tm_zn)

settimeofday () function takes two parameters as arguments inside its parenthesis. One of them is tm_vl; it is a pointer of timeval structure that represents the time in seconds and microseconds. Another one is tm_zn. At present, the time zone structure is not used as it is backdated in its value over a period of time. For this, we pass NULL as a value of the time zone structure.

Returns
On successful execution of settimeofday () function it returns 0.

On unsuccessful execution of settimeofday () function it returns -1.

Programming Example 3
This function will show the return value on the successful execution of this function.

#include <sys/time.h>
#include <stdio.h>
#include <errno.h>

int main()
{
    struct timeval tm_vl;
    int sreturn;

    tm_vl.tv_sec= 1885151555;
    tm_vl.tv_usec= 0;

    sreturn=settimeofday(&tm_vl, NULL);

    if (sreturn== 0)
    {
        printf("settimeofday is executed without any error\n");
    }
    else {
        printf("settimeofday is unsuccessful "
        "errno = %d\n", errno);

        return -1;
    }

    return 0;
}

Output
Run without sudo and so settimeofday() is failed.

Run with sudo and so settimeofday() is successful.

Explanation
Here we create two variables named “tv_sec” and “tv_usec.” Both the variables are a member of the “timeval” structure and assign some value. Then we will call the settimeofday () function, and inside the function, we will pass The “tm_val” variable. Whichever value will return, this function will be assigned to the “sreturn” variable. If the “sreturn” variable returns 0, then this function has been executed successfully and gives its output; otherwise not executed successfully and prints the value of “errno” to a particular value (For this program, it’s 1).

Here at first, we have run the program without sudo, and that is why settimeofday () could not run successfully and returned -1.

For the second time, we have run the program with sudo, and settimeofday () is successful and returned 0.

Conclusion

In the earlier discussion, we have gotten a lot of information about the gettimeofday () function and settimeofday () function. These two functions are the most important functions in terms of the Linux platform in C language in the content of time. To get time as input and to set time to the system, these two functions help us a lot in configuring the system.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com