rtc-lib.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /*
  26. * The number of days in the month.
  27. */
  28. int rtc_month_days(unsigned int month, unsigned int year)
  29. {
  30. return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
  31. }
  32. EXPORT_SYMBOL(rtc_month_days);
  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. EXPORT_SYMBOL(rtc_year_days);
  41. /*
  42. * rtc_time_to_tm64 - Converts time64_t to rtc_time.
  43. * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
  44. */
  45. void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
  46. {
  47. unsigned int month, year;
  48. unsigned long secs;
  49. int days;
  50. /* time must be positive */
  51. days = div_s64(time, 86400);
  52. secs = time - (unsigned int) days * 86400;
  53. /* day of the week, 1970-01-01 was a Thursday */
  54. tm->tm_wday = (days + 4) % 7;
  55. year = 1970 + days / 365;
  56. days -= (year - 1970) * 365
  57. + LEAPS_THRU_END_OF(year - 1)
  58. - LEAPS_THRU_END_OF(1970 - 1);
  59. if (days < 0) {
  60. year -= 1;
  61. days += 365 + is_leap_year(year);
  62. }
  63. tm->tm_year = year - 1900;
  64. tm->tm_yday = days + 1;
  65. for (month = 0; month < 11; month++) {
  66. int newdays;
  67. newdays = days - rtc_month_days(month, year);
  68. if (newdays < 0)
  69. break;
  70. days = newdays;
  71. }
  72. tm->tm_mon = month;
  73. tm->tm_mday = days + 1;
  74. tm->tm_hour = secs / 3600;
  75. secs -= tm->tm_hour * 3600;
  76. tm->tm_min = secs / 60;
  77. tm->tm_sec = secs - tm->tm_min * 60;
  78. tm->tm_isdst = 0;
  79. }
  80. EXPORT_SYMBOL(rtc_time64_to_tm);
  81. /*
  82. * Does the rtc_time represent a valid date/time?
  83. */
  84. int rtc_valid_tm(struct rtc_time *tm)
  85. {
  86. if (tm->tm_year < 70
  87. || ((unsigned)tm->tm_mon) >= 12
  88. || tm->tm_mday < 1
  89. || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
  90. || ((unsigned)tm->tm_hour) >= 24
  91. || ((unsigned)tm->tm_min) >= 60
  92. || ((unsigned)tm->tm_sec) >= 60)
  93. return -EINVAL;
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(rtc_valid_tm);
  97. /*
  98. * rtc_tm_to_time64 - Converts rtc_time to time64_t.
  99. * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
  100. */
  101. time64_t rtc_tm_to_time64(struct rtc_time *tm)
  102. {
  103. return mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  104. tm->tm_hour, tm->tm_min, tm->tm_sec);
  105. }
  106. EXPORT_SYMBOL(rtc_tm_to_time64);
  107. /*
  108. * Convert rtc_time to ktime
  109. */
  110. ktime_t rtc_tm_to_ktime(struct rtc_time tm)
  111. {
  112. return ktime_set(rtc_tm_to_time64(&tm), 0);
  113. }
  114. EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
  115. /*
  116. * Convert ktime to rtc_time
  117. */
  118. struct rtc_time rtc_ktime_to_tm(ktime_t kt)
  119. {
  120. struct timespec64 ts;
  121. struct rtc_time ret;
  122. ts = ktime_to_timespec64(kt);
  123. /* Round up any ns */
  124. if (ts.tv_nsec)
  125. ts.tv_sec++;
  126. rtc_time64_to_tm(ts.tv_sec, &ret);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);
  130. MODULE_LICENSE("GPL");