| Functions | ||||
| Time manipulation | ||||
difftime | ||||
(C11) | ||||
(C23) | ||||
| Format conversions | ||||
(deprecated in C23)(C11) | ||||
(deprecated in C23)(C11) | ||||
(C95) | ||||
(C23)(C11) | ||||
(C23)(C11) | ||||
| Constants | ||||
| Types | ||||
(C11) |
Defined in header <time.h> | ||
Computes difference between two calendar times astime_t objects (time_end- time_beg) in seconds. Iftime_end refers to time point beforetime_beg then the result is negative.
Contents |
| time_beg, time_end | - | times to compare |
Difference between two times in seconds.
On POSIX systems,time_t is measured in seconds, anddifftime is equivalent to arithmetic subtraction, but C and C++ allow fractional units fortime_t.
The following program computes the number of seconds that have passed since the beginning of the month.
#include <stdio.h>#include <time.h> int main(void){time_t now=time(0); structtm beg=*localtime(&now); // set beg to the beginning of the month beg.tm_hour=0, beg.tm_min=0, beg.tm_sec=0, beg.tm_mday=1; double seconds= difftime(now,mktime(&beg)); printf("%.f seconds have passed since the beginning of the month.\n", seconds); return0;}
Output:
1937968 seconds have passed since the beginning of the month.
C++ documentation fordifftime |