lib.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * rtc and date/time utility functions
  4. *
  5. * Copyright (C) 2005-06 Tower Technologies
  6. * Author: Alessandro Zummo <a.zummo@towertech.it>
  7. *
  8. * based on arch/arm/common/rtctime.c and other bits
  9. */
  10. #include <linux/export.h>
  11. #include <linux/rtc.h>
  12. static const unsigned char rtc_days_in_month[] = {
  13. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  14. };
  15. static const unsigned short rtc_ydays[2][13] = {
  16. /* Normal years */
  17. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  18. /* Leap years */
  19. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  20. };
  21. #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
  22. /*
  23. * The number of days in the month.
  24. */
  25. int rtc_month_days(unsigned int month, unsigned int year)
  26. {
  27. return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
  28. }
  29. EXPORT_SYMBOL(rtc_month_days);
  30. /*
  31. * The number of days since January 1. (0 to 365)
  32. */
  33. int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
  34. {
  35. return rtc_ydays[is_leap_year(year)][month] + day - 1;
  36. }
  37. EXPORT_SYMBOL(rtc_year_days);
  38. /*
  39. * rtc_time64_to_tm - Converts time64_t to rtc_time.
  40. * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
  41. */
  42. void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
  43. {
  44. unsigned int month, year, secs;
  45. int days;
  46. /* time must be positive */
  47. days = div_s64_rem(time, 86400, &secs);
  48. /* day of the week, 1970-01-01 was a Thursday */
  49. tm->tm_wday = (days + 4) % 7;
  50. year = 1970 + days / 365;
  51. days -= (year - 1970) * 365
  52. + LEAPS_THRU_END_OF(year - 1)
  53. - LEAPS_THRU_END_OF(1970 - 1);
  54. while (days < 0) {
  55. year -= 1;
  56. days += 365 + is_leap_year(year);
  57. }
  58. tm->tm_year = year - 1900;
  59. tm->tm_yday = days + 1;
  60. for (month = 0; month < 11; month++) {
  61. int newdays;
  62. newdays = days - rtc_month_days(month, year);
  63. if (newdays < 0)
  64. break;
  65. days = newdays;
  66. }
  67. tm->tm_mon = month;
  68. tm->tm_mday = days + 1;
  69. tm->tm_hour = secs / 3600;
  70. secs -= tm->tm_hour * 3600;
  71. tm->tm_min = secs / 60;
  72. tm->tm_sec = secs - tm->tm_min * 60;
  73. tm->tm_isdst = 0;
  74. }
  75. EXPORT_SYMBOL(rtc_time64_to_tm);
  76. /*
  77. * Does the rtc_time represent a valid date/time?
  78. */
  79. int rtc_valid_tm(struct rtc_time *tm)
  80. {
  81. if (tm->tm_year < 70 ||
  82. tm->tm_year > (INT_MAX - 1900) ||
  83. ((unsigned int)tm->tm_mon) >= 12 ||
  84. tm->tm_mday < 1 ||
  85. tm->tm_mday > rtc_month_days(tm->tm_mon,
  86. ((unsigned int)tm->tm_year + 1900)) ||
  87. ((unsigned int)tm->tm_hour) >= 24 ||
  88. ((unsigned int)tm->tm_min) >= 60 ||
  89. ((unsigned int)tm->tm_sec) >= 60)
  90. return -EINVAL;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(rtc_valid_tm);
  94. /*
  95. * rtc_tm_to_time64 - Converts rtc_time to time64_t.
  96. * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
  97. */
  98. time64_t rtc_tm_to_time64(struct rtc_time *tm)
  99. {
  100. return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,
  101. tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  102. }
  103. EXPORT_SYMBOL(rtc_tm_to_time64);
  104. /*
  105. * Convert rtc_time to ktime
  106. */
  107. ktime_t rtc_tm_to_ktime(struct rtc_time tm)
  108. {
  109. return ktime_set(rtc_tm_to_time64(&tm), 0);
  110. }
  111. EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
  112. /*
  113. * Convert ktime to rtc_time
  114. */
  115. struct rtc_time rtc_ktime_to_tm(ktime_t kt)
  116. {
  117. struct timespec64 ts;
  118. struct rtc_time ret;
  119. ts = ktime_to_timespec64(kt);
  120. /* Round up any ns */
  121. if (ts.tv_nsec)
  122. ts.tv_sec++;
  123. rtc_time64_to_tm(ts.tv_sec, &ret);
  124. return ret;
  125. }
  126. EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);