C++ Reference: Standard C++ Library reference: C Library: ctime: difftime

发布于:2022-10-31 ⋅ 阅读:(371) ⋅ 点赞:(0)

C++官网参考链接:https://cplusplus.com/reference/ctime/difftime/

函数 
<ctime>
difftime
double difftime (time_t end, time_t beginning);
返回两次时间的差值
计算beginning和end之间的秒差。

形参 
end 
计算时间间隔的长度的上界。
beginning
计算时间间隔的长度的下界。
如果描述的时间点晚于end,则结果为负。
time_t是一种基本算术类型(arithmetic type)的别名,它能够表示函数time返回的时间。

返回值
结果为(end-begin),以秒为单位,作为类型为double的浮点值。

用例
/* 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;

输出:

数据竞争
同时调用此函数是安全的,不会导致数据争用。

异常(C++) 
无抛出保证:此函数从不抛出异常。

另请参考
asctime    Convert tm structure to string (function)
gmtime    Convert time_t to tm as UTC time (function)
localtime    Convert time_t to tm as local time (function)
time    Get current time (function) 


网站公告

今日签到

点亮在社区的每一天
去签到