blob: 1ae7da5cfc608f6c4bcb3764b00683e2a54b7919 [file] [log] [blame]
Alessandro Zummoc58411e2006-03-27 01:16:34 -08001/*
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 Gortmaker87ebfd62016-10-31 14:55:25 -040014#include <linux/export.h>
Alessandro Zummoc58411e2006-03-27 01:16:34 -080015#include <linux/rtc.h>
16
17static const unsigned char rtc_days_in_month[] = {
18 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
19};
20
Andrew Victor82322122006-06-25 05:48:25 -070021static 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 Zummoc58411e2006-03-27 01:16:34 -080028#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
Alessandro Zummoc58411e2006-03-27 01:16:34 -080029
Andrew Victor82322122006-06-25 05:48:25 -070030/*
31 * The number of days in the month.
32 */
Alessandro Zummoc58411e2006-03-27 01:16:34 -080033int rtc_month_days(unsigned int month, unsigned int year)
34{
Andrew Morton78d89ef2009-03-31 15:24:48 -070035 return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
Alessandro Zummoc58411e2006-03-27 01:16:34 -080036}
37EXPORT_SYMBOL(rtc_month_days);
38
39/*
Andrew Victor82322122006-06-25 05:48:25 -070040 * The number of days since January 1. (0 to 365)
41 */
42int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
43{
Andrew Morton78d89ef2009-03-31 15:24:48 -070044 return rtc_ydays[is_leap_year(year)][month] + day-1;
Andrew Victor82322122006-06-25 05:48:25 -070045}
46EXPORT_SYMBOL(rtc_year_days);
47
pang.xunleic2c11ae2014-11-18 19:15:19 +080048
Andrew Victor82322122006-06-25 05:48:25 -070049/*
pang.xunleic2c11ae2014-11-18 19:15:19 +080050 * rtc_time_to_tm64 - Converts time64_t to rtc_time.
Alessandro Zummoc58411e2006-03-27 01:16:34 -080051 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
52 */
pang.xunleic2c11ae2014-11-18 19:15:19 +080053void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
Alessandro Zummoc58411e2006-03-27 01:16:34 -080054{
Jan Altenberg73442da2008-09-02 14:36:05 -070055 unsigned int month, year;
pang.xunleic2c11ae2014-11-18 19:15:19 +080056 unsigned long secs;
Jan Altenberg73442da2008-09-02 14:36:05 -070057 int days;
Alessandro Zummoc58411e2006-03-27 01:16:34 -080058
pang.xunleic2c11ae2014-11-18 19:15:19 +080059 /* time must be positive */
60 days = div_s64(time, 86400);
61 secs = time - (unsigned int) days * 86400;
Alessandro Zummoc58411e2006-03-27 01:16:34 -080062
63 /* day of the week, 1970-01-01 was a Thursday */
64 tm->tm_wday = (days + 4) % 7;
65
66 year = 1970 + days / 365;
67 days -= (year - 1970) * 365
68 + LEAPS_THRU_END_OF(year - 1)
69 - LEAPS_THRU_END_OF(1970 - 1);
70 if (days < 0) {
71 year -= 1;
Andrew Morton78d89ef2009-03-31 15:24:48 -070072 days += 365 + is_leap_year(year);
Alessandro Zummoc58411e2006-03-27 01:16:34 -080073 }
74 tm->tm_year = year - 1900;
75 tm->tm_yday = days + 1;
76
77 for (month = 0; month < 11; month++) {
78 int newdays;
79
80 newdays = days - rtc_month_days(month, year);
81 if (newdays < 0)
82 break;
83 days = newdays;
84 }
85 tm->tm_mon = month;
86 tm->tm_mday = days + 1;
87
pang.xunleic2c11ae2014-11-18 19:15:19 +080088 tm->tm_hour = secs / 3600;
89 secs -= tm->tm_hour * 3600;
90 tm->tm_min = secs / 60;
91 tm->tm_sec = secs - tm->tm_min * 60;
Mike Waychisona7402de2011-08-12 21:04:30 +000092
93 tm->tm_isdst = 0;
Alessandro Zummoc58411e2006-03-27 01:16:34 -080094}
pang.xunleic2c11ae2014-11-18 19:15:19 +080095EXPORT_SYMBOL(rtc_time64_to_tm);
Alessandro Zummoc58411e2006-03-27 01:16:34 -080096
97/*
98 * Does the rtc_time represent a valid date/time?
99 */
100int rtc_valid_tm(struct rtc_time *tm)
101{
102 if (tm->tm_year < 70
David Brownelldb621f12006-09-30 23:28:16 -0700103 || ((unsigned)tm->tm_mon) >= 12
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800104 || tm->tm_mday < 1
105 || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
David Brownelldb621f12006-09-30 23:28:16 -0700106 || ((unsigned)tm->tm_hour) >= 24
107 || ((unsigned)tm->tm_min) >= 60
108 || ((unsigned)tm->tm_sec) >= 60)
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800109 return -EINVAL;
110
111 return 0;
112}
113EXPORT_SYMBOL(rtc_valid_tm);
114
115/*
pang.xunleic2c11ae2014-11-18 19:15:19 +0800116 * rtc_tm_to_time64 - Converts rtc_time to time64_t.
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800117 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
118 */
pang.xunleic2c11ae2014-11-18 19:15:19 +0800119time64_t rtc_tm_to_time64(struct rtc_time *tm)
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800120{
pang.xunleic2c11ae2014-11-18 19:15:19 +0800121 return mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800122 tm->tm_hour, tm->tm_min, tm->tm_sec);
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800123}
pang.xunleic2c11ae2014-11-18 19:15:19 +0800124EXPORT_SYMBOL(rtc_tm_to_time64);
Alessandro Zummoc58411e2006-03-27 01:16:34 -0800125
John Stultz6610e082010-09-23 15:07:34 -0700126/*
127 * Convert rtc_time to ktime
128 */
129ktime_t rtc_tm_to_ktime(struct rtc_time tm)
130{
pang.xunleic2c11ae2014-11-18 19:15:19 +0800131 return ktime_set(rtc_tm_to_time64(&tm), 0);
John Stultz6610e082010-09-23 15:07:34 -0700132}
133EXPORT_SYMBOL_GPL(rtc_tm_to_ktime);
134
135/*
136 * Convert ktime to rtc_time
137 */
138struct rtc_time rtc_ktime_to_tm(ktime_t kt)
139{
pang.xunleic2c11ae2014-11-18 19:15:19 +0800140 struct timespec64 ts;
John Stultz6610e082010-09-23 15:07:34 -0700141 struct rtc_time ret;
142
pang.xunleic2c11ae2014-11-18 19:15:19 +0800143 ts = ktime_to_timespec64(kt);
John Stultz6610e082010-09-23 15:07:34 -0700144 /* Round up any ns */
145 if (ts.tv_nsec)
146 ts.tv_sec++;
pang.xunleic2c11ae2014-11-18 19:15:19 +0800147 rtc_time64_to_tm(ts.tv_sec, &ret);
John Stultz6610e082010-09-23 15:07:34 -0700148 return ret;
149}
150EXPORT_SYMBOL_GPL(rtc_ktime_to_tm);