Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 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 |
Cassio Neri | 1d1bb12 | 2021-06-24 21:13:43 +0100 | [diff] [blame] | 9 | * |
| 10 | * Author: Cassio Neri <cassio.neri@gmail.com> (rtc_time64_to_tm) |
Alexandre Belloni | cdf7545 | 2019-03-13 23:02:48 +0100 | [diff] [blame] | 11 | */ |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 12 | |
Paul Gortmaker | 87ebfd6 | 2016-10-31 14:55:25 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 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 | |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 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 | |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 27 | /* |
| 28 | * The number of days in the month. |
| 29 | */ |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 30 | int rtc_month_days(unsigned int month, unsigned int year) |
| 31 | { |
Andrew Morton | 78d89ef | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 32 | return rtc_days_in_month[month] + (is_leap_year(year) && month == 1); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 33 | } |
| 34 | EXPORT_SYMBOL(rtc_month_days); |
| 35 | |
| 36 | /* |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 37 | * The number of days since January 1. (0 to 365) |
| 38 | */ |
| 39 | int rtc_year_days(unsigned int day, unsigned int month, unsigned int year) |
| 40 | { |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 41 | return rtc_ydays[is_leap_year(year)][month] + day - 1; |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 42 | } |
| 43 | EXPORT_SYMBOL(rtc_year_days); |
| 44 | |
Cassio Neri | 1d1bb12 | 2021-06-24 21:13:43 +0100 | [diff] [blame] | 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 | * (Must be positive.) |
| 50 | * @tm: Pointer to the struct rtc_time. |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 51 | */ |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 52 | void rtc_time64_to_tm(time64_t time, struct rtc_time *tm) |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 53 | { |
Cassio Neri | 1d1bb12 | 2021-06-24 21:13:43 +0100 | [diff] [blame] | 54 | unsigned int secs; |
Jan Altenberg | 73442da | 2008-09-02 14:36:05 -0700 | [diff] [blame] | 55 | int days; |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 56 | |
Cassio Neri | 1d1bb12 | 2021-06-24 21:13:43 +0100 | [diff] [blame] | 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 | |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 62 | /* time must be positive */ |
Baolin Wang | 36d46cd | 2017-12-25 19:10:37 +0800 | [diff] [blame] | 63 | days = div_s64_rem(time, 86400, &secs); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 64 | |
| 65 | /* day of the week, 1970-01-01 was a Thursday */ |
| 66 | tm->tm_wday = (days + 4) % 7; |
| 67 | |
Cassio Neri | 1d1bb12 | 2021-06-24 21:13:43 +0100 | [diff] [blame] | 68 | /* |
| 69 | * The following algorithm is, basically, Proposition 6.3 of Neri |
| 70 | * and Schneider [1]. In a few words: it works on the computational |
| 71 | * (fictitious) calendar where the year starts in March, month = 2 |
| 72 | * (*), and finishes in February, month = 13. This calendar is |
| 73 | * mathematically convenient because the day of the year does not |
| 74 | * depend on whether the year is leap or not. For instance: |
| 75 | * |
| 76 | * March 1st 0-th day of the year; |
| 77 | * ... |
| 78 | * April 1st 31-st day of the year; |
| 79 | * ... |
| 80 | * January 1st 306-th day of the year; (Important!) |
| 81 | * ... |
| 82 | * February 28th 364-th day of the year; |
| 83 | * February 29th 365-th day of the year (if it exists). |
| 84 | * |
| 85 | * After having worked out the date in the computational calendar |
| 86 | * (using just arithmetics) it's easy to convert it to the |
| 87 | * corresponding date in the Gregorian calendar. |
| 88 | * |
| 89 | * [1] "Euclidean Affine Functions and Applications to Calendar |
| 90 | * Algorithms". https://arxiv.org/abs/2102.06959 |
| 91 | * |
| 92 | * (*) The numbering of months follows rtc_time more closely and |
| 93 | * thus, is slightly different from [1]. |
| 94 | */ |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 95 | |
Cassio Neri | 1d1bb12 | 2021-06-24 21:13:43 +0100 | [diff] [blame] | 96 | udays = ((u32) days) + 719468; |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 97 | |
Cassio Neri | 1d1bb12 | 2021-06-24 21:13:43 +0100 | [diff] [blame] | 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 | * Recall that January 01 is the 306-th day of the year in the |
| 117 | * computational (not Gregorian) calendar. |
| 118 | */ |
| 119 | is_Jan_or_Feb = day_of_year >= 306; |
| 120 | |
| 121 | /* Converts to the Gregorian calendar. */ |
| 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 | /* Converts to rtc_time's format. */ |
| 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; |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 134 | |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 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; |
Mike Waychison | a7402de | 2011-08-12 21:04:30 +0000 | [diff] [blame] | 139 | |
| 140 | tm->tm_isdst = 0; |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 141 | } |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 142 | EXPORT_SYMBOL(rtc_time64_to_tm); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Does the rtc_time represent a valid date/time? |
| 146 | */ |
| 147 | int rtc_valid_tm(struct rtc_time *tm) |
| 148 | { |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 149 | if (tm->tm_year < 70 || |
Xuefeng Wang | 73f28f7 | 2019-02-15 11:13:59 +0800 | [diff] [blame] | 150 | tm->tm_year > (INT_MAX - 1900) || |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 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) |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 158 | return -EINVAL; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | EXPORT_SYMBOL(rtc_valid_tm); |
| 163 | |
| 164 | /* |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 165 | * rtc_tm_to_time64 - Converts rtc_time to time64_t. |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 166 | * Convert Gregorian date to seconds since 01-01-1970 00:00:00. |
| 167 | */ |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 168 | time64_t rtc_tm_to_time64(struct rtc_time *tm) |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 169 | { |
Alexandre Belloni | 606cc43 | 2019-03-20 12:59:09 +0100 | [diff] [blame] | 170 | return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1, |
ZhangXiaoxu | 074b01a | 2018-12-20 17:36:56 +0800 | [diff] [blame] | 171 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 172 | } |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 173 | EXPORT_SYMBOL(rtc_tm_to_time64); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 174 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 175 | /* |
| 176 | * Convert rtc_time to ktime |
| 177 | */ |
| 178 | ktime_t rtc_tm_to_ktime(struct rtc_time tm) |
| 179 | { |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 180 | return ktime_set(rtc_tm_to_time64(&tm), 0); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 181 | } |
| 182 | EXPORT_SYMBOL_GPL(rtc_tm_to_ktime); |
| 183 | |
| 184 | /* |
| 185 | * Convert ktime to rtc_time |
| 186 | */ |
| 187 | struct rtc_time rtc_ktime_to_tm(ktime_t kt) |
| 188 | { |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 189 | struct timespec64 ts; |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 190 | struct rtc_time ret; |
| 191 | |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 192 | ts = ktime_to_timespec64(kt); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 193 | /* Round up any ns */ |
| 194 | if (ts.tv_nsec) |
| 195 | ts.tv_sec++; |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 196 | rtc_time64_to_tm(ts.tv_sec, &ret); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 197 | return ret; |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(rtc_ktime_to_tm); |