这个是一个求时间间隔的函数,可能还不是很准确,因为是以秒数相除求解的。所以有待改进。。。
int getDay(int *diffday/*天数*/)
{
time_t rawtime;
struct tm nowdate, setdate;
long dftime;
long daysecond = 60*60*24;//一天的总秒数
// 获取当前时间
time( &rawtime );
nowdate = *localtime( &rawtime );
nowdate.tm_mon = nowdate.tm_mon+1;
printf("now year:%d, month:%d, day:%d\n", nowdate.tm_year, nowdate.tm_mon, nowdate.tm_mday);
// 给出指定时间
setdate = *localtime( &rawtime );
setdate.tm_hour = 0; setdate.tm_min = 0; setdate.tm_sec=0;
setdate.tm_mon = 1; setdate.tm_mday = 1; setdate.tm_year = 114;//2014-1900
printf("modify now year:%d, month:%d, day:%d\n", setdate.tm_year, setdate.tm_mon, setdate.tm_mday);
// 计算两个时间点之间的间隔秒数
dftime = difftime(mktime(&nowdate), mktime(&setdate));
printf("dftime=%ld, daysecond=%ld, result=%ld\n", dftime, daysecond, dftime / daysecond);
*diffday/*天数*/ = (int)floor(dftime / daysecond);//通过计算秒数来确定所给天数(总秒数/一天所含秒数=天数)
return 0;
}