rtc-lib.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * rtc and date/time utility functions
  3. *
  4. * Copyright (C) 2005-06 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * based on arch/arm/common/rtctime.c and other bits
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <rtc/rtc.h>
  14. static const unsigned char rtc_days_in_month[] =
  15. {
  16. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  17. };
  18. static const unsigned short rtc_ydays[2][13] =
  19. {
  20. /* Normal years */
  21. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  22. /* Leap years */
  23. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  24. };
  25. #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
  26. /*
  27. * The number of days in the month.
  28. */
  29. int rtc_month_days(unsigned int month, unsigned int year)
  30. {
  31. return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
  32. }
  33. /*
  34. * The number of days since January 1. (0 to 365)
  35. */
  36. int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
  37. {
  38. return rtc_ydays[is_leap_year(year)][month] + day - 1;
  39. }
  40. /*
  41. * Does the rtc_time represent a valid date/time?
  42. */
  43. int rtc_valid_tm(struct rtc_time *tm)
  44. {
  45. if (tm->tm_year < 70
  46. || ((unsigned)tm->tm_mon) >= 12
  47. || tm->tm_mday < 1
  48. || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
  49. || ((unsigned)tm->tm_hour) >= 24
  50. || ((unsigned)tm->tm_min) >= 60
  51. || ((unsigned)tm->tm_sec) >= 60)
  52. {
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. /*
  58. * mktime64 - Converts date to seconds.
  59. * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  60. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  61. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  62. *
  63. * [For the Julian calendar (which was used in Russia before 1917,
  64. * Britain & colonies before 1752, anywhere else before 1582,
  65. * and is still in use by some communities) leave out the
  66. * -year/100+year/400 terms, and add 10.]
  67. *
  68. * This algorithm was first published by Gauss (I think).
  69. *
  70. * A leap second can be indicated by calling this function with sec as
  71. * 60 (allowable under ISO 8601). The leap second is treated the same
  72. * as the following second since they don't exist in UNIX time.
  73. *
  74. * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
  75. * tomorrow - (allowable under ISO 8601) is supported.
  76. */
  77. time64_t mktime64(const unsigned int year0, const unsigned int mon0,
  78. const unsigned int day, const unsigned int hour,
  79. const unsigned int min, const unsigned int sec)
  80. {
  81. unsigned int mon = mon0, year = year0;
  82. time64_t diff;
  83. /* 1..12 -> 11,12,1..10 */
  84. if (0 >= (int)(mon -= 2))
  85. {
  86. mon += 12; /* Puts Feb last since it has leap day */
  87. year -= 1;
  88. }
  89. return ((((time64_t)
  90. (year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) +
  91. year * 365 - 719499
  92. ) * 24 + hour /* now have hours - midnight tomorrow handled here */
  93. ) * 60 + min /* now have minutes */
  94. ) * 60 + sec; /* finally seconds */
  95. }
  96. /*
  97. * rtc_tm_to_time64 - Converts rtc_time to time64_t.
  98. * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
  99. */
  100. time64_t rtc_tm_to_time64(struct rtc_time *tm)
  101. {
  102. return mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  103. tm->tm_hour, tm->tm_min, tm->tm_sec);
  104. }
  105. static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
  106. {
  107. *remainder = dividend % divisor;
  108. return dividend / divisor;
  109. }
  110. static s64 div_s64_rem(s64 dividend, s32 divisor, unsigned int *remainder)
  111. {
  112. u64 quotient;
  113. if (dividend < 0)
  114. {
  115. quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
  116. *remainder = -*remainder;
  117. if (divisor > 0)
  118. {
  119. quotient = -quotient;
  120. }
  121. }
  122. else
  123. {
  124. quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
  125. if (divisor < 0)
  126. {
  127. quotient = -quotient;
  128. }
  129. }
  130. return quotient;
  131. }
  132. /*
  133. * rtc_time_to_tm64 - Converts time64_t to rtc_time.
  134. * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
  135. */
  136. void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
  137. {
  138. unsigned int month, year, secs;
  139. int days;
  140. /* time must be positive */
  141. days = div_s64_rem(time, 86400, &secs);
  142. /* day of the week, 1970-01-01 was a Thursday */
  143. tm->tm_wday = (days + 4) % 7;
  144. year = 1970 + days / 365;
  145. days -= (year - 1970) * 365
  146. + LEAPS_THRU_END_OF(year - 1)
  147. - LEAPS_THRU_END_OF(1970 - 1);
  148. if (days < 0)
  149. {
  150. year -= 1;
  151. days += 365 + is_leap_year(year);
  152. }
  153. tm->tm_year = year - 1900;
  154. tm->tm_yday = days + 1;
  155. for (month = 0; month < 11; month++)
  156. {
  157. int newdays;
  158. newdays = days - rtc_month_days(month, year);
  159. if (newdays < 0)
  160. {
  161. break;
  162. }
  163. days = newdays;
  164. }
  165. tm->tm_mon = month;
  166. tm->tm_mday = days + 1;
  167. tm->tm_hour = secs / 3600;
  168. secs -= tm->tm_hour * 3600;
  169. tm->tm_min = secs / 60;
  170. tm->tm_sec = secs - tm->tm_min * 60;
  171. tm->tm_isdst = 0;
  172. }