不知道能不能调用time.h文件里的时间函数,如果能,要怎么实现
------- 根据年月日时分秒构建struct tm结构,再通过mktime转化为time_t类型,这种类型保存的就是秒数,直接相减就得到了时间差
给你个我以前写的demo吧
程序代码:
#include <time.h>
#include <stdio.h>
void printtime( const struct tm* ptm )
{
printf( "%04d年%02d月%02d日 %02d时%02d分%02d秒\n"
, ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday
, ptm->tm_hour, ptm->tm_min, ptm->tm_sec );
printf( "本年第%03d天,星期%01d\n"
, ptm->tm_yday+1, ptm->tm_wday );
}
int main( void )
{
time_t t1 = time( 0 );
struct tm* tm1 = localtime( &t1 ); // gmtime
if( tm1 )
{
printtime( tm1 );
}
printf( "\n" );
const int year = 2008;
const int mon = 10;
const int day = 1;
const int hour = 0;
const int min = 0;
const int sec = 0;
struct tm tm2 = { sec,min,hour, day,mon-1,year-1900, 0,0,0 };
time_t t2 = mktime( &tm2 );
if( t2 != -1 )
{
struct tm* tm2 = localtime( &t2 );
printtime( tm2 );
}
return 0;
}