rtc_calendar.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * File : rtc_calendar.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-02-10 Gary Lee first implementation
  13. * 2010-02-21 Gary Lee add __DATA__
  14. */
  15. #include "rtc_calendar.h"
  16. #include <string.h>
  17. extern void rt_hw_console_putc(char c);
  18. extern rt_uint8_t rt_hw_serial_getc(void);
  19. rt_uint32_t year_seprt=0;
  20. rt_uint8_t month_seprt=0;
  21. rt_uint8_t day_seprt=0;
  22. static const rt_int8_t *month_cn[12] ={ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月",
  23. "九月", "十月", "十一月", "十二月"
  24. };
  25. static const rt_int8_t *month_en[12] ={ "January", "February", "March", "April", "May", "June", "July",
  26. "Auguest", "September", "October", "November", "December"
  27. };
  28. static const rt_int8_t *day_en[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Firday","Saturday"};
  29. static const rt_int8_t *day_cn[7]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
  30. //=====================================================================
  31. //read from uart
  32. rt_int32_t rt_rtc_isleap(rt_uint32_t year)
  33. {
  34. rt_int32_t leap = 0;
  35. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 4 == 0))
  36. leap = 1;
  37. return leap;
  38. }
  39. rt_int32_t rt_rtc_week_of_newyears_day(rt_uint32_t year)
  40. {
  41. rt_int32_t n = year - 1900;
  42. n = n + (n - 1) / 4 + 1;
  43. n = n % 7;
  44. return n;
  45. }
  46. rt_int32_t rt_rtc_month_day_num(rt_int32_t month, rt_int32_t leapyn)
  47. {
  48. rt_int32_t len_month = 0;
  49. if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
  50. len_month = 30;
  51. else if (month == 2)
  52. {
  53. if (leapyn == 1)
  54. len_month = 29;
  55. else
  56. len_month = 28;
  57. }
  58. else
  59. len_month = 31;
  60. return len_month;
  61. }
  62. rt_int32_t rt_rtc_space_days(rt_int32_t month, rt_int32_t year)
  63. {
  64. rt_int32_t all_days = 0;
  65. rt_int32_t i = 1;
  66. rt_int32_t leap = rt_rtc_isleap(year);
  67. for (i = 1; i <= month; i++)
  68. {
  69. all_days = all_days + rt_rtc_month_day_num(i, leap);
  70. }
  71. return all_days;
  72. }
  73. rt_int32_t rt_rtc_weekday_month(rt_int32_t month, rt_int32_t year)
  74. {
  75. rt_int32_t space = 0, j, all_days = 0;
  76. rt_int32_t leap = rt_rtc_isleap(year);
  77. space = rt_rtc_week_of_newyears_day(year);
  78. for (j = 1; j <= month - 1; j++)
  79. {
  80. all_days = all_days + rt_rtc_month_day_num(j, leap);
  81. }
  82. space = (space + all_days) % 7;
  83. return space;
  84. }
  85. void rt_rtc_print_common_fmt(rt_uint8_t month, rt_uint8_t weekday, rt_uint8_t leapyear)
  86. {
  87. rt_int32_t day, j, len_of_month;
  88. rt_kprintf("\n%s %s %d\n",
  89. month_cn[month - 1], month_en[month - 1], year_seprt);
  90. rt_kprintf("----------------------------------\n");
  91. rt_kprintf("SUN MON TUE WED THU FRI SAT\n");
  92. rt_kprintf("----------------------------------\n");
  93. for (j = 0; j < weekday; j++)
  94. rt_kprintf(" ");
  95. len_of_month = rt_rtc_month_day_num(month, leapyear);
  96. for (day = 1; day <= len_of_month; day++)
  97. {
  98. if (day > 9)
  99. rt_kprintf("%d ", day);
  100. else
  101. rt_kprintf("%d ", day);
  102. weekday = weekday + 1;
  103. if (weekday == 7)
  104. {
  105. weekday = 0;
  106. rt_kprintf("\n");
  107. }
  108. }
  109. rt_kprintf("\n");
  110. }
  111. void rt_rtc_print_one_month(rt_int32_t month, rt_int32_t year)
  112. {
  113. rt_int32_t weekday = rt_rtc_weekday_month(month, year);
  114. rt_int32_t leapyear = rt_rtc_isleap(year);
  115. rt_rtc_print_common_fmt(month, weekday, leapyear);
  116. }
  117. void rt_rtc_print_calendar(rt_uint32_t year)
  118. {
  119. rt_uint8_t month;
  120. if (month_seprt == 0)
  121. {
  122. for (month = 1; month <= 12; month = month + 1)
  123. {
  124. rt_rtc_print_one_month(month, year);
  125. }
  126. }
  127. else
  128. rt_rtc_print_one_month(month_seprt, year);
  129. }
  130. void rt_rtc_year_month_day_seperate(rt_uint32_t year)
  131. {
  132. rt_uint32_t temp;
  133. if (year < 1900 || year > 30000000)
  134. {
  135. rt_kprintf("\nPlease input year and month, if not, system default is loaded!\n");
  136. year = DEFAULT_YEAR;
  137. }
  138. if (year / 100 < 30 && year / 100 > 18)
  139. {
  140. year_seprt = year;
  141. month_seprt = 0;
  142. day_seprt = 0;
  143. }
  144. else if (year / 100 < 300 && year / 100 > 196)
  145. {
  146. year_seprt = year / 10;
  147. month_seprt = year % 10;
  148. day_seprt = 0;
  149. }
  150. else if (year / 100 < 3000 && year / 100 > 1960)
  151. {
  152. year_seprt = year / 100;
  153. month_seprt = year % 100;
  154. if (month_seprt > 12)
  155. {
  156. temp = month_seprt;
  157. month_seprt = temp / 10;
  158. day_seprt = temp % 10;
  159. }
  160. else if (month_seprt < 10)
  161. day_seprt = 0;
  162. }
  163. else if (year / 100 < 30000 && year / 100 > 19600)
  164. {
  165. year_seprt = year / 1000;
  166. month_seprt = (year % 1000) / 100;
  167. if (month_seprt == 0)
  168. {
  169. month_seprt = (year % 100) / 10;
  170. day_seprt = year % 10;
  171. }
  172. else
  173. day_seprt = year % 100;
  174. temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
  175. if (day_seprt > temp)
  176. {
  177. rt_kprintf("\nError:There are only %d days this month, using default date\n", temp);
  178. day_seprt = DEFAULT_DAY;
  179. }
  180. }
  181. else
  182. {
  183. year_seprt = year / 10000;
  184. month_seprt = (year % 10000) / 100;
  185. if (month_seprt > 12)
  186. {
  187. rt_kprintf("\nError: There are only 12 months a year, using default date\n");
  188. month_seprt = DEFAULT_MONTH;
  189. }
  190. day_seprt = year % 100;
  191. temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
  192. if (day_seprt > temp)
  193. {
  194. rt_kprintf("\nError: There are %d days in this month, using default date\n", temp);
  195. day_seprt = DEFAULT_DAY;
  196. }
  197. }
  198. }
  199. void rt_rtc_weekdate_calculate(void)
  200. {
  201. rt_uint32_t temp;
  202. temp = rt_rtc_weekday_month(month_seprt, year_seprt) - 1;
  203. temp = (temp + day_seprt) % 7;
  204. rt_kprintf("%d/%d/%d/:%s\n", year_seprt, month_seprt, day_seprt, day_cn[temp]);
  205. }
  206. static const rt_uint8_t *list_month[12]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  207. void rt_calendar(void)
  208. {
  209. //static rt_uint8_t receive_char;
  210. static rt_int32_t year;
  211. rt_uint8_t i = 0;
  212. rt_int32_t result, num_month, num_year;
  213. rt_uint8_t date_year[5], date_month[4], *date = __DATE__;
  214. strlcpy((char *)date_month, (const char *)date, 4);
  215. date += 7;
  216. strlcpy((char *)date_year, (const char *)date, 5);
  217. date = RT_NULL;
  218. num_year = atoi(date_year);
  219. do
  220. {
  221. result = strcmp((const char *)date_month, (const char *)list_month[i++]);
  222. if(result !=0)
  223. result = 1;
  224. else
  225. num_month = i;
  226. }while(result);
  227. i = 0;
  228. result = 1;
  229. year = num_year*100 + num_month;
  230. //year = Uart_GetIntNum_MT();
  231. //rt_kprintf("\nThe date is %d\n", year);
  232. rt_rtc_year_month_day_seperate(year);
  233. if (day_seprt == 0 && month_seprt == 0)
  234. {
  235. //rt_kprintf("\nYear: %d\n", year_seprt);
  236. rt_rtc_print_calendar(year_seprt);
  237. }
  238. else if (day_seprt == 0)
  239. {
  240. //rt_kprintf("\n%d/%d\n", year_seprt, month_seprt);
  241. rt_rtc_print_calendar(year_seprt);
  242. }
  243. else
  244. {
  245. //rt_kprintf("\n%d/%d/%d\n", year_seprt, month_seprt, day_seprt);
  246. rt_rtc_weekdate_calculate();
  247. }
  248. }
  249. #ifdef RT_USING_FINSH
  250. #include <finsh.h>
  251. FINSH_FUNCTION_EXPORT(rt_calendar, print calendar)
  252. #endif