blob: 23284580df97ae728287fd23d4a8b672e2a2733d [file] [log] [blame]
Alexandre Bellonicdf75452019-03-13 23:02:48 +01001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummoc58411e2006-03-27 01:16:34 -08002/*
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
Alexandre Bellonicdf75452019-03-13 23:02:48 +01009 */
Alessandro Zummoc58411e2006-03-27 01:16:34 -080010
Paul Gortmaker87ebfd62016-10-31 14:55:25 -040011#include <linux/export.h>
Alessandro Zummoc58411e2006-03-27 01:16:34 -080012#include <linux/rtc.h>
13
14static const unsigned char rtc_days_in_month[] = {
15 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
16};
17
Andrew Victor82322122006-06-25 05:48:25 -070018static const unsigned short rtc_ydays[2][13] = {
19 /* Normal years */
20 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
21 /* Leap years */
22 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
23};
24
Alexandre Belloni606cc432019-03-20 12:59:09 +010025#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
Alessandro Zummoc58411e2006-03-27 01:16:34 -080026
Andrew Victor82322122006-06-25 05:48:25 -070027/*
28 * The number of days in the month.
29 */
Alessandro Zummoc58411e2006-03-27 01:16:34 -080030int rtc_month_days(unsigned int month, unsigned int year)
31{
Andrew Morton78d89ef2009-03-31 15:24:48 -070032 return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
Alessandro Zummoc58411e2006-03-27 01:16:34 -080033}
34EXPORT_SYMBOL(rtc_month_days);
35
36/*
Andrew Victor82322122006-06-25 05:48:25 -070037 * The number of days since January 1. (0 to 365)
38 */
39int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
40{
Alexandre Belloni606cc432019-03-20 12:59:09 +010041 return rtc_ydays[is_leap_year(year)][month] + day - 1;
Andrew Victor82322122006-06-25 05:48:25 -070042}
43EXPORT_SYMBOL(rtc_year_days);
44
45/*
Alexandre Belloni0ae3f242018-09-22 01:22:34 +020046 * rtc_time64_to_tm - Converts time64_t to rtc_time.
Alessandro Zummoc58411e2006-03-27 01:16:34 -080047 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
48 */
pang.xunleic2c11ae2014-11-18 19:15:19 +080049void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
Alessandro Zummoc58411e2006-03-27 01:16:34 -080050{
Baolin Wang36d46cd2017-12-25 19:10:37 +080051 unsigned int month, year, secs;
Jan Altenberg73442da2008-09-02 14:36:05 -070052 int days;
Alessandro Zummoc58411e2006-03-27 01:16:34 -080053
pang.xunleic2c11ae2014-11-18 19:15:19 +080054 /* time must be positive */
Baolin Wang36d46cd2017-12-25 19:10:37 +080055 days = div_s64_rem(time, 86400, &secs);
Alessandro Zummoc58411e2006-03-27 01:16:34 -080056
57 /* day of the week, 1970-01-01 was a Thursday */
58 tm->tm_wday = (days + 4) % 7;
59
60 year = 1970 + days / 365;
61 days -= (year - 1970) * 365
62 + LEAPS_THRU_END_OF(year - 1)
63 - LEAPS_THRU_END_OF(1970 - 1);
Alexandre Bellonif18046c2018-02-28 22:50:53 +010064 while (days < 0) {
Alessandro Zummoc58411e2006-03-27 01:16:34 -080065 year -= 1;
Andrew Morton78d89ef2009-03-31 15:24:48 -070066 days += 365 + is_leap_year(year);
Alessandro Zummoc58411e2006-03-27 01:16:34 -080067 }
68 tm->tm_year = year - 1900;
69 tm->tm_yday = days + 1;
70
71 for (month = 0; month < 11; month++) {
72 int newdays;
73
74 newdays = days - rtc_month_days(month, year);
75 if (newdays < 0)
76 break;
77 days = newdays;
78 }
79 tm->tm_mon = month;
80 tm->tm_mday = days + 1;
81
pang.xunleic2c11ae2014-11-18 19:15:19 +080082 tm->tm_hour = secs / 3600;
83 secs -= tm->tm_hour * 3600;
84 tm->tm_min = secs / 60;
85 tm->tm_sec = secs - tm->tm_min * 60;
Mike Waychisona7402de2011-08-12 21:04:30 +000086
87 tm->tm_isdst = 0;
Alessandro Zummoc58411e2006-03-27 01:16:34 -080088}
pang.xunleic2c11ae2014-11-18 19:15:19 +080089EXPORT_SYMBOL(rtc_time64_to_tm);
Alessandro Zummoc58411e2006-03-27 01:16:34 -080090
91/*
92 * Does the rtc_time represent a valid date/time?
93 */
94int rtc_valid_tm(struct rtc_time *tm)
95{
Alexandre Belloni606cc432019-03-20 12:59:09 +010096 if (tm->tm_year < 70 ||
Xuefeng Wang73f28f72019-02-15 11:13:59 +080097 tm->tm_year > (INT_MAX - 1900) ||
Alexandre Belloni606cc432019-03-20 12:59:09 +010098 ((unsigned int)tm->tm_mon) >= 12 ||
99 tm->tm_mday < 1 ||
100 tm->tm_mday > rtc_month_days(tm->tm_mon,
101 ((unsigned int)tm->tm_year + 1900)) ||
102 ((unsigned int)tm->tm_hour) >= 24 ||
103 ((unsigned int)tm->tm_min) >= 60 ||
104 ((unsigned int)tm->tm_sec) >= 60)
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800105 return -EINVAL;
106
107 return 0;
108}
109EXPORT_SYMBOL(rtc_valid_tm);
110
111/*
pang.xunleic2c11ae2014-11-18 19:15:19 +0800112 * rtc_tm_to_time64 - Converts rtc_time to time64_t.
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800113 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
114 */
pang.xunleic2c11ae2014-11-18 19:15:19 +0800115time64_t rtc_tm_to_time64(struct rtc_time *tm)
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800116{
Alexandre Belloni606cc432019-03-20 12:59:09 +0100117 return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,
ZhangXiaoxu074b01a2018-12-20 17:36:56 +0800118 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800119}
pang.xunleic2c11ae2014-11-18 19:15:19 +0800120EXPORT_SYMBOL(rtc_tm_to_time64);
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800121
John Stultz6610e082010-09-23 15:07:34 -0700122/*
123 * Convert rtc_time to ktime
124 */
125ktime_t rtc_tm_to_ktime(struct rtc_time tm)
126{
pang.xunleic2c11ae2014-11-18 19:15:19 +0800127 return ktime_set(rtc_tm_to_time64(&tm), 0);
John Stultz6610e082010-09-23 15:07:34 -0700128}
129EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
130
131/*
132 * Convert ktime to rtc_time
133 */
134struct rtc_time rtc_ktime_to_tm(ktime_t kt)
135{
pang.xunleic2c11ae2014-11-18 19:15:19 +0800136 struct timespec64 ts;
John Stultz6610e082010-09-23 15:07:34 -0700137 struct rtc_time ret;
138
pang.xunleic2c11ae2014-11-18 19:15:19 +0800139 ts = ktime_to_timespec64(kt);
John Stultz6610e082010-09-23 15:07:34 -0700140 /* Round up any ns */
141 if (ts.tv_nsec)
142 ts.tv_sec++;
pang.xunleic2c11ae2014-11-18 19:15:19 +0800143 rtc_time64_to_tm(ts.tv_sec, &ret);
John Stultz6610e082010-09-23 15:07:34 -0700144 return ret;
145}
146EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);