2010-02-19 08:29:43 +08:00
|
|
|
|
/*
|
|
|
|
|
* 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-09-23 19:15:41 +08:00
|
|
|
|
* 2010-02-21 Gary Lee add __DATA__
|
2010-02-19 08:29:43 +08:00
|
|
|
|
*/
|
2010-09-23 19:15:41 +08:00
|
|
|
|
#include <rtthread.h>
|
2010-03-17 12:17:20 +08:00
|
|
|
|
#include <string.h>
|
2010-02-19 08:29:43 +08:00
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
#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
|
2010-02-19 08:29:43 +08:00
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static rt_uint32_t year_seprt = 0;
|
|
|
|
|
static rt_uint8_t month_seprt = 0;
|
|
|
|
|
static rt_uint8_t day_seprt = 0;
|
2010-02-19 08:29:43 +08:00
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static const rt_int8_t *month_cn[12] = { "һ<EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", "ʮ<EFBFBD><EFBFBD>", "ʮһ<EFBFBD><EFBFBD>", "ʮ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>"};
|
|
|
|
|
static const rt_int8_t *day_cn[7] = {"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ","<EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>","<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"};
|
|
|
|
|
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"};
|
2010-02-19 08:29:43 +08:00
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static rt_int32_t rt_rtc_isleap(rt_uint32_t year)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
rt_int32_t leap = 0;
|
|
|
|
|
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 4 == 0))
|
|
|
|
|
leap = 1;
|
|
|
|
|
return leap;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static rt_int32_t rt_rtc_week_of_newyears_day(rt_uint32_t year)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
rt_int32_t n = year - 1900;
|
|
|
|
|
n = n + (n - 1) / 4 + 1;
|
|
|
|
|
n = n % 7;
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static rt_int32_t rt_rtc_month_day_num(rt_int32_t month, rt_int32_t leapyn)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
rt_int32_t len_month = 0;
|
|
|
|
|
if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
|
|
|
|
|
len_month = 30;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
else if (month == 2)
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
if (leapyn == 1)
|
|
|
|
|
len_month = 29;
|
|
|
|
|
else
|
|
|
|
|
len_month = 28;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2010-02-19 08:29:43 +08:00
|
|
|
|
len_month = 31;
|
|
|
|
|
return len_month;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static rt_int32_t rt_rtc_weekday_month(rt_int32_t month, rt_int32_t year)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
2010-03-18 10:01:20 +08:00
|
|
|
|
for (j = 1; j <= month - 1; j++)
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
all_days = all_days + rt_rtc_month_day_num(j, leap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
space = (space + all_days) % 7;
|
2010-09-23 19:15:41 +08:00
|
|
|
|
return space;
|
2010-02-19 08:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static void rt_rtc_print_common_fmt(rt_uint8_t month, rt_uint8_t weekday, rt_uint8_t leapyear)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
rt_int32_t day, j, len_of_month;
|
|
|
|
|
|
|
|
|
|
rt_kprintf("\n%s %s %d\n",
|
2010-03-18 10:01:20 +08:00
|
|
|
|
month_cn[month - 1], month_en[month - 1], year_seprt);
|
2010-02-19 08:29:43 +08:00
|
|
|
|
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);
|
|
|
|
|
|
2010-03-18 10:01:20 +08:00
|
|
|
|
for (day = 1; day <= len_of_month; day++)
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
if (day > 9)
|
|
|
|
|
rt_kprintf("%d ", day);
|
|
|
|
|
else
|
|
|
|
|
rt_kprintf("%d ", day);
|
|
|
|
|
weekday = weekday + 1;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
if (weekday == 7)
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
weekday = 0;
|
|
|
|
|
rt_kprintf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rt_kprintf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static void rt_rtc_print_one_month(rt_int32_t month, rt_int32_t year)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static void rt_rtc_print_calendar(rt_uint32_t year)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
rt_uint8_t month;
|
|
|
|
|
|
2010-03-18 10:01:20 +08:00
|
|
|
|
if (month_seprt == 0)
|
|
|
|
|
{
|
|
|
|
|
for (month = 1; month <= 12; month = month + 1)
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
rt_rtc_print_one_month(month, year);
|
|
|
|
|
}
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2010-02-19 08:29:43 +08:00
|
|
|
|
rt_rtc_print_one_month(month_seprt, year);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static void rt_rtc_year_month_day_seperate(rt_uint32_t year)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
rt_uint32_t temp;
|
|
|
|
|
|
2010-03-18 10:01:20 +08:00
|
|
|
|
if (year < 1900 || year > 30000000)
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
rt_kprintf("\nPlease input year and month, if not, system default is loaded!\n");
|
|
|
|
|
year = DEFAULT_YEAR;
|
|
|
|
|
}
|
2010-03-17 12:17:20 +08:00
|
|
|
|
if (year / 100 < 30 && year / 100 > 18)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
year_seprt = year;
|
|
|
|
|
month_seprt = 0;
|
|
|
|
|
day_seprt = 0;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else if (year / 100 < 300 && year / 100 > 196)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
year_seprt = year / 10;
|
|
|
|
|
month_seprt = year % 10;
|
|
|
|
|
day_seprt = 0;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else if (year / 100 < 3000 && year / 100 > 1960)
|
2010-03-17 12:17:20 +08:00
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
year_seprt = year / 100;
|
|
|
|
|
month_seprt = year % 100;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
if (month_seprt > 12)
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
temp = month_seprt;
|
|
|
|
|
month_seprt = temp / 10;
|
|
|
|
|
day_seprt = temp % 10;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else if (month_seprt < 10)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
day_seprt = 0;
|
|
|
|
|
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else if (year / 100 < 30000 && year / 100 > 19600)
|
2010-03-17 12:17:20 +08:00
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
year_seprt = year / 1000;
|
|
|
|
|
month_seprt = (year % 1000) / 100;
|
2010-03-17 12:17:20 +08:00
|
|
|
|
if (month_seprt == 0)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
month_seprt = (year % 100) / 10;
|
|
|
|
|
day_seprt = year % 10;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2010-02-19 08:29:43 +08:00
|
|
|
|
day_seprt = year % 100;
|
|
|
|
|
temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
|
2010-03-18 10:01:20 +08:00
|
|
|
|
if (day_seprt > temp)
|
|
|
|
|
{
|
|
|
|
|
rt_kprintf("\nError<EFBFBD><EFBFBD>There are only %d days this month, using default date\n", temp);
|
2010-02-19 08:29:43 +08:00
|
|
|
|
day_seprt = DEFAULT_DAY;
|
|
|
|
|
}
|
2010-03-18 10:01:20 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-02-19 08:29:43 +08:00
|
|
|
|
year_seprt = year / 10000;
|
|
|
|
|
month_seprt = (year % 10000) / 100;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
if (month_seprt > 12)
|
|
|
|
|
{
|
|
|
|
|
rt_kprintf("\nError: There are only 12 months a year, using default date\n");
|
2010-02-19 08:29:43 +08:00
|
|
|
|
month_seprt = DEFAULT_MONTH;
|
|
|
|
|
}
|
|
|
|
|
day_seprt = year % 100;
|
|
|
|
|
temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
|
2010-03-18 10:01:20 +08:00
|
|
|
|
if (day_seprt > temp)
|
|
|
|
|
{
|
|
|
|
|
rt_kprintf("\nError: There are %d days in this month, using default date\n", temp);
|
2010-02-19 08:29:43 +08:00
|
|
|
|
day_seprt = DEFAULT_DAY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
static void rt_rtc_weekdate_calculate(void)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-23 19:15:41 +08:00
|
|
|
|
void calendar(void)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
{
|
2010-02-21 10:34:28 +08:00
|
|
|
|
static rt_int32_t year;
|
|
|
|
|
rt_uint8_t i = 0;
|
|
|
|
|
rt_int32_t result, num_month, num_year;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
rt_uint8_t date_year[5], date_month[4], *date = __DATE__;
|
2010-03-17 12:17:20 +08:00
|
|
|
|
|
2010-03-18 10:01:20 +08:00
|
|
|
|
strlcpy((char *)date_month, (const char *)date, 4);
|
2010-02-21 10:34:28 +08:00
|
|
|
|
date += 7;
|
2010-03-18 10:01:20 +08:00
|
|
|
|
strlcpy((char *)date_year, (const char *)date, 5);
|
2010-02-21 10:34:28 +08:00
|
|
|
|
date = RT_NULL;
|
|
|
|
|
num_year = atoi(date_year);
|
2010-03-18 10:01:20 +08:00
|
|
|
|
do
|
|
|
|
|
{
|
2010-02-21 10:34:28 +08:00
|
|
|
|
result = strcmp((const char *)date_month, (const char *)list_month[i++]);
|
|
|
|
|
if(result !=0)
|
|
|
|
|
result = 1;
|
|
|
|
|
else
|
|
|
|
|
num_month = i;
|
2010-03-17 12:17:20 +08:00
|
|
|
|
}while(result);
|
2010-02-21 10:34:28 +08:00
|
|
|
|
i = 0;
|
|
|
|
|
result = 1;
|
|
|
|
|
year = num_year*100 + num_month;
|
2010-09-23 19:15:41 +08:00
|
|
|
|
|
2010-03-18 10:01:20 +08:00
|
|
|
|
rt_rtc_year_month_day_seperate(year);
|
2010-02-19 08:29:43 +08:00
|
|
|
|
|
2010-03-18 10:01:20 +08:00
|
|
|
|
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();
|
|
|
|
|
}
|
2010-02-19 08:29:43 +08:00
|
|
|
|
}
|
|
|
|
|
#ifdef RT_USING_FINSH
|
|
|
|
|
#include <finsh.h>
|
2010-09-23 19:15:41 +08:00
|
|
|
|
FINSH_FUNCTION_EXPORT(calendar, print calendar)
|
2010-02-19 08:29:43 +08:00
|
|
|
|
#endif
|