rtc_calendar.c 7.0 KB
Newer Older
G
gary.li.wenchao.4 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * 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
G
gary.li.wenchao.4 已提交
13
 * 2010-02-21	  Gary Lee	   add __DATA__
G
gary.li.wenchao.4 已提交
14 15 16
 */

#include "rtc_calendar.h"
17
#include <string.h>
G
gary.li.wenchao.4 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

extern void rt_hw_console_putc(char c);
extern rt_uint8_t rt_hw_serial_getc(void);

rt_uint32_t year_seprt=0;
rt_uint8_t  month_seprt=0;
rt_uint8_t  day_seprt=0;

rt_int8_t *month_cn[12] ={ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
	"九月", "十月", "十一月", "十二月"
};
rt_int8_t *month_en[12] ={ "January", "February", "March", "April", "May", "June", "July",
	"Auguest", "September", "October", "November", "December"
};

rt_int8_t *day_en[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Firday","Saturday"};
rt_int8_t *day_cn[7]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
//=====================================================================
//read from uart

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;
}

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;
}

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;
}

rt_int32_t rt_rtc_space_days(rt_int32_t month, rt_int32_t year)
{
	rt_int32_t all_days = 0;
	rt_int32_t i = 1;
	rt_int32_t leap = rt_rtc_isleap(year);
	for (i = 1; i <= month; i++) {
		all_days = all_days + rt_rtc_month_day_num(i, leap);
	}
	return all_days;
}

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;
}

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");
}

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);
}


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);

}

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;
	}
149
	if (year / 100 < 30 && year / 100 > 18)
G
gary.li.wenchao.4 已提交
150 151 152 153
	{
		year_seprt = year;
		month_seprt = 0;
		day_seprt = 0;
154
	} else if (year / 100 < 300 && year / 100 > 196)
G
gary.li.wenchao.4 已提交
155 156 157 158
	{
		year_seprt = year / 10;
		month_seprt = year % 10;
		day_seprt = 0;
159 160
	} else if (year / 100 < 3000 && year / 100 > 1960)
	{
G
gary.li.wenchao.4 已提交
161 162 163 164 165 166 167 168 169
		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;

170 171
	} else if (year / 100 < 30000 && year / 100 > 19600)
	{
G
gary.li.wenchao.4 已提交
172 173
		year_seprt = year / 1000;
		month_seprt = (year % 1000) / 100;
174
		if (month_seprt == 0)
G
gary.li.wenchao.4 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
		{
			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
			    ("\nError:There 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;
		}
	}
}

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]);

}

214
extern size_t strlcpy(char *dest, const char *src, size_t siz);
G
gary.li.wenchao.4 已提交
215
rt_uint8_t *list_month[12]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
G
gary.li.wenchao.4 已提交
216
void rt_calendar(void)
G
gary.li.wenchao.4 已提交
217 218
{
	//static rt_uint8_t receive_char;
G
gary.li.wenchao.4 已提交
219 220 221 222
	static rt_int32_t year;
	rt_uint8_t i = 0;
	rt_int32_t result, num_month, num_year;
	rt_uint8_t date_year[7], date_month[3], *date = __DATE__;
223 224

	strlcpy((char *)date_month, (const char *)date, 3);
G
gary.li.wenchao.4 已提交
225
	date += 7;
226
	strlcpy((char *)date_year,  (const char *)date, 4);
G
gary.li.wenchao.4 已提交
227 228 229 230 231 232 233 234
	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;
235
	}while(result);
G
gary.li.wenchao.4 已提交
236 237 238
	i = 0;
	result = 1;
	year = num_year*100 + num_month;
G
gary.li.wenchao.4 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		//year = Uart_GetIntNum_MT();
		//rt_kprintf("\nThe date is %d\n", year);
		rt_rtc_year_month_day_seperate(year);

		if (day_seprt == 0 && month_seprt == 0) {
			//rt_kprintf("\nYear: %d\n", year_seprt);
			rt_rtc_print_calendar(year_seprt);

		} else if (day_seprt == 0) {
			//rt_kprintf("\n%d/%d\n", year_seprt, month_seprt);
			rt_rtc_print_calendar(year_seprt);

		} else {
			//rt_kprintf("\n%d/%d/%d\n", year_seprt, month_seprt, day_seprt);
			rt_rtc_weekdate_calculate();
		}
}
#ifdef RT_USING_FINSH
#include <finsh.h>
G
gary.li.wenchao.4 已提交
258
FINSH_FUNCTION_EXPORT(rt_calendar, print calendar)
G
gary.li.wenchao.4 已提交
259
#endif