网站首页 > 文章中心 > 其它

c语言计算日期差函数

作者:小编 更新时间:2023-10-25 18:07:19 浏览量:478人看过

C语言中如何计算时间差

#include stdio.h

#include stdlib.h

#include time.h

void main()

c语言计算日期差函数-图1

{

struct tm t1 = {0};

time_t _t1;

double diff;

t1.tm_year = time1[0] + 100;

t1.tm_mon = time1[1];

_t1 = _mkgmtime( t1 );

}

扩展资料:

C语言中有两个相关的函数用来计算时间差,分别是:

time_t time( time_t *t)? ?与 clock_t clock(void)

头文件: time.h

计算的时间单位分别为: s? ?, ms

time_t 和 clock_t 是函数库time.h 中定义的用来保存时间的数据结构

返回值:

所以我们可以根据具体情况需求,判断采用哪一个函数.

具体用法如下例子:

int main()

time_t c_start, t_start, c_end, t_end;

c_start = clock();? ? //! 单位为ms

t_start = time(NULL);? //! 单位为s

system("pause");

c_end? ?= clock();

t_end= time(NULL);

//!difftime(time_t, time_t)返回两个time_t变量间的时间间隔,即时间差

printf("The pause used %f ms by clock()\n",difftime(c_end,c_start));

printf("The pause used %f s by time()\n",difftime(t_end,t_start));

return 0;

所以呢,要计算某一函数块的占用时间时,只需要在执行该函数块之前和执行完该函数块之后调用同一个时间计算函数.再调用函数difftime()计算两者的差,即可得到耗费时间.

C语言中计算2个时间的差值的函数

#include?time.h

#include?stdio.h

time_t?_mktime(?char?*slTime?)?/**?yyyy-mm-dd?**/

struct?tm?tm_t;

int?year;

int?mon;

int?day;

tm_t.tm_mon?=?mon?-?1;

tm_t.tm_mday?=?day;

tm_t.tm_min?=?00;

tm_t.tm_sec?=?01;

tm_t.tm_wday?=?0;

tm_t.tm_yday?=?0;

tm_t.tm_isdst?=?0;

return?mktime(?tm_t?);

time_t?t1?=?_mktime(?date1?);

int?main()

printf(?"input?date1:?"?);

scanf(?"%s",?date1?);

fflush(?stdin?);

c语言 用库函数计算两日期相差的天数

#include?stdlib.h

#include?string.h

#include?math.h

int?get_days(const?char*?from,?const?char*?to);

time_t?convert(int?year,int?month,int?day);

int?days=get_days(from,to);

printf("From:%s\nTo:%s\n",from,to);

printf("%d\n",days);

return?0;

time_t?convert(int?year,int?month,int?day)

struct?tm?info={0};

info.tm_mon=month-1;

info.tm_mday=day;

return?mktime(info);

int?get_days(const?char*?from,?const?char*?to)

int?year,month,day,fromSecond,toSecond;

sscanf(from,"%d-%d-%d",year,month,day);

fromSecond=(int)convert(year,month,day);

sscanf(to,"%d-%d-%d",year,month,day);

toSecond=(int)convert(year,month,day);

Press?any?key?to?continue

这才算是用了库函数了---

以上就是土嘎嘎小编为大家整理的c语言计算日期差函数相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章