rtc-lib.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <linux/module.h>
  14. #include <linux/rtc.h>
  15. static const unsigned char rtc_days_in_month[] = {
  16. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  17. };
  18. static const unsigned short rtc_ydays[2][13] = {
  19. /* Normal years */
  20. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  21. /* Leap years */
  22. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  23. };
  24. #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
  25. #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 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] + (LEAP_YEAR(year) && month == 1);
  32. }
  33. EXPORT_SYMBOL(rtc_month_days);
  34. /*
  35. * The number of days since January 1. (0 to 365)
  36. */
  37. int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
  38. {
  39. return rtc_ydays[LEAP_YEAR(year)][month] + day-1;
  40. }
  41. EXPORT_SYMBOL(rtc_year_days);
  42. /*
  43. * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
  44. */
  45. void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
  46. {
  47. register int days, month, year;
  48. days = time / 86400;
  49. time -= days * 86400;
  50. /* day of the week, 1970-01-01 was a Thursday */
  51. tm->tm_wday = (days + 4) % 7;
  52. year = 1970 + days / 365;
  53. days -= (year - 1970) * 365
  54. + LEAPS_THRU_END_OF(year - 1)
  55. - LEAPS_THRU_END_OF(1970 - 1);
  56. if (days < 0) {
  57. year -= 1;
  58. days += 365 + LEAP_YEAR(year);
  59. }
  60. tm->tm_year = year - 1900;
  61. tm->tm_yday = days + 1;
  62. for (month = 0; month < 11; month++) {
  63. int newdays;
  64. newdays = days - rtc_month_days(month, year);
  65. if (newdays < 0)
  66. break;
  67. days = newdays;
  68. }
  69. tm->tm_mon = month;
  70. tm->tm_mday = days + 1;
  71. tm->tm_hour = time / 3600;
  72. time -= tm->tm_hour * 3600;
  73. tm->tm_min = time / 60;
  74. tm->tm_sec = time - tm->tm_min * 60;
  75. }
  76. EXPORT_SYMBOL(rtc_time_to_tm);
  77. /*
  78. * Does the rtc_time represent a valid date/time?
  79. */
  80. int rtc_valid_tm(struct rtc_time *tm)
  81. {
  82. if (tm->tm_year < 70
  83. || ((unsigned)tm->tm_mon) >= 12
  84. || tm->tm_mday < 1
  85. || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
  86. || ((unsigned)tm->tm_hour) >= 24
  87. || ((unsigned)tm->tm_min) >= 60
  88. || ((unsigned)tm->tm_sec) >= 60)
  89. return -EINVAL;
  90. return 0;
  91. }
  92. EXPORT_SYMBOL(rtc_valid_tm);
  93. /*
  94. * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
  95. */
  96. int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
  97. {
  98. *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  99. tm->tm_hour, tm->tm_min, tm->tm_sec);
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(rtc_tm_to_time);
  103. /* Merge the valid (i.e. non-negative) fields of alarm into the current
  104. * time. If the valid alarm fields are earlier than the equivalent
  105. * fields in the time, carry one into the least significant invalid
  106. * field, so that the alarm expiry is in the future. It assumes that the
  107. * least significant invalid field is more significant than the most
  108. * significant valid field, and that the seconds field is valid.
  109. *
  110. * This is used by alarms that take relative (rather than absolute)
  111. * times, and/or have a simple binary second counter instead of
  112. * day/hour/minute/sec registers.
  113. */
  114. void rtc_merge_alarm(struct rtc_time *now, struct rtc_time *alarm)
  115. {
  116. int *alarmp = &alarm->tm_sec;
  117. int *timep = &now->tm_sec;
  118. int carry_into, i;
  119. /* Ignore everything past the 6th element (tm_year). */
  120. for (i = 5; i > 0; i--) {
  121. if (alarmp[i] < 0)
  122. alarmp[i] = timep[i];
  123. else
  124. break;
  125. }
  126. /* No carry needed if all fields are valid. */
  127. if (i == 5)
  128. return;
  129. for (carry_into = i + 1; i >= 0; i--) {
  130. if (alarmp[i] < timep[i])
  131. break;
  132. if (alarmp[i] > timep[i])
  133. return;
  134. }
  135. switch (carry_into) {
  136. case 1:
  137. alarm->tm_min++;
  138. if (alarm->tm_min < 60)
  139. return;
  140. alarm->tm_min = 0;
  141. /* fall-through */
  142. case 2:
  143. alarm->tm_hour++;
  144. if (alarm->tm_hour < 60)
  145. return;
  146. alarm->tm_hour = 0;
  147. /* fall-through */
  148. case 3:
  149. alarm->tm_mday++;
  150. if (alarm->tm_mday <= rtc_days_in_month[alarm->tm_mon])
  151. return;
  152. alarm->tm_mday = 1;
  153. /* fall-through */
  154. case 4:
  155. alarm->tm_mon++;
  156. if (alarm->tm_mon <= 12)
  157. return;
  158. alarm->tm_mon = 1;
  159. /* fall-through */
  160. case 5:
  161. alarm->tm_year++;
  162. }
  163. }
  164. EXPORT_SYMBOL(rtc_merge_alarm);
  165. MODULE_LICENSE("GPL");