bsp_date.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "bsp_date.h"
  2. #define FEBRUARY 2
  3. #define STARTOFTIME 1970
  4. #define SECDAY 86400L /* 一天有多少s */
  5. #define SECYR (SECDAY * 365)
  6. #define leapyear(year) ((year) % 4 == 0)
  7. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  8. #define days_in_month(a) (month_days[(a) - 1])
  9. static int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  10. /*
  11. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  12. */
  13. /*计算公历*/
  14. void GregorianDay(struct rtc_time * tm)
  15. {
  16. int leapsToDate;
  17. int lastYear;
  18. int day;
  19. int MonthOffset[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
  20. lastYear=tm->tm_year-1;
  21. /*计算从公元元年到计数的前一年之中一共经历了多少个闰年*/
  22. leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
  23. /*如若计数的这一年为闰年,且计数的月份在2月之后,则日数加1,否则不加1*/
  24. if((tm->tm_year%4==0) &&
  25. ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
  26. (tm->tm_mon>2)) {
  27. /*
  28. * We are past Feb. 29 in a leap year
  29. */
  30. day=1;
  31. } else {
  32. day=0;
  33. }
  34. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + tm->tm_mday; /*计算从公元元年元旦到计数日期一共有多少天*/
  35. tm->tm_wday=day%7;
  36. }
  37. /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  38. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  39. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  40. *
  41. * [For the Julian calendar (which was used in Russia before 1917,
  42. * Britain & colonies before 1752, anywhere else before 1582,
  43. * and is still in use by some communities) leave out the
  44. * -year/100+year/400 terms, and add 10.]
  45. *
  46. * This algorithm was first published by Gauss (I think).
  47. *
  48. * WARNING: this function will overflow on 2106-02-07 06:28:16 on
  49. * machines were long is 32-bit! (However, as time_t is signed, we
  50. * will already get problems at other places on 2038-01-19 03:14:08)
  51. *ADD by fire:本函数在工程中的输入参数为北京时间,
  52. 所以在转换成时间戳时最后要从北京时间转换为标准时间的时间戳
  53. */
  54. uint32_t mktimev(struct rtc_time *tm)
  55. {
  56. // if (0 >= (int) (tm->tm_mon -= 2)) { /* 1..12 -> 11,12,1..10 */
  57. // tm->tm_mon += 12; /* Puts Feb last since it has leap day */
  58. // tm->tm_year -= 1;
  59. // }
  60. //
  61. // return (((
  62. // (uint32_t) (tm->tm_year/4 - tm->tm_year/100 + tm->tm_year/400 + 367*tm->tm_mon/12 + tm->tm_mday) +
  63. // tm->tm_year*365 - 719499
  64. // )*24 + tm->tm_hour /* now have hours */
  65. // )*60 + tm->tm_min /* now have minutes */
  66. // )*60 + tm->tm_sec-8*60*60; /* finally seconds */
  67. // /*Add by fire: -8*60*60 把输入的北京时间转换为标准时间,
  68. // 再写入计时器中,确保计时器的数据为标准的UNIX时间戳*/
  69. // return (uint32_t)(
  70. // tm->tm_year * 31536000 +
  71. // tm->tm_mon *
  72. // );
  73. return 0;
  74. }
  75. void to_tm(uint32_t tim, struct rtc_time * tm)
  76. {
  77. register uint32_t i;
  78. register long hms, day;
  79. day = tim / SECDAY; /* 有多少天 */
  80. hms = tim % SECDAY; /* 今天的时间,单位s */
  81. /* Hours, minutes, seconds are easy */
  82. tm->tm_hour = hms / 3600;
  83. tm->tm_min = (hms % 3600) / 60;
  84. tm->tm_sec = (hms % 3600) % 60;
  85. /* Number of years in days */ /*算出当前年份,起始的计数年份为1970年*/
  86. for (i = STARTOFTIME; day >= days_in_year(i); i++) {
  87. day -= days_in_year(i);
  88. }
  89. tm->tm_year = i;
  90. /* Number of months in days left */ /*计算当前的月份*/
  91. if (leapyear(tm->tm_year)) {
  92. days_in_month(FEBRUARY) = 29;
  93. }
  94. for (i = 1; day >= days_in_month(i); i++) {
  95. day -= days_in_month(i);
  96. }
  97. days_in_month(FEBRUARY) = 28;
  98. tm->tm_mon = i;
  99. /* Days are what is left over (+1) from all that. *//*计算当前日期*/
  100. tm->tm_mday = day + 1;
  101. /*
  102. * Determine the day of week
  103. */
  104. GregorianDay(tm);
  105. }