date.c 3.1 KB

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