Home C&C++函数库 c++ 语法 程序源码 Linux C库

difftime

函数   <ctime>

double difftime (time_t end, time_t beginning);

时钟程序

返回两次之间的差值

计算开始和结束之间的秒差。

☲  参数


end
计算时间间隔的结束时间。

beginning
计算时间间隔的开始时间。 如果开始时间晚于结束时间,则结果为负。

time_t是基本算术类型的别名,能够表示由函数time返回的时间。

☉  返回值



以秒为单位的double类型浮点值的结果 (end-beginning)。

☣  示例



/* difftime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, difftime, time, mktime */

int main ()
{
  time_t now;
  struct tm newyear;
  double seconds;

  time(&now);  /* get current time; same as: now = time(NULL)  */

  newyear = *localtime(&now);

  newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;
  newyear.tm_mon = 0;  newyear.tm_mday = 1;

  seconds = difftime(now,mktime(&newyear));

  printf ("%.f seconds since new year in the current timezone.\n", seconds);

  return 0;
}

输出:
3777291 seconds since new year in the current timezone.

↭  数据竞争



并发地调用这个函数是安全的,不会导致数据竞争。

❆  异常(c++)



无抛出保证:此函数从不抛出异常。

🍄  另请参阅



time 获取当前时间(function)
asctime 转换tm结构为字符串(function )
gmtime 将time_t转换为tm作为UTC时间(function )
localtime 将time_t转换为tm作为当地时间(function )

联系我们 免责声明 关于CandCplus 网站地图