/*************************************************************************************** |
建立头文件"date.h" |
****************************************************************************************/ |
#ifndef __DATE_H |
#define __DATE_H |
#include "stm8_macro.h" |
#include "ds1302.h" |
long DaysApart(u16 year1, u8 month1, u8 day1, u16 year2, u8 month2, u8 day2); |
u8 GetDayofWeek(u16 year, u8 month, u8 day); |
void DateStep(u16* year, u8* month, u8* day); |
void DateBack(u16* year, u8* month, u8* day); |
#endif /* __TIME_DATE_H */ |
/*************************************************************************************** |
建立源文件"date.c" |
****************************************************************************************/ |
#include "date.h" |
/* |
@Function : 是否是闰年 |
@Input : year 年份 |
@Output : 1 闰年 |
0 平年 |
**/ |
static u8 IsLeapYear(u16 year) |
{ |
if ( (year%100 == 0 && year%400 == 0) || |
(year%100 != 0 && year%4 == 0) ) |
{ |
return 1; |
} |
else |
return 0; |
} |
/* |
@Function : 计算某个月份的天数 |
@Input : year 年份 |
month 月份 |
@Output : 天数 |
**/ |
static u8 GetDaysInMonth(u16 year, u8 month) |
{ |
u8 days = 0; |
switch (month) |
{ |
case 2: |
if (IsLeapYear(year)) |
days = 29; |
else |
days = 28; |
break ; |
case 4: |
case 6: |
case 9: |
case 11: |
days = 30; |
break ; |
default : |
days = 31; |
break ; |
} |
return days; |
} |
/* |
@Function: 计算两个日期相隔天数,年份 |
@Input : year1,month1,day1 日期1 |
year2, month2, day2 日期2 |
@Output : 0xffff: 出错 |
other : 两个日期相隔的天数,日期2 - 日期1 |
< 0 : 表示日期1 > 日期2 |
**/ |
long DaysApart( |
u16 year1, u8 month1, u8 day1, |
u16 year2, u8 month2, u8 day2) |
{ |
u16 i; |
long res_days = 0; |
//日期格式错误,error |
if ( month1 >12 || month2 >12 || |
day1 >31 || day2 >31 || |
!year1 || !year2 || |
!month1 || !month2 || |
!day1 || !day2 ) |
{ |
return 0xffff; |
} |
//日期1比日期2后 |
if ( ( year1 > year2) || |
((year1 == year2) && (month1 > month2)) || |
((year1 == year2) && (month1 == month2) && (day1 > day2))) |
{ |
res_days = -1 * DaysApart(year2, month2, day2, |
year1, month1, day1); |
return res_days; |
} |
//天数计算 |
if (year1 != year2) //不同年份 |
{ |
//第一年的天数 |
res_days = GetDaysInMonth(year1, month1) - day1; |
for (i=month1+1; i<=12; i++) |
{ |
res_days += GetDaysInMonth(year1, (u8)i); |
} |
//最后一年的天数 |
for (i=1; i<month2; i++) |
{ |
res_days += GetDaysInMonth(year2, (u8)i); |
} |
res_days += day2; |
//中间年份天数 |
for (i=year1+1; i<year2; i++) |
{ |
if (IsLeapYear((u16)i)) |
res_days += 366; |
else |
res_days += 365; |
} |
} |
else //相同年份 |
{ |
if (month1 != month2) //不同月份 |
{ |
res_days = GetDaysInMonth(year1, month1) - day1 + day2; |
for (i=month1+1; i<month2; i++) |
{ |
res_days += GetDaysInMonth(year1, (u8)i); |
} |
} |
else //相同月份 |
{ |
res_days = day2 - day1; |
} |
} |
return res_days; |
} |
/* |
@Function: 获取星期几 |
@Input : year month day,日期须大于公元1世纪.01.01 |
@Output : 星期 1~7 |
**/ |
u8 GetDayofWeek(u16 year, u8 month, u8 day) |
{ |
long days = 0; |
u8 dayofweek; |
days = DaysApart(2001, 1, 1, //2001.01.01,星期一,以此作参照 |
year, month, day); |
if (days >= 0) |
{ |
dayofweek = (u8)((days + 1)%7); |
if (dayofweek == 0) |
dayofweek = 7; |
} |
else |
{ |
dayofweek = (u8)((-1*days)%7); |
dayofweek = (u8)((dayofweek == 0)?(1):((8-dayofweek))); |
} |
return dayofweek; |
} |
/* |
@Function: 日期步进,日期前进一天 |
@Input : year month day |
@Output : None |
**/ |
void DateStep(u16* year, u8* month, u8* day) |
{ |
u8 daysofMonth; |
daysofMonth = GetDaysInMonth(*year, *month); |
if (++(*day) > daysofMonth) |
{ |
*day = 1; |
if (++(*month) > 12) |
{ |
*month = 1; |
++(*year); |
} |
} |
} |
/* |
@Function: 日期步进,日期倒退一天 |
@Input : year month day |
@Output : None |
**/ |
void DateBack(u16* year, u8* month, u8* day) |
{ |
if (--(*day) == 0) |
{ |
if (--(*month) == 0) |
{ |
*month = 12; |
--(*year); |
} |
*day = GetDaysInMonth(*year, *month); |
} |
} |
初级程序员
by: 永远的希望 发表于:2016-07-06 14:24:36 顶(0) | 踩(0) 回复
谢谢分享
回复评论