将指定日期转换成秒数

头文件:

需要数据类型: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
#include
 
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" );
}

你可能感兴趣的:(将指定日期转换成秒数)