rtc-lib.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
  19. #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
  20. int rtc_month_days(unsigned int month, unsigned int year)
  21. {
  22. return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1);
  23. }
  24. EXPORT_SYMBOL(rtc_month_days);
  25. /*
  26. * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
  27. */
  28. void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
  29. {
  30. register int days, month, year;
  31. days = time / 86400;
  32. time -= days * 86400;
  33. /* day of the week, 1970-01-01 was a Thursday */
  34. tm->tm_wday = (days + 4) % 7;
  35. year = 1970 + days / 365;
  36. days -= (year - 1970) * 365
  37. + LEAPS_THRU_END_OF(year - 1)
  38. - LEAPS_THRU_END_OF(1970 - 1);
  39. if (days < 0) {
  40. year -= 1;
  41. days += 365 + LEAP_YEAR(year);
  42. }
  43. tm->tm_year = year - 1900;
  44. tm->tm_yday = days + 1;
  45. for (month = 0; month < 11; month++) {
  46. int newdays;
  47. newdays = days - rtc_month_days(month, year);
  48. if (newdays < 0)
  49. break;
  50. days = newdays;
  51. }
  52. tm->tm_mon = month;
  53. tm->tm_mday = days + 1;
  54. tm->tm_hour = time / 3600;
  55. time -= tm->tm_hour * 3600;
  56. tm->tm_min = time / 60;
  57. tm->tm_sec = time - tm->tm_min * 60;
  58. }
  59. EXPORT_SYMBOL(rtc_time_to_tm);
  60. /*
  61. * Does the rtc_time represent a valid date/time?
  62. */
  63. int rtc_valid_tm(struct rtc_time *tm)
  64. {
  65. if (tm->tm_year < 70
  66. || tm->tm_mon >= 12
  67. || tm->tm_mday < 1
  68. || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
  69. || tm->tm_hour >= 24
  70. || tm->tm_min >= 60
  71. || tm->tm_sec >= 60)
  72. return -EINVAL;
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(rtc_valid_tm);
  76. /*
  77. * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
  78. */
  79. int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
  80. {
  81. *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  82. tm->tm_hour, tm->tm_min, tm->tm_sec);
  83. return 0;
  84. }
  85. EXPORT_SYMBOL(rtc_tm_to_time);
  86. MODULE_LICENSE("GPL");