xref: /openbmc/linux/drivers/rtc/lib.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1cdf7545aSAlexandre Belloni // SPDX-License-Identifier: GPL-2.0
236e14f5fSAlexandre Belloni /*
336e14f5fSAlexandre Belloni  * rtc and date/time utility functions
436e14f5fSAlexandre Belloni  *
536e14f5fSAlexandre Belloni  * Copyright (C) 2005-06 Tower Technologies
636e14f5fSAlexandre Belloni  * Author: Alessandro Zummo <a.zummo@towertech.it>
736e14f5fSAlexandre Belloni  *
836e14f5fSAlexandre Belloni  * based on arch/arm/common/rtctime.c and other bits
9*1d1bb12aSCassio Neri  *
10*1d1bb12aSCassio Neri  * Author: Cassio Neri <cassio.neri@gmail.com> (rtc_time64_to_tm)
1136e14f5fSAlexandre Belloni  */
1236e14f5fSAlexandre Belloni 
1336e14f5fSAlexandre Belloni #include <linux/export.h>
1436e14f5fSAlexandre Belloni #include <linux/rtc.h>
1536e14f5fSAlexandre Belloni 
1636e14f5fSAlexandre Belloni static const unsigned char rtc_days_in_month[] = {
1736e14f5fSAlexandre Belloni 	31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1836e14f5fSAlexandre Belloni };
1936e14f5fSAlexandre Belloni 
2036e14f5fSAlexandre Belloni static const unsigned short rtc_ydays[2][13] = {
2136e14f5fSAlexandre Belloni 	/* Normal years */
2236e14f5fSAlexandre Belloni 	{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
2336e14f5fSAlexandre Belloni 	/* Leap years */
2436e14f5fSAlexandre Belloni 	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
2536e14f5fSAlexandre Belloni };
2636e14f5fSAlexandre Belloni 
2736e14f5fSAlexandre Belloni /*
2836e14f5fSAlexandre Belloni  * The number of days in the month.
2936e14f5fSAlexandre Belloni  */
rtc_month_days(unsigned int month,unsigned int year)3036e14f5fSAlexandre Belloni int rtc_month_days(unsigned int month, unsigned int year)
3136e14f5fSAlexandre Belloni {
3236e14f5fSAlexandre Belloni 	return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
3336e14f5fSAlexandre Belloni }
3436e14f5fSAlexandre Belloni EXPORT_SYMBOL(rtc_month_days);
3536e14f5fSAlexandre Belloni 
3636e14f5fSAlexandre Belloni /*
3736e14f5fSAlexandre Belloni  * The number of days since January 1. (0 to 365)
3836e14f5fSAlexandre Belloni  */
rtc_year_days(unsigned int day,unsigned int month,unsigned int year)3936e14f5fSAlexandre Belloni int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
4036e14f5fSAlexandre Belloni {
4136e14f5fSAlexandre Belloni 	return rtc_ydays[is_leap_year(year)][month] + day - 1;
4236e14f5fSAlexandre Belloni }
4336e14f5fSAlexandre Belloni EXPORT_SYMBOL(rtc_year_days);
4436e14f5fSAlexandre Belloni 
45*1d1bb12aSCassio Neri /**
46*1d1bb12aSCassio Neri  * rtc_time64_to_tm - converts time64_t to rtc_time.
47*1d1bb12aSCassio Neri  *
48*1d1bb12aSCassio Neri  * @time:	The number of seconds since 01-01-1970 00:00:00.
49*1d1bb12aSCassio Neri  *		(Must be positive.)
50*1d1bb12aSCassio Neri  * @tm:		Pointer to the struct rtc_time.
5136e14f5fSAlexandre Belloni  */
rtc_time64_to_tm(time64_t time,struct rtc_time * tm)5236e14f5fSAlexandre Belloni void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
5336e14f5fSAlexandre Belloni {
54*1d1bb12aSCassio Neri 	unsigned int secs;
5536e14f5fSAlexandre Belloni 	int days;
5636e14f5fSAlexandre Belloni 
57*1d1bb12aSCassio Neri 	u64 u64tmp;
58*1d1bb12aSCassio Neri 	u32 u32tmp, udays, century, day_of_century, year_of_century, year,
59*1d1bb12aSCassio Neri 		day_of_year, month, day;
60*1d1bb12aSCassio Neri 	bool is_Jan_or_Feb, is_leap_year;
61*1d1bb12aSCassio Neri 
6236e14f5fSAlexandre Belloni 	/* time must be positive */
6336e14f5fSAlexandre Belloni 	days = div_s64_rem(time, 86400, &secs);
6436e14f5fSAlexandre Belloni 
6536e14f5fSAlexandre Belloni 	/* day of the week, 1970-01-01 was a Thursday */
6636e14f5fSAlexandre Belloni 	tm->tm_wday = (days + 4) % 7;
6736e14f5fSAlexandre Belloni 
68*1d1bb12aSCassio Neri 	/*
69*1d1bb12aSCassio Neri 	 * The following algorithm is, basically, Proposition 6.3 of Neri
70*1d1bb12aSCassio Neri 	 * and Schneider [1]. In a few words: it works on the computational
71*1d1bb12aSCassio Neri 	 * (fictitious) calendar where the year starts in March, month = 2
72*1d1bb12aSCassio Neri 	 * (*), and finishes in February, month = 13. This calendar is
73*1d1bb12aSCassio Neri 	 * mathematically convenient because the day of the year does not
74*1d1bb12aSCassio Neri 	 * depend on whether the year is leap or not. For instance:
75*1d1bb12aSCassio Neri 	 *
76*1d1bb12aSCassio Neri 	 * March 1st		0-th day of the year;
77*1d1bb12aSCassio Neri 	 * ...
78*1d1bb12aSCassio Neri 	 * April 1st		31-st day of the year;
79*1d1bb12aSCassio Neri 	 * ...
80*1d1bb12aSCassio Neri 	 * January 1st		306-th day of the year; (Important!)
81*1d1bb12aSCassio Neri 	 * ...
82*1d1bb12aSCassio Neri 	 * February 28th	364-th day of the year;
83*1d1bb12aSCassio Neri 	 * February 29th	365-th day of the year (if it exists).
84*1d1bb12aSCassio Neri 	 *
85*1d1bb12aSCassio Neri 	 * After having worked out the date in the computational calendar
86*1d1bb12aSCassio Neri 	 * (using just arithmetics) it's easy to convert it to the
87*1d1bb12aSCassio Neri 	 * corresponding date in the Gregorian calendar.
88*1d1bb12aSCassio Neri 	 *
89*1d1bb12aSCassio Neri 	 * [1] "Euclidean Affine Functions and Applications to Calendar
90*1d1bb12aSCassio Neri 	 * Algorithms". https://arxiv.org/abs/2102.06959
91*1d1bb12aSCassio Neri 	 *
92*1d1bb12aSCassio Neri 	 * (*) The numbering of months follows rtc_time more closely and
93*1d1bb12aSCassio Neri 	 * thus, is slightly different from [1].
94*1d1bb12aSCassio Neri 	 */
9536e14f5fSAlexandre Belloni 
96*1d1bb12aSCassio Neri 	udays		= ((u32) days) + 719468;
9736e14f5fSAlexandre Belloni 
98*1d1bb12aSCassio Neri 	u32tmp		= 4 * udays + 3;
99*1d1bb12aSCassio Neri 	century		= u32tmp / 146097;
100*1d1bb12aSCassio Neri 	day_of_century	= u32tmp % 146097 / 4;
101*1d1bb12aSCassio Neri 
102*1d1bb12aSCassio Neri 	u32tmp		= 4 * day_of_century + 3;
103*1d1bb12aSCassio Neri 	u64tmp		= 2939745ULL * u32tmp;
104*1d1bb12aSCassio Neri 	year_of_century	= upper_32_bits(u64tmp);
105*1d1bb12aSCassio Neri 	day_of_year	= lower_32_bits(u64tmp) / 2939745 / 4;
106*1d1bb12aSCassio Neri 
107*1d1bb12aSCassio Neri 	year		= 100 * century + year_of_century;
108*1d1bb12aSCassio Neri 	is_leap_year	= year_of_century != 0 ?
109*1d1bb12aSCassio Neri 		year_of_century % 4 == 0 : century % 4 == 0;
110*1d1bb12aSCassio Neri 
111*1d1bb12aSCassio Neri 	u32tmp		= 2141 * day_of_year + 132377;
112*1d1bb12aSCassio Neri 	month		= u32tmp >> 16;
113*1d1bb12aSCassio Neri 	day		= ((u16) u32tmp) / 2141;
114*1d1bb12aSCassio Neri 
115*1d1bb12aSCassio Neri 	/*
116*1d1bb12aSCassio Neri 	 * Recall that January 01 is the 306-th day of the year in the
117*1d1bb12aSCassio Neri 	 * computational (not Gregorian) calendar.
118*1d1bb12aSCassio Neri 	 */
119*1d1bb12aSCassio Neri 	is_Jan_or_Feb	= day_of_year >= 306;
120*1d1bb12aSCassio Neri 
121*1d1bb12aSCassio Neri 	/* Converts to the Gregorian calendar. */
122*1d1bb12aSCassio Neri 	year		= year + is_Jan_or_Feb;
123*1d1bb12aSCassio Neri 	month		= is_Jan_or_Feb ? month - 12 : month;
124*1d1bb12aSCassio Neri 	day		= day + 1;
125*1d1bb12aSCassio Neri 
126*1d1bb12aSCassio Neri 	day_of_year	= is_Jan_or_Feb ?
127*1d1bb12aSCassio Neri 		day_of_year - 306 : day_of_year + 31 + 28 + is_leap_year;
128*1d1bb12aSCassio Neri 
129*1d1bb12aSCassio Neri 	/* Converts to rtc_time's format. */
130*1d1bb12aSCassio Neri 	tm->tm_year	= (int) (year - 1900);
131*1d1bb12aSCassio Neri 	tm->tm_mon	= (int) month;
132*1d1bb12aSCassio Neri 	tm->tm_mday	= (int) day;
133*1d1bb12aSCassio Neri 	tm->tm_yday	= (int) day_of_year + 1;
13436e14f5fSAlexandre Belloni 
13536e14f5fSAlexandre Belloni 	tm->tm_hour = secs / 3600;
13636e14f5fSAlexandre Belloni 	secs -= tm->tm_hour * 3600;
13736e14f5fSAlexandre Belloni 	tm->tm_min = secs / 60;
13836e14f5fSAlexandre Belloni 	tm->tm_sec = secs - tm->tm_min * 60;
13936e14f5fSAlexandre Belloni 
14036e14f5fSAlexandre Belloni 	tm->tm_isdst = 0;
14136e14f5fSAlexandre Belloni }
14236e14f5fSAlexandre Belloni EXPORT_SYMBOL(rtc_time64_to_tm);
14336e14f5fSAlexandre Belloni 
14436e14f5fSAlexandre Belloni /*
14536e14f5fSAlexandre Belloni  * Does the rtc_time represent a valid date/time?
14636e14f5fSAlexandre Belloni  */
rtc_valid_tm(struct rtc_time * tm)14736e14f5fSAlexandre Belloni int rtc_valid_tm(struct rtc_time *tm)
14836e14f5fSAlexandre Belloni {
149606cc43cSAlexandre Belloni 	if (tm->tm_year < 70 ||
15073f28f71SXuefeng Wang 	    tm->tm_year > (INT_MAX - 1900) ||
151606cc43cSAlexandre Belloni 	    ((unsigned int)tm->tm_mon) >= 12 ||
152606cc43cSAlexandre Belloni 	    tm->tm_mday < 1 ||
153606cc43cSAlexandre Belloni 	    tm->tm_mday > rtc_month_days(tm->tm_mon,
154606cc43cSAlexandre Belloni 					 ((unsigned int)tm->tm_year + 1900)) ||
155606cc43cSAlexandre Belloni 	    ((unsigned int)tm->tm_hour) >= 24 ||
156606cc43cSAlexandre Belloni 	    ((unsigned int)tm->tm_min) >= 60 ||
157606cc43cSAlexandre Belloni 	    ((unsigned int)tm->tm_sec) >= 60)
15836e14f5fSAlexandre Belloni 		return -EINVAL;
15936e14f5fSAlexandre Belloni 
16036e14f5fSAlexandre Belloni 	return 0;
16136e14f5fSAlexandre Belloni }
16236e14f5fSAlexandre Belloni EXPORT_SYMBOL(rtc_valid_tm);
16336e14f5fSAlexandre Belloni 
16436e14f5fSAlexandre Belloni /*
16536e14f5fSAlexandre Belloni  * rtc_tm_to_time64 - Converts rtc_time to time64_t.
16636e14f5fSAlexandre Belloni  * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
16736e14f5fSAlexandre Belloni  */
rtc_tm_to_time64(struct rtc_time * tm)16836e14f5fSAlexandre Belloni time64_t rtc_tm_to_time64(struct rtc_time *tm)
16936e14f5fSAlexandre Belloni {
170606cc43cSAlexandre Belloni 	return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,
171074b01a5SZhangXiaoxu 			tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
17236e14f5fSAlexandre Belloni }
17336e14f5fSAlexandre Belloni EXPORT_SYMBOL(rtc_tm_to_time64);
17436e14f5fSAlexandre Belloni 
17536e14f5fSAlexandre Belloni /*
17636e14f5fSAlexandre Belloni  * Convert rtc_time to ktime
17736e14f5fSAlexandre Belloni  */
rtc_tm_to_ktime(struct rtc_time tm)17836e14f5fSAlexandre Belloni ktime_t rtc_tm_to_ktime(struct rtc_time tm)
17936e14f5fSAlexandre Belloni {
18036e14f5fSAlexandre Belloni 	return ktime_set(rtc_tm_to_time64(&tm), 0);
18136e14f5fSAlexandre Belloni }
18236e14f5fSAlexandre Belloni EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
18336e14f5fSAlexandre Belloni 
18436e14f5fSAlexandre Belloni /*
18536e14f5fSAlexandre Belloni  * Convert ktime to rtc_time
18636e14f5fSAlexandre Belloni  */
rtc_ktime_to_tm(ktime_t kt)18736e14f5fSAlexandre Belloni struct rtc_time rtc_ktime_to_tm(ktime_t kt)
18836e14f5fSAlexandre Belloni {
18936e14f5fSAlexandre Belloni 	struct timespec64 ts;
19036e14f5fSAlexandre Belloni 	struct rtc_time ret;
19136e14f5fSAlexandre Belloni 
19236e14f5fSAlexandre Belloni 	ts = ktime_to_timespec64(kt);
19336e14f5fSAlexandre Belloni 	/* Round up any ns */
19436e14f5fSAlexandre Belloni 	if (ts.tv_nsec)
19536e14f5fSAlexandre Belloni 		ts.tv_sec++;
19636e14f5fSAlexandre Belloni 	rtc_time64_to_tm(ts.tv_sec, &ret);
19736e14f5fSAlexandre Belloni 	return ret;
19836e14f5fSAlexandre Belloni }
19936e14f5fSAlexandre Belloni EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);
200