typedef struct
{
uint16 year; //年
uint8 month; //月
uint8 day; //日
uint8 hour; //时,24小时制
uint8 minute; //分
uint8 second; //秒
}mr_datetime;
static int16 SW2(uint8 month)
{
uint8 days;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days = 31;break;
case 4:
case 6:
case 9:
case 11:days=30;break;
case 2:days=28;break;
default:mrc_printf("Error!");break;
}
return days;
}
/**
* x,时间在后, 即验证时获得的时间
* y, 时间在前,即付费时记录的时间
* \return, 返回负数,出错,返回大于0的数表示间隔天数
*/
static int16 CalculateDays2(mr_datetime x, mr_datetime y)
{
int i;
int16 days = x.day;
days -= y.day;
if ((x.year == y.year) && (x.month == y.month) && (x.day == y.day))
return 0;
if (x.year > y.year)
{
if ((x.year % 400 == 0 || (x.year % 100 != 0 && x.year % 4 == 0)) && x.month > 2)
days++; // 闰年2月加1
if (x.year - y.year >= 1)
{
for (i = y.year; i < x.year; i++)
{
if (i % 400 == 0 || (i % 100 != 0 && i % 4 == 0))
days += 366;
else
days += 365;
}
}
if (x.month >= y.month)
{
for (i = y.month; i < x.month; i++)
days += SW2(i);
}
else
{
for (i = x.month; i < y.month; i++)
days -= SW2(i);
}
}
else if (x.year == y.year)
{
if ((x.year % 400 == 0 ||(x.year % 100 != 0 && x.year % 4 == 0)) && x.month > 2)
days++; // 闰年2月加1
if (x.month >= y.month)
{
for (i = y.month; i < x.month; i++)
days+=SW2(i);
}
else
{
mrc_printf("Error!");// 时间小于付费时,逻辑错误
return -1;
}
}
else
{
mrc_printf("Error!");// 时间小于付费时,逻辑错误
return -1;
}
return days;
}