时间转秒函数c语言,c语言函数mktime()如何将时间转换成经过的秒数实例源码

c语言函数mktime()如何将时间转换成经过的秒数实例源码。

需要引入的头文件:#include

定义函数:time_t mktime(strcut tm * timeptr);

函数mktime()使用介绍:mktime()用来将参数timeptr 所指的tm 结构数据转换成从公元1970 年1 月1 日0 时0 分0 秒算起至今的UTC 时间所经过的秒数。

返回值:返回经过的秒数。

mktime()函数使用实例:用time()取得时间 (秒数), 利用localtime() 转换成struct tm 再利用mktine()将structtm 转换成原来的秒数。

#include

main(){

time_t timep;

strcut tm *p;

time(&timep);

printf(“time() : %d \n”, timep);

p = localtime(&timep);

timep = mktime(p);

printf(“time()->localtime()->mktime():%d\n”, timep);

}

执行结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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) < _BASE_YEAR - 1) || (tmptm1 > _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 < 0) || (tb->tm_mon > 11) ) { tmptm1 += (tb->tm_mon / 12); if ( (tb->tm_mon %= 12) < 0 ) { tb->tm_mon += 12; tmptm1--; } /* * Make sure year count is still in range. */ if ( (tmptm1 < _BASE_YEAR - 1) || (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) && (tbtemp.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); }
C语言实现UTC时间秒数转换成北京时间的方法如下: 1. 将UTC时间秒数转换为UTC时间结构体,可以使用time.h头文件中的gmtime()函数: ``` time_t utc_seconds = 1609459200; // UTC时间秒数 struct tm *utc_time = gmtime(&utc_seconds); // 转换为UTC时间结构体 ``` 2. 将UTC时间结构体转换为北京时间结构体。需要使用time.h头文件中的mktime()函数将UTC时间结构体转换为UTC时间秒数,再加上北京时间与UTC时间的时差8小时,得到北京时间秒数,最后再使用localtime()函数将北京时间秒数转换为北京时间结构体: ``` time_t utc_seconds = 1609459200; // UTC时间秒数 struct tm *utc_time = gmtime(&utc_seconds); // 转换为UTC时间结构体 time_t beijing_seconds = mktime(utc_time) + 8 * 3600; // UTC时间秒数转换为北京时间秒数 struct tm *beijing_time = localtime(&beijing_seconds); // 转换为北京时间结构体 ``` 3. 将北京时间结构体转换为字符串形式,可以使用strftime()函数,将北京时间结构体转换为指定格式的字符串: ``` char beijing_time_str[20]; strftime(beijing_time_str, 20, "%Y-%m-%d %H:%M:%S", beijing_time); // 格式化输出北京时间字符串 printf("Beijing time: %s\n", beijing_time_str); ``` 需要注意的是,在进行时间转换时,需要正确处理时区信息,否则可能会出现错误的时间结果。此外,以上方法仅适用于UTC时间与北京时间的时差为8小时的情况,如果时差不同,需要相应调整代码中的时差值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值