18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * rtc and date/time utility functions 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2005-06 Tower Technologies 68c2ecf20Sopenharmony_ci * Author: Alessandro Zummo <a.zummo@towertech.it> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * based on arch/arm/common/rtctime.c and other bits 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/export.h> 128c2ecf20Sopenharmony_ci#include <linux/rtc.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_cistatic const unsigned char rtc_days_in_month[] = { 158c2ecf20Sopenharmony_ci 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 168c2ecf20Sopenharmony_ci}; 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic const unsigned short rtc_ydays[2][13] = { 198c2ecf20Sopenharmony_ci /* Normal years */ 208c2ecf20Sopenharmony_ci { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 218c2ecf20Sopenharmony_ci /* Leap years */ 228c2ecf20Sopenharmony_ci { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } 238c2ecf20Sopenharmony_ci}; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400) 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci/* 288c2ecf20Sopenharmony_ci * The number of days in the month. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ciint rtc_month_days(unsigned int month, unsigned int year) 318c2ecf20Sopenharmony_ci{ 328c2ecf20Sopenharmony_ci return rtc_days_in_month[month] + (is_leap_year(year) && month == 1); 338c2ecf20Sopenharmony_ci} 348c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtc_month_days); 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* 378c2ecf20Sopenharmony_ci * The number of days since January 1. (0 to 365) 388c2ecf20Sopenharmony_ci */ 398c2ecf20Sopenharmony_ciint rtc_year_days(unsigned int day, unsigned int month, unsigned int year) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci return rtc_ydays[is_leap_year(year)][month] + day - 1; 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtc_year_days); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* 468c2ecf20Sopenharmony_ci * rtc_time64_to_tm - Converts time64_t to rtc_time. 478c2ecf20Sopenharmony_ci * Convert seconds since 01-01-1970 00:00:00 to Gregorian date. 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_civoid rtc_time64_to_tm(time64_t time, struct rtc_time *tm) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci unsigned int month, year, secs; 528c2ecf20Sopenharmony_ci int days; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* time must be positive */ 558c2ecf20Sopenharmony_ci days = div_s64_rem(time, 86400, &secs); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci /* day of the week, 1970-01-01 was a Thursday */ 588c2ecf20Sopenharmony_ci tm->tm_wday = (days + 4) % 7; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci year = 1970 + days / 365; 618c2ecf20Sopenharmony_ci days -= (year - 1970) * 365 628c2ecf20Sopenharmony_ci + LEAPS_THRU_END_OF(year - 1) 638c2ecf20Sopenharmony_ci - LEAPS_THRU_END_OF(1970 - 1); 648c2ecf20Sopenharmony_ci while (days < 0) { 658c2ecf20Sopenharmony_ci year -= 1; 668c2ecf20Sopenharmony_ci days += 365 + is_leap_year(year); 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci tm->tm_year = year - 1900; 698c2ecf20Sopenharmony_ci tm->tm_yday = days + 1; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci for (month = 0; month < 11; month++) { 728c2ecf20Sopenharmony_ci int newdays; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci newdays = days - rtc_month_days(month, year); 758c2ecf20Sopenharmony_ci if (newdays < 0) 768c2ecf20Sopenharmony_ci break; 778c2ecf20Sopenharmony_ci days = newdays; 788c2ecf20Sopenharmony_ci } 798c2ecf20Sopenharmony_ci tm->tm_mon = month; 808c2ecf20Sopenharmony_ci tm->tm_mday = days + 1; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci tm->tm_hour = secs / 3600; 838c2ecf20Sopenharmony_ci secs -= tm->tm_hour * 3600; 848c2ecf20Sopenharmony_ci tm->tm_min = secs / 60; 858c2ecf20Sopenharmony_ci tm->tm_sec = secs - tm->tm_min * 60; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci tm->tm_isdst = 0; 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtc_time64_to_tm); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* 928c2ecf20Sopenharmony_ci * Does the rtc_time represent a valid date/time? 938c2ecf20Sopenharmony_ci */ 948c2ecf20Sopenharmony_ciint rtc_valid_tm(struct rtc_time *tm) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci if (tm->tm_year < 70 || 978c2ecf20Sopenharmony_ci tm->tm_year > (INT_MAX - 1900) || 988c2ecf20Sopenharmony_ci ((unsigned int)tm->tm_mon) >= 12 || 998c2ecf20Sopenharmony_ci tm->tm_mday < 1 || 1008c2ecf20Sopenharmony_ci tm->tm_mday > rtc_month_days(tm->tm_mon, 1018c2ecf20Sopenharmony_ci ((unsigned int)tm->tm_year + 1900)) || 1028c2ecf20Sopenharmony_ci ((unsigned int)tm->tm_hour) >= 24 || 1038c2ecf20Sopenharmony_ci ((unsigned int)tm->tm_min) >= 60 || 1048c2ecf20Sopenharmony_ci ((unsigned int)tm->tm_sec) >= 60) 1058c2ecf20Sopenharmony_ci return -EINVAL; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci return 0; 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtc_valid_tm); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci/* 1128c2ecf20Sopenharmony_ci * rtc_tm_to_time64 - Converts rtc_time to time64_t. 1138c2ecf20Sopenharmony_ci * Convert Gregorian date to seconds since 01-01-1970 00:00:00. 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_citime64_t rtc_tm_to_time64(struct rtc_time *tm) 1168c2ecf20Sopenharmony_ci{ 1178c2ecf20Sopenharmony_ci return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1, 1188c2ecf20Sopenharmony_ci tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtc_tm_to_time64); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci/* 1238c2ecf20Sopenharmony_ci * Convert rtc_time to ktime 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_ciktime_t rtc_tm_to_ktime(struct rtc_time tm) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci return ktime_set(rtc_tm_to_time64(&tm), 0); 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rtc_tm_to_ktime); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/* 1328c2ecf20Sopenharmony_ci * Convert ktime to rtc_time 1338c2ecf20Sopenharmony_ci */ 1348c2ecf20Sopenharmony_cistruct rtc_time rtc_ktime_to_tm(ktime_t kt) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci struct timespec64 ts; 1378c2ecf20Sopenharmony_ci struct rtc_time ret; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci ts = ktime_to_timespec64(kt); 1408c2ecf20Sopenharmony_ci /* Round up any ns */ 1418c2ecf20Sopenharmony_ci if (ts.tv_nsec) 1428c2ecf20Sopenharmony_ci ts.tv_sec++; 1438c2ecf20Sopenharmony_ci rtc_time64_to_tm(ts.tv_sec, &ret); 1448c2ecf20Sopenharmony_ci return ret; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(rtc_ktime_to_tm); 147