lib.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. * Author: Cassio Neri <cassio.neri@gmail.com> (rtc_time64_to_tm)
  11. */
  12. #include <linux/export.h>
  13. #include <linux/rtc.h>
  14. static const unsigned char rtc_days_in_month[] = {
  15. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  16. };
  17. static const unsigned short rtc_ydays[2][13] = {
  18. /* Normal years */
  19. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  20. /* Leap years */
  21. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  22. };
  23. /*
  24. * The number of days in the month.
  25. */
  26. int rtc_month_days(unsigned int month, unsigned int year)
  27. {
  28. return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
  29. }
  30. EXPORT_SYMBOL(rtc_month_days);
  31. /*
  32. * The number of days since January 1. (0 to 365)
  33. */
  34. int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
  35. {
  36. return rtc_ydays[is_leap_year(year)][month] + day - 1;
  37. }
  38. EXPORT_SYMBOL(rtc_year_days);
  39. /**
  40. * rtc_time64_to_tm - converts time64_t to rtc_time.
  41. *
  42. * @time: The number of seconds since 01-01-1970 00:00:00.
  43. * Works for values since at least 1900
  44. * @tm: Pointer to the struct rtc_time.
  45. */
  46. void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
  47. {
  48. int secs;
  49. u64 u64tmp;
  50. u32 u32tmp, udays, century, day_of_century, year_of_century, year,
  51. day_of_year, month, day;
  52. bool is_Jan_or_Feb, is_leap_year;
  53. /*
  54. * The time represented by `time` is given in seconds since 1970-01-01
  55. * (UTC). As the division done below might misbehave for negative
  56. * values, we convert it to seconds since 0000-03-01 and then assume it
  57. * will be non-negative.
  58. * Below we do 4 * udays + 3 which should fit into a 32 bit unsigned
  59. * variable. So the latest date this algorithm works for is 1073741823
  60. * days after 0000-03-01 which is in the year 2939805.
  61. */
  62. time += (u64)719468 * 86400;
  63. udays = div_s64_rem(time, 86400, &secs);
  64. /*
  65. * day of the week, 0000-03-01 was a Wednesday (in the proleptic
  66. * Gregorian calendar)
  67. */
  68. tm->tm_wday = (udays + 3) % 7;
  69. /*
  70. * The following algorithm is, basically, Figure 12 of Neri
  71. * and Schneider [1]. In a few words: it works on the computational
  72. * (fictitious) calendar where the year starts in March, month = 2
  73. * (*), and finishes in February, month = 13. This calendar is
  74. * mathematically convenient because the day of the year does not
  75. * depend on whether the year is leap or not. For instance:
  76. *
  77. * March 1st 0-th day of the year;
  78. * ...
  79. * April 1st 31-st day of the year;
  80. * ...
  81. * January 1st 306-th day of the year; (Important!)
  82. * ...
  83. * February 28th 364-th day of the year;
  84. * February 29th 365-th day of the year (if it exists).
  85. *
  86. * After having worked out the date in the computational calendar
  87. * (using just arithmetics) it's easy to convert it to the
  88. * corresponding date in the Gregorian calendar.
  89. *
  90. * [1] Neri C, Schneider L. Euclidean affine functions and their
  91. * application to calendar algorithms. Softw Pract Exper.
  92. * 2023;53(4):937-970. doi: 10.1002/spe.3172
  93. * https://doi.org/10.1002/spe.3172
  94. *
  95. * (*) The numbering of months follows rtc_time more closely and
  96. * thus, is slightly different from [1].
  97. */
  98. u32tmp = 4 * udays + 3;
  99. century = u32tmp / 146097;
  100. day_of_century = u32tmp % 146097 / 4;
  101. u32tmp = 4 * day_of_century + 3;
  102. u64tmp = 2939745ULL * u32tmp;
  103. year_of_century = upper_32_bits(u64tmp);
  104. day_of_year = lower_32_bits(u64tmp) / 2939745 / 4;
  105. year = 100 * century + year_of_century;
  106. is_leap_year = year_of_century != 0 ?
  107. year_of_century % 4 == 0 : century % 4 == 0;
  108. u32tmp = 2141 * day_of_year + 132377;
  109. month = u32tmp >> 16;
  110. day = ((u16) u32tmp) / 2141;
  111. /*
  112. * Recall that January 01 is the 306-th day of the year in the
  113. * computational (not Gregorian) calendar.
  114. */
  115. is_Jan_or_Feb = day_of_year >= 306;
  116. /* Converts to the Gregorian calendar. */
  117. year = year + is_Jan_or_Feb;
  118. month = is_Jan_or_Feb ? month - 12 : month;
  119. day = day + 1;
  120. day_of_year = is_Jan_or_Feb ?
  121. day_of_year - 306 : day_of_year + 31 + 28 + is_leap_year;
  122. /* Converts to rtc_time's format. */
  123. tm->tm_year = (int) (year - 1900);
  124. tm->tm_mon = (int) month;
  125. tm->tm_mday = (int) day;
  126. tm->tm_yday = (int) day_of_year + 1;
  127. tm->tm_hour = secs / 3600;
  128. secs -= tm->tm_hour * 3600;
  129. tm->tm_min = secs / 60;
  130. tm->tm_sec = secs - tm->tm_min * 60;
  131. tm->tm_isdst = 0;
  132. }
  133. EXPORT_SYMBOL(rtc_time64_to_tm);
  134. /*
  135. * Does the rtc_time represent a valid date/time?
  136. */
  137. int rtc_valid_tm(struct rtc_time *tm)
  138. {
  139. if (tm->tm_year < 70 ||
  140. tm->tm_year > (INT_MAX - 1900) ||
  141. ((unsigned int)tm->tm_mon) >= 12 ||
  142. tm->tm_mday < 1 ||
  143. tm->tm_mday > rtc_month_days(tm->tm_mon,
  144. ((unsigned int)tm->tm_year + 1900)) ||
  145. ((unsigned int)tm->tm_hour) >= 24 ||
  146. ((unsigned int)tm->tm_min) >= 60 ||
  147. ((unsigned int)tm->tm_sec) >= 60)
  148. return -EINVAL;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(rtc_valid_tm);
  152. /*
  153. * rtc_tm_to_time64 - Converts rtc_time to time64_t.
  154. * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
  155. */
  156. time64_t rtc_tm_to_time64(struct rtc_time *tm)
  157. {
  158. return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,
  159. tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  160. }
  161. EXPORT_SYMBOL(rtc_tm_to_time64);
  162. /*
  163. * Convert rtc_time to ktime
  164. */
  165. ktime_t rtc_tm_to_ktime(struct rtc_time tm)
  166. {
  167. return ktime_set(rtc_tm_to_time64(&tm), 0);
  168. }
  169. EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
  170. /*
  171. * Convert ktime to rtc_time
  172. */
  173. struct rtc_time rtc_ktime_to_tm(ktime_t kt)
  174. {
  175. struct timespec64 ts;
  176. struct rtc_time ret;
  177. ts = ktime_to_timespec64(kt);
  178. /* Round up any ns */
  179. if (ts.tv_nsec)
  180. ts.tv_sec++;
  181. rtc_time64_to_tm(ts.tv_sec, &ret);
  182. return ret;
  183. }
  184. EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);