date.c 3.0 KB

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