#include <stdio.h>
typedef struct Time{
int Y,M,D,h,m,s;
}Time;
Time time1 = {20,7,28,5,30,10};
Time time2 = {20,7,29,6,10,10};
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*函数名称:Day_Diff
*函数功能:计算任意日期的相隔天数
*输入参数:time1 结构体指针 , time2结构体指针
*返回参数:天数
* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
int Day_Diff(Time *time1 , Time *time2)
{
int year2 , month2 , day2;
int year1 , month1 , day1;
month1 = (time1->M + 9)%12;
year1 = time1->Y - month1/10;
day1 = 365 * year1 + year1 / 4 - year1 / 100 + year1 / 400 + (month1 *306 + 5) / 10 + (time1->D - 1);
month2 = (time2->M + 9) % 12;
year2 = time2->Y - month2 / 10;
day2 = 365 * year2 + year2 / 4 - year2 / 100 + year2 / 400 + (month2 *306 + 5) / 10 + (time2->D - 1);
return (day2 - day1);
}
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*函数名称:TimeSub
*函数功能:计算任意两个时间点相隔分钟数
*输入参数:Day 天数 , time1 结构体指针 , time2结构体指针
*返回参数:
* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
int TimeSub(int Day , Time *time1,Time *time2)
{
long t1,t2,t;
t1 = (time1->h * 3600) + (time1->m * 60) + (time1 -> s);
t2 = (Day * 24 *3600) + (time2->h * 3600) + (time2->m *60) + (time2 -> s);
t = (t2 - t1) /60;
return t;
}
int main()
{
int num = 0;
int day = 0;
day = Day_Diff(&time1 , &time2);
printf("day = %d\r\n",day);
num = TimeSub(day,&time1 , &time2);
printf("%d\r\n" , num);
return 0;
}
|