C Programming

Difftime() Function in C Language

Functions defined in the “time.h” header are used to determine, calculate, and convert the Unix times. The unit of time in this system is the second. To get the date data, a conversion must be done.

In this Linux Hint article, we will show you how to use the difftime() function to determine the difference between two Unix times. We will discuss the syntax of this function, the input and output arguments, and the data types used in each case. Then, we apply the function with some practical examples including code snippets in images.

Difftime() Function

Difftime() Function Syntax in C Language

double dif = difftime ( time_t t_2, time_t t_1);

Description of the Difftime() Function in C Language

The difftime() function calculates the time difference in seconds between t_1 and t_2 and returns the result in diff.

The time_t variable type is part of the “time” library and is defined in its header. Both time_t, clock_t and tm are typedef which is used by functions in this library to return their results.

The difftime() function is typically used to calculate the difference between a given time and the current Unix time. Then, we use the division math to find the difference of days, hours, minutes, etc.

The difftime() is defined in the “time.h” header. Before this function, we need to include it in our “.c” or “.h” file as follows:

#include <time.h>

Once we include the “time.h” header, we can use the difftime(), time(), and all functions that are defined in “time.h”.

How to Calculate the Time Difference Between Two Timestamps Using the Difftime() Function in the C Language

In this example, we calculate the number of seconds that elapsed since January 1, 2000 at 00:00:00 until this code is executed on your computer.

The first step is to include the libraries that we use and define the t_1 and t_2 variables of time_t type and double diff as shown in the following:

#include <stdio.h>

#include <time.h>

void main()
   {
double t_diff;
time_t t_1, t_2;
//…
   }

​Unix time is the seconds elapsed since 00:00:00 UTC on January 1, 1970. For the date and time which is January 1, 2000 at 00:00:00, this counter is at 946,695.600 seconds. This is the value of the t_2 variable.

The current Unix time is determined using the time() function. This function returns the current time in a variable of time_t type. In this case, it is t_1 which we defined previously.

In the following fragment, we assign the Unix time corresponding to January 1, 2000 at time 00:00:00 to t_1. We get the current Unix time in t_2 using the time() function and display the result in the command console using the printf() function.

#include <stdio.h>

#include <time.h>

void main()
   {
time_t t_1, t_2 =946695600;
    t_1 = time(NULL);
printf ("The Unix time is: %ld\n", t_1);
   }

The following image shows the execution of this fragment and the number of Unix seconds at t_1:

After we obtain the current time in the t_2 variable, we already have the two data that we need for the input arguments of the difftime() function.

The next step is to find the time difference between these two data. To do this, we call the difftime() function with t_1 and t_2 as input arguments and double diff as the output argument.

The following snippet shows the full code that calls this function and displays the difference between t_1 and t_2 in the command console:

#include <stdio.h>

#include <time.h>

void main()
   {
doublediff;
time_t t_1, t_2 =946695600;
    t_1 = time(NULL);
    diff = difftime(t_1, t_2);
printf ("The difference in seconds is: %li\n", ( long int ) diff);
   }

The following image shows the output of difftime(), the seconds difference between 00:00:00 UTC on January 1, 1970, and the time that this code is executed on the command line:

How to Convert the Unix Seconds to Datetime Format in the C Language

The ctime() function accepts the variables of time_t type in its input arguments and returns a string with the Unix time which is converted to datetime format.

Next, we use the code from the previous example and the ctime() and printf() functions to display the times of t_1 and t_2 which are converted to datetime.

#include <stdio.h>

#include <time.h>

void main()
   {
doublediff;
time_t t_1, t_2 =946695600;
    t_1 = time(NULL);
    diff = difftime(t_1, t_2);
printf ("The difference between the %s", ctime (&t_2));
printf ("and %s", ctime (&t_1));
printf ("is seconds %s\n", ( long int )diff);
   }

The following image shows the date and time format at t_1 and t_2:

Conclusion

In this Linux Hint article, we explained the step-by-step process on how to use difftime() to find the difference between two Unix times. We also briefly explained some of the additional functions of the “time” library that are needed to get this data and convert it to date and time format. We hope that you found this article useful. For more tips on the C language, use the search engine on our website.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).