date.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Date & Time support for Philips PCF8563 RTC
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <errno.h>
  13. #include <rtc.h>
  14. #if defined(CONFIG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
  15. #define FEBRUARY 2
  16. #define STARTOFTIME 1970
  17. #define SECDAY 86400L
  18. #define SECYR (SECDAY * 365)
  19. #define leapyear(year) ((year) % 4 == 0)
  20. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  21. #define days_in_month(a) (month_days[(a) - 1])
  22. static int month_days[12] = {
  23. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  24. };
  25. /*
  26. * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
  27. */
  28. int rtc_calc_weekday(struct rtc_time *tm)
  29. {
  30. int leapsToDate;
  31. int lastYear;
  32. int day;
  33. int MonthOffset[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
  34. if (tm->tm_year < 1753)
  35. return -EINVAL;
  36. lastYear=tm->tm_year-1;
  37. /*
  38. * Number of leap corrections to apply up to end of last year
  39. */
  40. leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
  41. /*
  42. * This year is a leap year if it is divisible by 4 except when it is
  43. * divisible by 100 unless it is divisible by 400
  44. *
  45. * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 will be
  46. */
  47. if((tm->tm_year%4==0) &&
  48. ((tm->tm_year%100!=0) || (tm->tm_year%400==0)) &&
  49. (tm->tm_mon>2)) {
  50. /*
  51. * We are past Feb. 29 in a leap year
  52. */
  53. day=1;
  54. } else {
  55. day=0;
  56. }
  57. day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] + tm->tm_mday;
  58. tm->tm_wday=day%7;
  59. return 0;
  60. }
  61. void to_tm(int tim, struct rtc_time * tm)
  62. {
  63. register int i;
  64. register long hms, day;
  65. day = tim / SECDAY;
  66. hms = tim % SECDAY;
  67. /* Hours, minutes, seconds are easy */
  68. tm->tm_hour = hms / 3600;
  69. tm->tm_min = (hms % 3600) / 60;
  70. tm->tm_sec = (hms % 3600) % 60;
  71. /* Number of years in days */
  72. for (i = STARTOFTIME; day >= days_in_year(i); i++) {
  73. day -= days_in_year(i);
  74. }
  75. tm->tm_year = i;
  76. /* Number of months in days left */
  77. if (leapyear(tm->tm_year)) {
  78. days_in_month(FEBRUARY) = 29;
  79. }
  80. for (i = 1; day >= days_in_month(i); i++) {
  81. day -= days_in_month(i);
  82. }
  83. days_in_month(FEBRUARY) = 28;
  84. tm->tm_mon = i;
  85. /* Days are what is left over (+1) from all that. */
  86. tm->tm_mday = day + 1;
  87. /*
  88. * Determine the day of week
  89. */
  90. rtc_calc_weekday(tm);
  91. }
  92. /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  93. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  94. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  95. *
  96. * [For the Julian calendar (which was used in Russia before 1917,
  97. * Britain & colonies before 1752, anywhere else before 1582,
  98. * and is still in use by some communities) leave out the
  99. * -year/100+year/400 terms, and add 10.]
  100. *
  101. * This algorithm was first published by Gauss (I think).
  102. *
  103. * WARNING: this function will overflow on 2106-02-07 06:28:16 on
  104. * machines were long is 32-bit! (However, as time_t is signed, we
  105. * will already get problems at other places on 2038-01-19 03:14:08)
  106. */
  107. unsigned long
  108. mktime (unsigned int year, unsigned int mon,
  109. unsigned int day, unsigned int hour,
  110. unsigned int min, unsigned int sec)
  111. {
  112. if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
  113. mon += 12; /* Puts Feb last since it has leap day */
  114. year -= 1;
  115. }
  116. return (((
  117. (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  118. year*365 - 719499
  119. )*24 + hour /* now have hours */
  120. )*60 + min /* now have minutes */
  121. )*60 + sec; /* finally seconds */
  122. }
  123. #endif