1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * rtc and date/time utility functions
4 *
5 * Copyright (C) 2005-06 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 *
8 * based on arch/arm/common/rtctime.c and other bits
9 *
10 * Author: Cassio Neri <cassio.neri@gmail.com> (rtc_time64_to_tm)
11 */
12
13 #include <linux/export.h>
14 #include <linux/rtc.h>
15
16 static const unsigned char rtc_days_in_month[] = {
17 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
18 };
19
20 static const unsigned short rtc_ydays[2][13] = {
21 /* Normal years */
22 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
23 /* Leap years */
24 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
25 };
26
27 /*
28 * The number of days in the month.
29 */
rtc_month_days(unsigned int month,unsigned int year)30 int rtc_month_days(unsigned int month, unsigned int year)
31 {
32 return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
33 }
34 EXPORT_SYMBOL(rtc_month_days);
35
36 /*
37 * The number of days since January 1. (0 to 365)
38 */
rtc_year_days(unsigned int day,unsigned int month,unsigned int year)39 int 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 }
43 EXPORT_SYMBOL(rtc_year_days);
44
45 /**
46 * rtc_time64_to_tm - converts time64_t to rtc_time.
47 *
48 * @time: The number of seconds since 01-01-1970 00:00:00.
49 * Works for values since at least 1900
50 * @tm: Pointer to the struct rtc_time.
51 */
rtc_time64_to_tm(time64_t time,struct rtc_time * tm)52 void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
53 {
54 int secs;
55
56 u64 u64tmp;
57 u32 u32tmp, udays, century, day_of_century, year_of_century, year,
58 day_of_year, month, day;
59 bool is_Jan_or_Feb, is_leap_year;
60
61 /*
62 * The time represented by `time` is given in seconds since 1970-01-01
63 * (UTC). As the division done below might misbehave for negative
64 * values, we convert it to seconds since 0000-03-01 and then assume it
65 * will be non-negative.
66 * Below we do 4 * udays + 3 which should fit into a 32 bit unsigned
67 * variable. So the latest date this algorithm works for is 1073741823
68 * days after 0000-03-01 which is in the year 2939805.
69 */
70 time += (u64)719468 * 86400;
71
72 udays = div_s64_rem(time, 86400, &secs);
73
74 /*
75 * day of the week, 0000-03-01 was a Wednesday (in the proleptic
76 * Gregorian calendar)
77 */
78 tm->tm_wday = (udays + 3) % 7;
79
80 /*
81 * The following algorithm is, basically, Figure 12 of Neri
82 * and Schneider [1]. In a few words: it works on the computational
83 * (fictitious) calendar where the year starts in March, month = 2
84 * (*), and finishes in February, month = 13. This calendar is
85 * mathematically convenient because the day of the year does not
86 * depend on whether the year is leap or not. For instance:
87 *
88 * March 1st 0-th day of the year;
89 * ...
90 * April 1st 31-st day of the year;
91 * ...
92 * January 1st 306-th day of the year; (Important!)
93 * ...
94 * February 28th 364-th day of the year;
95 * February 29th 365-th day of the year (if it exists).
96 *
97 * After having worked out the date in the computational calendar
98 * (using just arithmetics) it's easy to convert it to the
99 * corresponding date in the Gregorian calendar.
100 *
101 * [1] Neri C, Schneider L. Euclidean affine functions and their
102 * application to calendar algorithms. Softw Pract Exper.
103 * 2023;53(4):937-970. doi: 10.1002/spe.3172
104 * https://doi.org/10.1002/spe.3172
105 *
106 * (*) The numbering of months follows rtc_time more closely and
107 * thus, is slightly different from [1].
108 */
109
110 u32tmp = 4 * udays + 3;
111 century = u32tmp / 146097;
112 day_of_century = u32tmp % 146097 / 4;
113
114 u32tmp = 4 * day_of_century + 3;
115 u64tmp = 2939745ULL * u32tmp;
116 year_of_century = upper_32_bits(u64tmp);
117 day_of_year = lower_32_bits(u64tmp) / 2939745 / 4;
118
119 year = 100 * century + year_of_century;
120 is_leap_year = year_of_century != 0 ?
121 year_of_century % 4 == 0 : century % 4 == 0;
122
123 u32tmp = 2141 * day_of_year + 132377;
124 month = u32tmp >> 16;
125 day = ((u16) u32tmp) / 2141;
126
127 /*
128 * Recall that January 01 is the 306-th day of the year in the
129 * computational (not Gregorian) calendar.
130 */
131 is_Jan_or_Feb = day_of_year >= 306;
132
133 /* Converts to the Gregorian calendar. */
134 year = year + is_Jan_or_Feb;
135 month = is_Jan_or_Feb ? month - 12 : month;
136 day = day + 1;
137
138 day_of_year = is_Jan_or_Feb ?
139 day_of_year - 306 : day_of_year + 31 + 28 + is_leap_year;
140
141 /* Converts to rtc_time's format. */
142 tm->tm_year = (int) (year - 1900);
143 tm->tm_mon = (int) month;
144 tm->tm_mday = (int) day;
145 tm->tm_yday = (int) day_of_year + 1;
146
147 tm->tm_hour = secs / 3600;
148 secs -= tm->tm_hour * 3600;
149 tm->tm_min = secs / 60;
150 tm->tm_sec = secs - tm->tm_min * 60;
151
152 tm->tm_isdst = 0;
153 }
154 EXPORT_SYMBOL(rtc_time64_to_tm);
155
156 /*
157 * Does the rtc_time represent a valid date/time?
158 */
rtc_valid_tm(struct rtc_time * tm)159 int rtc_valid_tm(struct rtc_time *tm)
160 {
161 if (tm->tm_year < 70 ||
162 tm->tm_year > (INT_MAX - 1900) ||
163 ((unsigned int)tm->tm_mon) >= 12 ||
164 tm->tm_mday < 1 ||
165 tm->tm_mday > rtc_month_days(tm->tm_mon,
166 ((unsigned int)tm->tm_year + 1900)) ||
167 ((unsigned int)tm->tm_hour) >= 24 ||
168 ((unsigned int)tm->tm_min) >= 60 ||
169 ((unsigned int)tm->tm_sec) >= 60)
170 return -EINVAL;
171
172 return 0;
173 }
174 EXPORT_SYMBOL(rtc_valid_tm);
175
176 /*
177 * rtc_tm_to_time64 - Converts rtc_time to time64_t.
178 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
179 */
rtc_tm_to_time64(struct rtc_time * tm)180 time64_t rtc_tm_to_time64(struct rtc_time *tm)
181 {
182 return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,
183 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
184 }
185 EXPORT_SYMBOL(rtc_tm_to_time64);
186
187 /*
188 * Convert rtc_time to ktime
189 */
rtc_tm_to_ktime(struct rtc_time tm)190 ktime_t rtc_tm_to_ktime(struct rtc_time tm)
191 {
192 return ktime_set(rtc_tm_to_time64(&tm), 0);
193 }
194 EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
195
196 /*
197 * Convert ktime to rtc_time
198 */
rtc_ktime_to_tm(ktime_t kt)199 struct rtc_time rtc_ktime_to_tm(ktime_t kt)
200 {
201 struct timespec64 ts;
202 struct rtc_time ret;
203
204 ts = ktime_to_timespec64(kt);
205 /* Round up any ns */
206 if (ts.tv_nsec)
207 ts.tv_sec++;
208 rtc_time64_to_tm(ts.tv_sec, &ret);
209 return ret;
210 }
211 EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);
212