将指定日期转换成秒数

头文件:<time.h>

需要数据类型:time_t,struct tm

需要函数:time_t mktime(struct tm * timeptr);

目标:将指定日期转换成距离1970.1.1所经过的秒数

过程:填写tm数据结构,传入函数中,返回一个time_t的结果。

下面是msdn关于mktime函数的描述:

 
time_t mktime(
   struct tm *timeptr 
);
__time32_t _mktime32(
   struct tm *timeptr 
);
__time64_t _mktime64(
   struct tm *timeptr 
);

 

_mktime32 returns the specified calendar time encoded as a value of type time_t. If timeptr references a date before midnight, January 1, 1970, or if the calendar time cannot be represented, _mktime32 returns –1 cast to type time_t. When using _mktime32 and if timeptr references a date after 03:14:07 January 19, 2038, Coordinated Universal Time (UTC), it will return –1 cast to type time_t.

_mktime64 will return –1 cast to type __time64_t if timeptr references a date after 23:59:59, December 31, 3000, UTC.

 
下面是一个实例:
#include <time.h>
#include <stdio.h>
 
int main( void ){
struct tm when;
__time64_t now, result; int days;
 char buff[80]; time( &now );
_localtime64_s( &when, &now );
asctime_s( buff, sizeof(buff), &when );
 printf( "Current time is %s\n", buff );
days = 20;
when.tm_mday = when.tm_mday + days;
 if( (result = mktime( &when )) != (time_t)-1 ) {
       asctime_s( buff, sizeof(buff), &when );
      printf( "In %d days the time will be %s\n", days, buff );
    } else perror( "mktime failed" );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值