Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * rtc and date/time utility functions |
| 3 | * |
| 4 | * Copyright (C) 2005-06 Tower Technologies |
| 5 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 6 | * |
| 7 | * based on arch/arm/common/rtctime.c and other bits |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
Paul Gortmaker | 87ebfd6 | 2016-10-31 14:55:25 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 15 | #include <linux/rtc.h> |
| 16 | |
| 17 | static const unsigned char rtc_days_in_month[] = { |
| 18 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 19 | }; |
| 20 | |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 21 | static const unsigned short rtc_ydays[2][13] = { |
| 22 | /* Normal years */ |
| 23 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, |
| 24 | /* Leap years */ |
| 25 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } |
| 26 | }; |
| 27 | |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 28 | #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400) |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 29 | |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 30 | /* |
| 31 | * The number of days in the month. |
| 32 | */ |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 33 | int rtc_month_days(unsigned int month, unsigned int year) |
| 34 | { |
Andrew Morton | 78d89ef | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 35 | return rtc_days_in_month[month] + (is_leap_year(year) && month == 1); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 36 | } |
| 37 | EXPORT_SYMBOL(rtc_month_days); |
| 38 | |
| 39 | /* |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 40 | * The number of days since January 1. (0 to 365) |
| 41 | */ |
| 42 | int rtc_year_days(unsigned int day, unsigned int month, unsigned int year) |
| 43 | { |
Andrew Morton | 78d89ef | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 44 | return rtc_ydays[is_leap_year(year)][month] + day-1; |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 45 | } |
| 46 | EXPORT_SYMBOL(rtc_year_days); |
| 47 | |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 48 | |
Andrew Victor | 8232212 | 2006-06-25 05:48:25 -0700 | [diff] [blame] | 49 | /* |
Alexandre Belloni | 0ae3f24 | 2018-09-22 01:22:34 +0200 | [diff] [blame] | 50 | * rtc_time64_to_tm - Converts time64_t to rtc_time. |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 51 | * Convert seconds since 01-01-1970 00:00:00 to Gregorian date. |
| 52 | */ |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 53 | void rtc_time64_to_tm(time64_t time, struct rtc_time *tm) |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 54 | { |
Baolin Wang | 36d46cd | 2017-12-25 19:10:37 +0800 | [diff] [blame] | 55 | unsigned int month, year, secs; |
Jan Altenberg | 73442da | 2008-09-02 14:36:05 -0700 | [diff] [blame] | 56 | int days; |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 57 | |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 58 | /* time must be positive */ |
Baolin Wang | 36d46cd | 2017-12-25 19:10:37 +0800 | [diff] [blame] | 59 | days = div_s64_rem(time, 86400, &secs); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 60 | |
| 61 | /* day of the week, 1970-01-01 was a Thursday */ |
| 62 | tm->tm_wday = (days + 4) % 7; |
| 63 | |
| 64 | year = 1970 + days / 365; |
| 65 | days -= (year - 1970) * 365 |
| 66 | + LEAPS_THRU_END_OF(year - 1) |
| 67 | - LEAPS_THRU_END_OF(1970 - 1); |
Alexandre Belloni | f18046c | 2018-02-28 22:50:53 +0100 | [diff] [blame] | 68 | while (days < 0) { |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 69 | year -= 1; |
Andrew Morton | 78d89ef | 2009-03-31 15:24:48 -0700 | [diff] [blame] | 70 | days += 365 + is_leap_year(year); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 71 | } |
| 72 | tm->tm_year = year - 1900; |
| 73 | tm->tm_yday = days + 1; |
| 74 | |
| 75 | for (month = 0; month < 11; month++) { |
| 76 | int newdays; |
| 77 | |
| 78 | newdays = days - rtc_month_days(month, year); |
| 79 | if (newdays < 0) |
| 80 | break; |
| 81 | days = newdays; |
| 82 | } |
| 83 | tm->tm_mon = month; |
| 84 | tm->tm_mday = days + 1; |
| 85 | |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 86 | tm->tm_hour = secs / 3600; |
| 87 | secs -= tm->tm_hour * 3600; |
| 88 | tm->tm_min = secs / 60; |
| 89 | tm->tm_sec = secs - tm->tm_min * 60; |
Mike Waychison | a7402de | 2011-08-12 21:04:30 +0000 | [diff] [blame] | 90 | |
| 91 | tm->tm_isdst = 0; |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 92 | } |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 93 | EXPORT_SYMBOL(rtc_time64_to_tm); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Does the rtc_time represent a valid date/time? |
| 97 | */ |
| 98 | int rtc_valid_tm(struct rtc_time *tm) |
| 99 | { |
| 100 | if (tm->tm_year < 70 |
David Brownell | db621f1 | 2006-09-30 23:28:16 -0700 | [diff] [blame] | 101 | || ((unsigned)tm->tm_mon) >= 12 |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 102 | || tm->tm_mday < 1 |
| 103 | || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900) |
David Brownell | db621f1 | 2006-09-30 23:28:16 -0700 | [diff] [blame] | 104 | || ((unsigned)tm->tm_hour) >= 24 |
| 105 | || ((unsigned)tm->tm_min) >= 60 |
| 106 | || ((unsigned)tm->tm_sec) >= 60) |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 107 | return -EINVAL; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | EXPORT_SYMBOL(rtc_valid_tm); |
| 112 | |
| 113 | /* |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 114 | * rtc_tm_to_time64 - Converts rtc_time to time64_t. |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 115 | * Convert Gregorian date to seconds since 01-01-1970 00:00:00. |
| 116 | */ |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 117 | time64_t rtc_tm_to_time64(struct rtc_time *tm) |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 118 | { |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 119 | return mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 120 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 121 | } |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 122 | EXPORT_SYMBOL(rtc_tm_to_time64); |
Alessandro Zummo | c58411e | 2006-03-27 01:16:34 -0800 | [diff] [blame] | 123 | |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 124 | /* |
| 125 | * Convert rtc_time to ktime |
| 126 | */ |
| 127 | ktime_t rtc_tm_to_ktime(struct rtc_time tm) |
| 128 | { |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 129 | return ktime_set(rtc_tm_to_time64(&tm), 0); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 130 | } |
| 131 | EXPORT_SYMBOL_GPL(rtc_tm_to_ktime); |
| 132 | |
| 133 | /* |
| 134 | * Convert ktime to rtc_time |
| 135 | */ |
| 136 | struct rtc_time rtc_ktime_to_tm(ktime_t kt) |
| 137 | { |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 138 | struct timespec64 ts; |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 139 | struct rtc_time ret; |
| 140 | |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 141 | ts = ktime_to_timespec64(kt); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 142 | /* Round up any ns */ |
| 143 | if (ts.tv_nsec) |
| 144 | ts.tv_sec++; |
pang.xunlei | c2c11ae | 2014-11-18 19:15:19 +0800 | [diff] [blame] | 145 | rtc_time64_to_tm(ts.tv_sec, &ret); |
John Stultz | 6610e08 | 2010-09-23 15:07:34 -0700 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(rtc_ktime_to_tm); |