date.c 3.5 KB

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