difftime() Function
The difftime()
function in C time.h computes the difference in seconds between two time points. It calculates the elapsed time between an earlier and a later moment and returns the result as a floating-point number.
Syntax of difftime()
double difftime(time_t end, time_t beginning);
Parameters
Parameter | Description |
---|---|
end | The later time point in the interval whose length is calculated. |
beginning | The earlier time point in the interval. If this time is later than end , the result will be negative. |
The difftime()
function calculates the difference (end – beginning) in seconds and returns this value as a double
. The type time_t
is an arithmetic type that represents time values returned by the time()
function.
Return Value
The function returns the difference between the two time points in seconds as a double
. A negative value indicates that the beginning
time is later than the end
time.
Examples for difftime()
Example 1: Measuring Elapsed Time Using difftime()
This example demonstrates how to measure elapsed time by capturing the current time before and after a delay, then calculating the difference.
Program
#include <stdio.h>
#include <time.h>
int main(void) {
time_t start, end;
double elapsed;
// Capture the start time
start = time(NULL);
// Simulate a delay using a loop
for (volatile long i = 0; i < 100000000; i++);
// Capture the end time after the delay
end = time(NULL);
// Calculate the elapsed time in seconds
elapsed = difftime(end, start);
printf("Elapsed time: %.0f seconds\n", elapsed);
return 0;
}
Explanation:
- The program captures the current time before a simulated delay.
- A loop is used to create a time delay.
- The current time is captured again after the delay.
difftime()
computes the difference between the two captured times.- The elapsed time is printed to the console.
Program Output:
Elapsed time: 1 seconds
Example 2: Calculating a Fixed Time Interval
This example uses fixed Unix timestamp values to calculate the difference between two specific time points.
Program
#include <stdio.h>
#include <time.h>
int main(void) {
// Fixed time points represented as Unix timestamps
time_t beginning = 1609459200; // Jan 1, 2021 00:00:00 UTC
time_t end = 1609545600; // Jan 2, 2021 00:00:00 UTC
double diff = difftime(end, beginning);
printf("Difference: %.0f seconds\n", diff);
return 0;
}
Explanation:
- Two time points are defined using Unix timestamps.
difftime()
calculates the difference between these two time points.- The result, which represents one day (86400 seconds), is printed.
Program Output:
Difference: 86400 seconds
Example 3: Handling Negative Time Differences
This example shows the behavior of difftime()
when the beginning time is later than the end time, resulting in a negative value.
Program
#include <stdio.h>
#include <time.h>
int main(void) {
// Fixed time points where the beginning is later than the end
time_t beginning = 1609545600; // Jan 2, 2021 00:00:00 UTC
time_t end = 1609459200; // Jan 1, 2021 00:00:00 UTC
double diff = difftime(end, beginning);
printf("Difference: %.0f seconds\n", diff);
return 0;
}
Explanation:
- The time points are set such that the beginning time is later than the end time.
difftime()
computes the difference, resulting in a negative value.- The negative result is printed, indicating an inverse time interval.
Program Output:
Difference: -86400 seconds