rtt-f030/bsp/mini2440/calendar.c

256 lines
6.4 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* File : rtc_calendar.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006, RT-Thread Develop Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2010-02-10 Gary Lee first implementation
* 2010-02-21 Gary Lee add __DATA__
*/
#include <rtthread.h>
#include <string.h>
#define DEFAULT_YEAR_MONTH_DAY (DEFAULT_YEAR*10000+DEFAULT_MONTH*100+DEFAULT_DAY)
#define DEFAULT_YEAR 2010
#define DEFAULT_MONTH 01
#define DEFAULT_DAY 01
static rt_uint32_t year_seprt = 0;
static rt_uint8_t month_seprt = 0;
static rt_uint8_t day_seprt = 0;
static const rt_int8_t *month_cn[12] = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月","九月", "十月", "十一月", "十二月"};
static const rt_int8_t *day_cn[7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
static const rt_int8_t *month_en[12] = { "January", "February", "March", "April", "May", "June", "July","Auguest", "September", "October", "November", "December"};
static const rt_uint8_t *list_month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static rt_int32_t rt_rtc_isleap(rt_uint32_t year)
{
rt_int32_t leap = 0;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 4 == 0))
leap = 1;
return leap;
}
static rt_int32_t rt_rtc_week_of_newyears_day(rt_uint32_t year)
{
rt_int32_t n = year - 1900;
n = n + (n - 1) / 4 + 1;
n = n % 7;
return n;
}
static rt_int32_t rt_rtc_month_day_num(rt_int32_t month, rt_int32_t leapyn)
{
rt_int32_t len_month = 0;
if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
len_month = 30;
else if (month == 2)
{
if (leapyn == 1)
len_month = 29;
else
len_month = 28;
}
else
len_month = 31;
return len_month;
}
static rt_int32_t rt_rtc_weekday_month(rt_int32_t month, rt_int32_t year)
{
rt_int32_t space = 0, j, all_days = 0;
rt_int32_t leap = rt_rtc_isleap(year);
space = rt_rtc_week_of_newyears_day(year);
for (j = 1; j <= month - 1; j++)
{
all_days = all_days + rt_rtc_month_day_num(j, leap);
}
space = (space + all_days) % 7;
return space;
}
static void rt_rtc_print_common_fmt(rt_uint8_t month, rt_uint8_t weekday, rt_uint8_t leapyear)
{
rt_int32_t day, j, len_of_month;
rt_kprintf("\n%s %s %d\n",
month_cn[month - 1], month_en[month - 1], year_seprt);
rt_kprintf("----------------------------------\n");
rt_kprintf("SUN MON TUE WED THU FRI SAT\n");
rt_kprintf("----------------------------------\n");
for (j = 0; j < weekday; j++)
rt_kprintf(" ");
len_of_month = rt_rtc_month_day_num(month, leapyear);
for (day = 1; day <= len_of_month; day++)
{
if (day > 9)
rt_kprintf("%d ", day);
else
rt_kprintf("%d ", day);
weekday = weekday + 1;
if (weekday == 7)
{
weekday = 0;
rt_kprintf("\n");
}
}
rt_kprintf("\n");
}
static void rt_rtc_print_one_month(rt_int32_t month, rt_int32_t year)
{
rt_int32_t weekday = rt_rtc_weekday_month(month, year);
rt_int32_t leapyear = rt_rtc_isleap(year);
rt_rtc_print_common_fmt(month, weekday, leapyear);
}
static void rt_rtc_print_calendar(rt_uint32_t year)
{
rt_uint8_t month;
if (month_seprt == 0)
{
for (month = 1; month <= 12; month = month + 1)
{
rt_rtc_print_one_month(month, year);
}
}
else
rt_rtc_print_one_month(month_seprt, year);
}
static void rt_rtc_year_month_day_seperate(rt_uint32_t year)
{
rt_uint32_t temp;
if (year < 1900 || year > 30000000)
{
rt_kprintf("\nPlease input year and month, if not, system default is loaded!\n");
year = DEFAULT_YEAR;
}
if (year / 100 < 30 && year / 100 > 18)
{
year_seprt = year;
month_seprt = 0;
day_seprt = 0;
}
else if (year / 100 < 300 && year / 100 > 196)
{
year_seprt = year / 10;
month_seprt = year % 10;
day_seprt = 0;
}
else if (year / 100 < 3000 && year / 100 > 1960)
{
year_seprt = year / 100;
month_seprt = year % 100;
if (month_seprt > 12)
{
temp = month_seprt;
month_seprt = temp / 10;
day_seprt = temp % 10;
}
else if (month_seprt < 10)
day_seprt = 0;
}
else if (year / 100 < 30000 && year / 100 > 19600)
{
year_seprt = year / 1000;
month_seprt = (year % 1000) / 100;
if (month_seprt == 0)
{
month_seprt = (year % 100) / 10;
day_seprt = year % 10;
}
else
day_seprt = year % 100;
temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
if (day_seprt > temp)
{
rt_kprintf("\nErrorThere are only %d days this month, using default date\n", temp);
day_seprt = DEFAULT_DAY;
}
}
else
{
year_seprt = year / 10000;
month_seprt = (year % 10000) / 100;
if (month_seprt > 12)
{
rt_kprintf("\nError: There are only 12 months a year, using default date\n");
month_seprt = DEFAULT_MONTH;
}
day_seprt = year % 100;
temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
if (day_seprt > temp)
{
rt_kprintf("\nError: There are %d days in this month, using default date\n", temp);
day_seprt = DEFAULT_DAY;
}
}
}
static void rt_rtc_weekdate_calculate(void)
{
rt_uint32_t temp;
temp = rt_rtc_weekday_month(month_seprt, year_seprt) - 1;
temp = (temp + day_seprt) % 7;
rt_kprintf("%d/%d/%d/:%s\n", year_seprt, month_seprt, day_seprt, day_cn[temp]);
}
void calendar(void)
{
static rt_int32_t year;
rt_uint8_t i = 0;
rt_int32_t result, num_month, num_year;
rt_uint8_t date_year[5], date_month[4], *date = __DATE__;
strlcpy((char *)date_month, (const char *)date, 4);
date += 7;
strlcpy((char *)date_year, (const char *)date, 5);
date = RT_NULL;
num_year = atoi(date_year);
do
{
result = strcmp((const char *)date_month, (const char *)list_month[i++]);
if(result !=0)
result = 1;
else
num_month = i;
}while(result);
i = 0;
result = 1;
year = num_year*100 + num_month;
rt_rtc_year_month_day_seperate(year);
if (day_seprt == 0 && month_seprt == 0)
{
rt_rtc_print_calendar(year_seprt);
}
else if (day_seprt == 0)
{
rt_rtc_print_calendar(year_seprt);
}
else
{
rt_rtc_weekdate_calculate();
}
}
#ifdef RT_USING_FINSH
#include <finsh.h>
FINSH_FUNCTION_EXPORT(calendar, print calendar)
#endif