#include "date.h"
/* 判断是不是是闰年,函数放回为1表示是闰年,函数返回为0为非闰年 */
UINT32 Is_Leap_Year(
UINT32 u32_year
)
{
UINT32 u32_Leap = 0u, u32_day = 0u;
/* 能被4整除并且不能被100整除,或能被400 */
if (((0u == (u32_year % 4u)) && ((u32_year % 100u) != 0u)) || (( u32_year % 400u) == 0u))
{
/* OSM_Printf是闰年 */
u32_Leap = 1u;
u32_day = 366u;
}
else
{
/* 不是闰年 */
u32_Leap = 0u;
u32_day = 365u;
}
return u32_Leap;
}
/* 2、输入1个日期如:20110415,判断是星期几 */
UINT32 Week(
UINT32 year_u32,
UINT32 mouth_u32,
UINT32 day_u32
)
{
UINT32 day_code = 0u;
UINT32 i32i = 0u;
UINT32 u32_j = 0u;
UINT32 year_flag = 0u;
UINT32 days = 0u;
UINT32 i32_sum = 0u;
UINT32 MOUTH[12] = {30u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u};
if ( (mouth_u32 < 1u) || (mouth_u32 > 12u) || (day_u32 > 31u) || (year_u32 < 1900u))
{
(void)OSM_Printf("输入有误的数据");
}
else
{
if ( 1900U == year_u32 )
{
u32_j = (days + day_u32) % 7u;
}
else
{
for ( i32i = 1900u ; i32i < year_u32; i32i++)
{
year_flag = Is_Leap_Year(i32i);
if ( 1u == year_flag)
{
days += 366u;
}
else
{
days += 365u;
}
}
for ( i32i = 0u; i32i < (mouth_u32 - 1u); i32i++)
{
days += MOUTH[i32i];
}
u32_j = (days + day_u32) % 7u;
}
switch(u32_j)
{
case 0u:
(void)OSM_Printf("星期日
");
break;
case 1u:
(void)OSM_Printf("星期1
");
break;
case 2u:
(void)OSM_Printf("星期2
");
break;
case 3u:
(void)OSM_Printf("星期3
");
break;
case 4u:
(void)OSM_Printf("星期4
");
break;
case 5u:
(void)OSM_Printf("星期5
");
break;
case 6u:
(void)OSM_Printf("星期6
");
break;
default:
break;
}
}
return 0u;
}
/* 3、两个指定的日期间相隔多少天,如:20110415距离19880906相隔多少天 */
/* 思路:分别求出两个日期距离1900年1月1日的天数,然后求出连个之间的差值 */
/* 求出1个日期1990年以后的包括1990年与1990年1月1日的距离 */
UINT32 Distance(
UINT32 year_u32,
UINT32 mouth_u32,
UINT32 day_u32
)
{
UINT32 distance_to_u32 = 0u;
UINT32 i32i = 0u;
UINT32 year_flag = 0u;
UINT32 MOUTH[12] = {30u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u};
if ( (mouth_u32 < 1u) || (mouth_u32 > 12u) || (day_u32 > 31u) || (year_u32 < 1900u))
{
(void)OSM_Printf("输入有误的数据");
}
else
{
if ( 1900u == year_u32 )
{
if ( 1u != mouth_u32 )
{
for ( i32i = 0u; i32i < (mouth_u32 - 1u); i32i++)
{
distance_to_u32 += MOUTH[i32i];
}
}
distance_to_u32 += day_u32;
}
else
{
for ( i32i = 1900u ; i32i < year_u32; i32i++)
{
year_flag = Is_Leap_Year(i32i);
if ( 1u == year_flag)
{
distance_to_u32 += 366u;
}
else
{
distance_to_u32 += 365u;
}
}
for ( i32i = 0u; i32i < (mouth_u32 - 1u); i32i++)
{
distance_to_u32 += MOUTH[i32i];
}
}
}
return distance_to_u32;
}
/* 求出两个日期之间的距离 */
UINT32 Absolue_distance(
UINT32 year1_u32,
UINT32 mouth1_u32,
UINT32 day1_u32,
UINT32 year2_u32,
UINT32 mouth2_u32,
UINT32 day2_u32
)
{
UINT32 absolue_distance = 0u;
UINT32 distance1 = 0u;
UINT32 distance2 = 0u;
distance1 = Distance(year1_u32, mouth1_u32, day1_u32);
distance2 = Distance(year2_u32, mouth2_u32, day2_u32);
if ( distance1 > distance2)
{
absolue_distance = distance1 - distance2;
}
else
{
absolue_distance = distance2 - distance1;
}
(void)OSM_Printf("%d",absolue_distance);
return absolue_distance;
}
/* 4、实现日期的格式化输出方式如YYYYMMDD, YYMMDD */
UINT32 Formatout1(
UINT32 year_u32,
UINT32 mouth_u32,
UINT32 day_u32
)
{
if((NULL != year_u32) || (NULL != mouth_u32) || (NULL != day_u32))
{
(void)OSM_Printf("YYYYMMDD的情势:%04d%02d%02d
",year_u32,mouth_u32,day_u32);
(void)OSM_Printf("YYMMDD的情势:%02d%02d%02d
",(year_u32 % 100U),mouth_u32,day_u32);
}
else
{
(void)OSM_Printf("输入有误");
}
return 0u;
}
主函数
#include "date.h"
UINT32 main(void)
{
UINT32 u32_year1 = 0u;
UINT32 u32_year2 = 0u;
UINT32 u32_mouth1 = 0u;
UINT32 u32_mouth2 = 0u;
UINT32 u32_day1 = 0u;
UINT32 u32_day2 = 0u;
UINT32 flag = 0u;
UINT32 distance_abs = 0u;
UINT32 key = 0u;
(void)OSM_Printf("~~~~~~~~~~~~~~~~~~实战4~~~~~~~~~~~~~~~~~~~~~
");
(void)OSM_Printf("~~~~~~~~~~~~~~~~~功能菜单~~~~~~~~~~~~~~~~~~~~
");
(void)OSM_Printf("~~~~~~~~~~~~~~~~~功能菜单~~~~~~~~~~~~~~~~~~~~
");
(void)OSM_Printf("~~~~~~~~~~~~~~~~~1、闰年判断~~~~~~~~~~~~~~~~~
");
(void)OSM_Printf("~~~~~~~~~~~~~~~~~2、星期判断~~~~~~~~~~~~~~~~~
");
(void)OSM_Printf("~~~~~~~~~~~~~~~~~3、日期间隔~~~~~~~~~~~~~~~~~
");
(void)OSM_Printf("~~~~~~~~~~~~~~~~~4、格式输出~~~~~~~~~~~~~~~~~
");
(void)OSM_Printf("~~~~~~~~~~~~~~~~~输入相应的数字~~~~~~~~~~~~~~
");
(void)OSM_Scanf("%d", &key);
if ( key < 1 || key > 4)
{
(void)OSM_Printf("输入毛病了!");
}
switch (key)
{
case 1u:
(void)OSM_Printf("输入年份
");
(void)OSM_Scanf("%d", &u32_year1);
flag = Is_Leap_Year(u32_year1);
if ( 1u == flag)
{
(void)OSM_Printf("是闰年
");
}
if ( 0u == flag)
{
(void)OSM_Printf("不是闰年
");
}
break;
case 2u:
(void)OSM_Printf("请输入年月日:
");
(void)OSM_Scanf("%4d%2d%2d",&u32_year1, &u32_mouth1, &u32_day1);
(void)Week(u32_year1, u32_mouth1, u32_day1);
break;
case 3u:
(void)OSM_Printf("请输入第1个日期:
");
(void)OSM_Scanf("%4d%2d%2d",&u32_year1, &u32_mouth1, &u32_day1);
(void)OSM_Printf("请输入第2个日期:
");
(void)OSM_Scanf("%4d%2d%2d",&u32_year2, &u32_mouth2, &u32_day2);
(void)Absolue_distance( u32_year1, u32_mouth1, u32_day1, u32_year2, u32_mouth2, u32_day2);
break;
case 4u:
(void)OSM_Printf("请输入年月日:
");
(void)OSM_Scanf("%4d%2d%2d",&u32_year1, &u32_mouth1, &u32_day1);
(void)Formatout1(u32_year1, u32_mouth1, u32_day1);
break;
default:
break;
}
return 0u;
}