1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/export.h>
14#include <linux/rtc.h>
15
16static const unsigned char rtc_days_in_month[] = {
17 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
18};
19
20static const unsigned short rtc_ydays[2][13] = {
21
22 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
23
24 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
25};
26
27
28
29
30int rtc_month_days(unsigned int month, unsigned int year)
31{
32 return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
33}
34EXPORT_SYMBOL(rtc_month_days);
35
36
37
38
39int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
40{
41 return rtc_ydays[is_leap_year(year)][month] + day - 1;
42}
43EXPORT_SYMBOL(rtc_year_days);
44
45
46
47
48
49
50
51
52void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
53{
54 unsigned int secs;
55 int days;
56
57 u64 u64tmp;
58 u32 u32tmp, udays, century, day_of_century, year_of_century, year,
59 day_of_year, month, day;
60 bool is_Jan_or_Feb, is_leap_year;
61
62
63 days = div_s64_rem(time, 86400, &secs);
64
65
66 tm->tm_wday = (days + 4) % 7;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 udays = ((u32) days) + 719468;
97
98 u32tmp = 4 * udays + 3;
99 century = u32tmp / 146097;
100 day_of_century = u32tmp % 146097 / 4;
101
102 u32tmp = 4 * day_of_century + 3;
103 u64tmp = 2939745ULL * u32tmp;
104 year_of_century = upper_32_bits(u64tmp);
105 day_of_year = lower_32_bits(u64tmp) / 2939745 / 4;
106
107 year = 100 * century + year_of_century;
108 is_leap_year = year_of_century != 0 ?
109 year_of_century % 4 == 0 : century % 4 == 0;
110
111 u32tmp = 2141 * day_of_year + 132377;
112 month = u32tmp >> 16;
113 day = ((u16) u32tmp) / 2141;
114
115
116
117
118
119 is_Jan_or_Feb = day_of_year >= 306;
120
121
122 year = year + is_Jan_or_Feb;
123 month = is_Jan_or_Feb ? month - 12 : month;
124 day = day + 1;
125
126 day_of_year = is_Jan_or_Feb ?
127 day_of_year - 306 : day_of_year + 31 + 28 + is_leap_year;
128
129
130 tm->tm_year = (int) (year - 1900);
131 tm->tm_mon = (int) month;
132 tm->tm_mday = (int) day;
133 tm->tm_yday = (int) day_of_year + 1;
134
135 tm->tm_hour = secs / 3600;
136 secs -= tm->tm_hour * 3600;
137 tm->tm_min = secs / 60;
138 tm->tm_sec = secs - tm->tm_min * 60;
139
140 tm->tm_isdst = 0;
141}
142EXPORT_SYMBOL(rtc_time64_to_tm);
143
144
145
146
147int rtc_valid_tm(struct rtc_time *tm)
148{
149 if (tm->tm_year < 70 ||
150 tm->tm_year > (INT_MAX - 1900) ||
151 ((unsigned int)tm->tm_mon) >= 12 ||
152 tm->tm_mday < 1 ||
153 tm->tm_mday > rtc_month_days(tm->tm_mon,
154 ((unsigned int)tm->tm_year + 1900)) ||
155 ((unsigned int)tm->tm_hour) >= 24 ||
156 ((unsigned int)tm->tm_min) >= 60 ||
157 ((unsigned int)tm->tm_sec) >= 60)
158 return -EINVAL;
159
160 return 0;
161}
162EXPORT_SYMBOL(rtc_valid_tm);
163
164
165
166
167
168time64_t rtc_tm_to_time64(struct rtc_time *tm)
169{
170 return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,
171 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
172}
173EXPORT_SYMBOL(rtc_tm_to_time64);
174
175
176
177
178ktime_t rtc_tm_to_ktime(struct rtc_time tm)
179{
180 return ktime_set(rtc_tm_to_time64(&tm), 0);
181}
182EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
183
184
185
186
187struct rtc_time rtc_ktime_to_tm(ktime_t kt)
188{
189 struct timespec64 ts;
190 struct rtc_time ret;
191
192 ts = ktime_to_timespec64(kt);
193
194 if (ts.tv_nsec)
195 ts.tv_sec++;
196 rtc_time64_to_tm(ts.tv_sec, &ret);
197 return ret;
198}
199EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);
200