calendar.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <rtthread.h>
  16. #include <string.h>
  17. #define DEFAULT_YEAR_MONTH_DAY (DEFAULT_YEAR*10000+DEFAULT_MONTH*100+DEFAULT_DAY)
  18. #define DEFAULT_YEAR 2010
  19. #define DEFAULT_MONTH 01
  20. #define DEFAULT_DAY 01
  21. static rt_uint32_t year_seprt = 0;
  22. static rt_uint8_t month_seprt = 0;
  23. static rt_uint8_t day_seprt = 0;
  24. static const rt_int8_t *month_cn[12] = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月","九月", "十月", "十一月", "十二月"};
  25. static const rt_int8_t *day_cn[7] = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
  26. static const rt_int8_t *month_en[12] = { "January", "February", "March", "April", "May", "June", "July","Auguest", "September", "October", "November", "December"};
  27. static const rt_uint8_t *list_month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  28. static rt_int32_t rt_rtc_isleap(rt_uint32_t year)
  29. {
  30. rt_int32_t leap = 0;
  31. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 4 == 0))
  32. leap = 1;
  33. return leap;
  34. }
  35. static rt_int32_t rt_rtc_week_of_newyears_day(rt_uint32_t year)
  36. {
  37. rt_int32_t n = year - 1900;
  38. n = n + (n - 1) / 4 + 1;
  39. n = n % 7;
  40. return n;
  41. }
  42. static rt_int32_t rt_rtc_month_day_num(rt_int32_t month, rt_int32_t leapyn)
  43. {
  44. rt_int32_t len_month = 0;
  45. if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
  46. len_month = 30;
  47. else if (month == 2)
  48. {
  49. if (leapyn == 1)
  50. len_month = 29;
  51. else
  52. len_month = 28;
  53. }
  54. else
  55. len_month = 31;
  56. return len_month;
  57. }
  58. static rt_int32_t rt_rtc_weekday_month(rt_int32_t month, rt_int32_t year)
  59. {
  60. rt_int32_t space = 0, j, all_days = 0;
  61. rt_int32_t leap = rt_rtc_isleap(year);
  62. space = rt_rtc_week_of_newyears_day(year);
  63. for (j = 1; j <= month - 1; j++)
  64. {
  65. all_days = all_days + rt_rtc_month_day_num(j, leap);
  66. }
  67. space = (space + all_days) % 7;
  68. return space;
  69. }
  70. static void rt_rtc_print_common_fmt(rt_uint8_t month, rt_uint8_t weekday, rt_uint8_t leapyear)
  71. {
  72. rt_int32_t day, j, len_of_month;
  73. rt_kprintf("\n%s %s %d\n",
  74. month_cn[month - 1], month_en[month - 1], year_seprt);
  75. rt_kprintf("----------------------------------\n");
  76. rt_kprintf("SUN MON TUE WED THU FRI SAT\n");
  77. rt_kprintf("----------------------------------\n");
  78. for (j = 0; j < weekday; j++)
  79. rt_kprintf(" ");
  80. len_of_month = rt_rtc_month_day_num(month, leapyear);
  81. for (day = 1; day <= len_of_month; day++)
  82. {
  83. if (day > 9)
  84. rt_kprintf("%d ", day);
  85. else
  86. rt_kprintf("%d ", day);
  87. weekday = weekday + 1;
  88. if (weekday == 7)
  89. {
  90. weekday = 0;
  91. rt_kprintf("\n");
  92. }
  93. }
  94. rt_kprintf("\n");
  95. }
  96. static void rt_rtc_print_one_month(rt_int32_t month, rt_int32_t year)
  97. {
  98. rt_int32_t weekday = rt_rtc_weekday_month(month, year);
  99. rt_int32_t leapyear = rt_rtc_isleap(year);
  100. rt_rtc_print_common_fmt(month, weekday, leapyear);
  101. }
  102. static void rt_rtc_print_calendar(rt_uint32_t year)
  103. {
  104. rt_uint8_t month;
  105. if (month_seprt == 0)
  106. {
  107. for (month = 1; month <= 12; month = month + 1)
  108. {
  109. rt_rtc_print_one_month(month, year);
  110. }
  111. }
  112. else
  113. rt_rtc_print_one_month(month_seprt, year);
  114. }
  115. static void rt_rtc_year_month_day_seperate(rt_uint32_t year)
  116. {
  117. rt_uint32_t temp;
  118. if (year < 1900 || year > 30000000)
  119. {
  120. rt_kprintf("\nPlease input year and month, if not, system default is loaded!\n");
  121. year = DEFAULT_YEAR;
  122. }
  123. if (year / 100 < 30 && year / 100 > 18)
  124. {
  125. year_seprt = year;
  126. month_seprt = 0;
  127. day_seprt = 0;
  128. }
  129. else if (year / 100 < 300 && year / 100 > 196)
  130. {
  131. year_seprt = year / 10;
  132. month_seprt = year % 10;
  133. day_seprt = 0;
  134. }
  135. else if (year / 100 < 3000 && year / 100 > 1960)
  136. {
  137. year_seprt = year / 100;
  138. month_seprt = year % 100;
  139. if (month_seprt > 12)
  140. {
  141. temp = month_seprt;
  142. month_seprt = temp / 10;
  143. day_seprt = temp % 10;
  144. }
  145. else if (month_seprt < 10)
  146. day_seprt = 0;
  147. }
  148. else if (year / 100 < 30000 && year / 100 > 19600)
  149. {
  150. year_seprt = year / 1000;
  151. month_seprt = (year % 1000) / 100;
  152. if (month_seprt == 0)
  153. {
  154. month_seprt = (year % 100) / 10;
  155. day_seprt = year % 10;
  156. }
  157. else
  158. day_seprt = year % 100;
  159. temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
  160. if (day_seprt > temp)
  161. {
  162. rt_kprintf("\nError:There are only %d days this month, using default date\n", temp);
  163. day_seprt = DEFAULT_DAY;
  164. }
  165. }
  166. else
  167. {
  168. year_seprt = year / 10000;
  169. month_seprt = (year % 10000) / 100;
  170. if (month_seprt > 12)
  171. {
  172. rt_kprintf("\nError: There are only 12 months a year, using default date\n");
  173. month_seprt = DEFAULT_MONTH;
  174. }
  175. day_seprt = year % 100;
  176. temp = rt_rtc_month_day_num(month_seprt, rt_rtc_isleap(year_seprt));
  177. if (day_seprt > temp)
  178. {
  179. rt_kprintf("\nError: There are %d days in this month, using default date\n", temp);
  180. day_seprt = DEFAULT_DAY;
  181. }
  182. }
  183. }
  184. static void rt_rtc_weekdate_calculate(void)
  185. {
  186. rt_uint32_t temp;
  187. temp = rt_rtc_weekday_month(month_seprt, year_seprt) - 1;
  188. temp = (temp + day_seprt) % 7;
  189. rt_kprintf("%d/%d/%d/:%s\n", year_seprt, month_seprt, day_seprt, day_cn[temp]);
  190. }
  191. void calendar(void)
  192. {
  193. static rt_int32_t year;
  194. rt_uint8_t i = 0;
  195. rt_int32_t result, num_month, num_year;
  196. rt_uint8_t date_year[5], date_month[4], *date = __DATE__;
  197. strlcpy((char *)date_month, (const char *)date, 4);
  198. date += 7;
  199. strlcpy((char *)date_year, (const char *)date, 5);
  200. date = RT_NULL;
  201. num_year = atoi(date_year);
  202. do
  203. {
  204. result = strcmp((const char *)date_month, (const char *)list_month[i++]);
  205. if(result !=0)
  206. result = 1;
  207. else
  208. num_month = i;
  209. }while(result);
  210. i = 0;
  211. result = 1;
  212. year = num_year*100 + num_month;
  213. rt_rtc_year_month_day_seperate(year);
  214. if (day_seprt == 0 && month_seprt == 0)
  215. {
  216. rt_rtc_print_calendar(year_seprt);
  217. }
  218. else if (day_seprt == 0)
  219. {
  220. rt_rtc_print_calendar(year_seprt);
  221. }
  222. else
  223. {
  224. rt_rtc_weekdate_calculate();
  225. }
  226. }
  227. #ifdef RT_USING_FINSH
  228. #include <finsh.h>
  229. FINSH_FUNCTION_EXPORT(calendar, print calendar)
  230. #endif