Namespaces
Variants

difftime

From cppreference.net
헤더 파일에 정의됨 <time.h>
double difftime ( time_t time_end, time_t time_beg ) ;

두 캘린더 시간 사이의 차이를 time_t 객체( time_end - time_beg )로 초 단위로 계산합니다. time_end time_beg 이전의 시간을 가리키면 결과는 음수입니다.

목차

매개변수

time_beg, time_end - 비교할 시간

반환값

두 시간 간의 차이(초 단위).

참고 사항

POSIX 시스템에서, time_t 는 초 단위로 측정되며, difftime 는 산술적 뺄셈과 동일합니다. 그러나 C와 C++는 time_t 에 대해 소수 단위를 허용합니다.

예제

다음 프로그램은 해당 월의 시작 이후 경과된 초 수를 계산합니다.

#include <stdio.h>
#include <time.h>
int main(void)
{
    time_t now = time(0);
    struct tm 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);
    return 0;
}

출력:

1937968 seconds have passed since the beginning of the month.

참고문헌

  • C17 표준 (ISO/IEC 9899:2018):
  • 7.27.2.2 difftime 함수 (p: 285)
  • C11 표준 (ISO/IEC 9899:2011):
  • 7.27.2.2 difftime 함수 (p: 390)
  • C99 표준 (ISO/IEC 9899:1999):
  • 7.23.2.2 difftime 함수 (p: 338)
  • C89/C90 표준 (ISO/IEC 9899:1990):
  • 7.12.2.2 difftime 함수 (p: 171)

참고 항목

C++ documentation for difftime