C语言学习笔记---时间函数mktime()和difftime()

本文详细介绍了C语言中用于时间处理的两个关键函数:mktime和difftime。mktime函数将结构体tm转换为自1970年以来的秒数,而difftime函数则计算两个时间点之间的差值(以秒为单位)。通过示例代码展示了如何使用这两个函数,以及它们在实际应用中的效果。文章还提供了计算代码运行时间的示例,验证了difftime函数的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  这两个函数原型如下:

	__CRT_INLINE time_t __cdecl mktime(struct tm *_Tm);
	__CRT_INLINE double __cdecl difftime(time_t _Time1,time_t _Time2);

mktime函数

  mktime函数会把参数把 timeptr 所指向的结构转换为自 1970 年 1 月 1 日以来持续时间的秒数,如果发生错误时则返回-1。

  参数结构体原型如下:

struct tm {
   int tm_sec;         /* 秒,范围从 0 到 59                */
   int tm_min;         /* 分,范围从 0 到 59                */
   int tm_hour;        /* 小时,范围从 0 到 23              */
   int tm_mday;        /* 一月中的第几天,范围从 1 到 31  */
   int tm_mon;         /* 月份,范围从 0 到 11              */
   int tm_year;        /* 自 1900 起的年数                    */
   int tm_wday;        /* 一周中的第几天,范围从 0 到 6    */
   int tm_yday;        /* 一年中的第几天,范围从 0 到 365 */
   int tm_isdst;       /* 夏令时                        */    
};

  下面直接通过一段代码来演示。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char** argv)
{
    int ret,time_cnt;
    struct tm info;

    info.tm_year = 2022 - 1900;
    info.tm_mon = 1 - 1;
    info.tm_mday = 25;
    info.tm_hour = 11;
    info.tm_min = 28;
    info.tm_sec = 50;
    info.tm_isdst = -1;

    ret = mktime(&info);
    
    time_cnt = time(NULL);
    if( ret == -1 ) {
        printf("Error: unable to make time using mktime\n");
    } else {
        printf("%d %d",ret,time_cnt);
    }
    return 0;
}

  首先定义时间结构体,然后给结构体中的变量赋值,将当前时间值赋给变量,然后在通过time函数获取当前时间的秒数,最后将mktime函数转换后的秒数和time函数返回的秒数打印出来。

image.png

  通过结果可以看到两个函数的秒数相差1,这是由于程序在编译执行的时候延时了一秒,说明mktime函数转换后的秒数和time函数返回的秒数是一样的。

difftime函数

  difftime函数有两个时间参数,这个函数的主要作用返回这两个时间就参数的差,也就是这两个时间值相差的秒数。

  一般可以通过这个函数来计算某段代码运行的时间。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char** argv)
{
	time_t start_t, end_t;
	double diff_t;

	printf("程序启动...\n");
	time(&start_t);

	sleep(2);

	printf("运行结束!\n");
	time(&end_t);
	diff_t = difftime(end_t, start_t);

	printf("\n开始时间: %d 结束时间: %d 代码运行时间: %fs\n", start_t,end_t,diff_t);

	return 0;
}

  定义两个变量来记录程序运行前的时间值和程序运行后的时间值,然后通过延时函数来模拟程序的运行过程,最后通过difftime函数来计算函数运行的时长。

image.png

  通过打印的结果可看出,延时函数的执行时间为2s,程序中的延时也是2s,说明函数计算的结果是正确的。在这里要注意一点difftime函数的返回值是double类型的数据。

static __time64_t __cdecl _make__time64_t ( struct tm *tb, int ultflag ) { __time64_t tmptm1, tmptm2, tmptm3; struct tm tbtemp; long dstbias = 0; long timezone = 0; _VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __time64_t )( -1 ) ) ) /* * First, make sure tm_year is reasonably close to being in range. */ if ( ((tmptm1 = tb->tm_year) _MAX_YEAR64 + 1) ) goto err_mktime; /* * Adjust month value so it is in the range 0 - 11. This is because * we don't know how many days are in months 12, 13, 14, etc. */ if ( (tb->tm_mon tm_mon > 11) ) { tmptm1 += (tb->tm_mon / 12); if ( (tb->tm_mon %= 12) tm_mon += 12; tmptm1--; } /* * Make sure year count is still in range. */ if ( (tmptm1 _MAX_YEAR64 + 1) ) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed years *****/ /* * Calculate days elapsed minus one, in the given year, to the given * month. Check for leap year and adjust if necessary. */ tmptm2 = _days[tb->tm_mon]; if ( _IS_LEAP_YEAR(tmptm1) && (tb->tm_mon > 1) ) tmptm2++; /* * Calculate elapsed days since base date (midnight, 1/1/70, UTC) * * * 365 days for each elapsed year since 1970, plus one more day for * each elapsed leap year. no danger of overflow because of the range * check (above) on tmptm1. */ tmptm3 = (tmptm1 - _BASE_YEAR) * 365 + _ELAPSED_LEAP_YEARS(tmptm1); /* * elapsed days to current month (still no possible overflow) */ tmptm3 += tmptm2; /* * elapsed days to current date. */ tmptm1 = tmptm3 + (tmptm2 = (__time64_t)(tb->tm_mday)); /***** HERE: tmptm1 holds number of elapsed days *****/ /* * Calculate elapsed hours since base date */ tmptm2 = tmptm1 * 24; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_hour); /***** HERE: tmptm1 holds number of elapsed hours *****/ /* * Calculate elapsed minutes since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_min); /***** HERE: tmptm1 holds number of elapsed minutes *****/ /* * Calculate elapsed seconds since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_sec); /***** HERE: tmptm1 holds number of elapsed seconds *****/ if ( ultflag ) { /* * Adjust for timezone. No need to check for overflow since * localtime() will check its arg value */ __tzset(); _ERRCHECK(_get_dstbias(&dstbias;)); _ERRCHECK(_get_timezone(&timezone;)); tmptm1 += timezone; /* * Convert this second count back into a time block structure. * If localtime returns NULL, return an error. */ if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; /* * Now must compensate for DST. The ANSI rules are to use the * passed-in tm_isdst flag if it is non-negative. Otherwise, * compute if DST applies. Recall that tbtemp has the time without * DST compensation, but has set tm_isdst correctly. */ if ( (tb->tm_isdst > 0) || ((tb->tm_isdst 0)) ) { tmptm1 += dstbias; if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; } } else { if ( _gmtime64_s(&tbtemp;, &tmptm1;) != 0) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/ /***** for local time if requested *****/ *tb = tbtemp; return tmptm1; err_mktime: /* * All errors come to here */ errno = EINVAL; return (__time64_t)(-1); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值