difftime()、difftime64() 時刻差の計算

標準

標準/拡張機能 C/C++ 依存項目

ISO C
XPG4
XPG4.2
C99
Single UNIX Specification、バージョン 3
Language Environment®

両方  

形式

difftime() 関数の形式:
#include <time.h>

double difftime(time64_t time2, time_t time1);
difftime64() 関数の形式:
#define _LARGE_TIME_API
#include <time.h>

double difftime64(time64_t time2, time64_t time1);

機能説明

time() によって戻されるカレンダー時間 の time2time1 の差 (秒数) を計算します。

difftime() 関数は、2 つのカレンダー時間の差を double 型で戻します。戻り値は、difftime() を呼び出すスレッドの浮動小数点モードに 応じて、16 進数浮動小数点形式または IEEE 2 進数浮動小数点形式のどちらかに なります。difftime() 関数は、__isBFP() を使用して、どちらの浮動小数点形式 (16 進数浮動小数点または IEEE 2 進数浮動小数点) を呼び出しスレッドに戻す かを判別します。

関数 difftime64() は、difftime() とまったく同じように動作しますが、2038 年 1 月 19 日の 03:14:07 UTC を超え、9999 年 12 月 31 日の 23:59:59 UTC までを限度とするカレンダー時間をサポートできます。

戻り値

time1 から time2 への 経過時間 (秒数) を double として戻します。

CELEBD04
⁄* CELEBD04                                      

   This example shows a timing application using &diff..                        
   The example calculates how long, on average, it takes a                      
   user to input some data to the program.                                      
                                                                                
 *⁄                                                                             
#include <time.h>                                                               
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   time_t start, finish;                                                        
   int i, n, num;                                                               
   int answer;                                                                  
                                                                                
   printf("11 x 55 = ? Enter your answer below¥n");                             
   time(&start);                                                                
   scanf("%d",&answer);                                                         
   time(&finish);                                                               
   printf("You answered %s in %.0f seconds.¥n",                                 
           answer == 605 ? "correctly" : "incorrectly",                         
           difftime(finish,start));                                             
}                                                                               
出力:
 11 x 55 = ? Enter your answer below
 605
 You answered correctly in 20 seconds

関連情報