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