Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      difftime

      From cppreference.com
      <c‎ |chrono
       
       
      Date and time utilities
      Functions
      Time manipulation
      difftime
      Format conversions
      (deprecated in C23)(C11)
      (deprecated in C23)(C11)
      Constants
      Types
       
      Defined in header<time.h>
      double difftime(time_t time_end,time_t time_beg);

      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

      [edit]Parameters

      time_beg, time_end - times to compare

      [edit]Return value

      Difference between two times in seconds.

      [edit]Notes

      On POSIX systems,time_t is measured in seconds, anddifftime is equivalent to arithmetic subtraction, but C and C++ allow fractional units fortime_t.

      [edit]Example

      The following program computes the number of seconds that have passed since the beginning of the month.

      Run this code
      #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.

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.27.2.2 The difftime function (p: 285)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.27.2.2 The difftime function (p: 390)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.23.2.2 The difftime function (p: 338)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 7.12.2.2 The difftime function (p: 171)

      [edit]See also

      C++ documentation fordifftime
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/chrono/difftime&oldid=144873"

      [8]ページ先頭

      ©2009-2025 Movatter.jp