]>
Commit | Line | Data |
---|---|---|
cdf7545a | 1 | // SPDX-License-Identifier: GPL-2.0 |
c58411e9 AZ |
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 | |
cdf7545a | 9 | */ |
c58411e9 | 10 | |
87ebfd64 | 11 | #include <linux/export.h> |
c58411e9 AZ |
12 | #include <linux/rtc.h> |
13 | ||
14 | static const unsigned char rtc_days_in_month[] = { | |
15 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 | |
16 | }; | |
17 | ||
8232212e AV |
18 | static 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 | ||
606cc43c | 25 | #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400) |
c58411e9 | 26 | |
8232212e AV |
27 | /* |
28 | * The number of days in the month. | |
29 | */ | |
c58411e9 AZ |
30 | int rtc_month_days(unsigned int month, unsigned int year) |
31 | { | |
78d89ef4 | 32 | return rtc_days_in_month[month] + (is_leap_year(year) && month == 1); |
c58411e9 AZ |
33 | } |
34 | EXPORT_SYMBOL(rtc_month_days); | |
35 | ||
8232212e AV |
36 | /* |
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 | { | |
606cc43c | 41 | return rtc_ydays[is_leap_year(year)][month] + day - 1; |
8232212e AV |
42 | } |
43 | EXPORT_SYMBOL(rtc_year_days); | |
44 | ||
c58411e9 | 45 | /* |
0ae3f24d | 46 | * rtc_time64_to_tm - Converts time64_t to rtc_time. |
c58411e9 AZ |
47 | * Convert seconds since 01-01-1970 00:00:00 to Gregorian date. |
48 | */ | |
c2c11ae4 | 49 | void rtc_time64_to_tm(time64_t time, struct rtc_time *tm) |
c58411e9 | 50 | { |
36d46cdb | 51 | unsigned int month, year, secs; |
73442daf | 52 | int days; |
c58411e9 | 53 | |
c2c11ae4 | 54 | /* time must be positive */ |
36d46cdb | 55 | days = div_s64_rem(time, 86400, &secs); |
c58411e9 AZ |
56 | |
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); | |
f18046c5 | 64 | while (days < 0) { |
c58411e9 | 65 | year -= 1; |
78d89ef4 | 66 | days += 365 + is_leap_year(year); |
c58411e9 AZ |
67 | } |
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 | ||
c2c11ae4 | 82 | 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; | |
a7402deb MW |
86 | |
87 | tm->tm_isdst = 0; | |
c58411e9 | 88 | } |
c2c11ae4 | 89 | EXPORT_SYMBOL(rtc_time64_to_tm); |
c58411e9 AZ |
90 | |
91 | /* | |
92 | * Does the rtc_time represent a valid date/time? | |
93 | */ | |
94 | int rtc_valid_tm(struct rtc_time *tm) | |
95 | { | |
606cc43c | 96 | if (tm->tm_year < 70 || |
73f28f71 | 97 | tm->tm_year > (INT_MAX - 1900) || |
606cc43c AB |
98 | ((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) | |
c58411e9 AZ |
105 | return -EINVAL; |
106 | ||
107 | return 0; | |
108 | } | |
109 | EXPORT_SYMBOL(rtc_valid_tm); | |
110 | ||
111 | /* | |
c2c11ae4 | 112 | * rtc_tm_to_time64 - Converts rtc_time to time64_t. |
c58411e9 AZ |
113 | * Convert Gregorian date to seconds since 01-01-1970 00:00:00. |
114 | */ | |
c2c11ae4 | 115 | time64_t rtc_tm_to_time64(struct rtc_time *tm) |
c58411e9 | 116 | { |
606cc43c | 117 | return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1, |
074b01a5 | 118 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); |
c58411e9 | 119 | } |
c2c11ae4 | 120 | EXPORT_SYMBOL(rtc_tm_to_time64); |
c58411e9 | 121 | |
6610e089 JS |
122 | /* |
123 | * Convert rtc_time to ktime | |
124 | */ | |
125 | ktime_t rtc_tm_to_ktime(struct rtc_time tm) | |
126 | { | |
c2c11ae4 | 127 | return ktime_set(rtc_tm_to_time64(&tm), 0); |
6610e089 JS |
128 | } |
129 | EXPORT_SYMBOL_GPL(rtc_tm_to_ktime); | |
130 | ||
131 | /* | |
132 | * Convert ktime to rtc_time | |
133 | */ | |
134 | struct rtc_time rtc_ktime_to_tm(ktime_t kt) | |
135 | { | |
c2c11ae4 | 136 | struct timespec64 ts; |
6610e089 JS |
137 | struct rtc_time ret; |
138 | ||
c2c11ae4 | 139 | ts = ktime_to_timespec64(kt); |
6610e089 JS |
140 | /* Round up any ns */ |
141 | if (ts.tv_nsec) | |
142 | ts.tv_sec++; | |
c2c11ae4 | 143 | rtc_time64_to_tm(ts.tv_sec, &ret); |
6610e089 JS |
144 | return ret; |
145 | } | |
146 | EXPORT_SYMBOL_GPL(rtc_ktime_to_tm); |