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 
14 #include <rtc/rtc.h>
15 
16 static const unsigned char rtc_days_in_month[] =
17 {
18     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
19 };
20 
21 static const unsigned short rtc_ydays[2][13] =
22 {
23     /* Normal years */
24     { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
25     /* Leap years */
26     { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
27 };
28 
29 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
30 
31 /*
32  * The number of days in the month.
33  */
rtc_month_days(unsigned int month,unsigned int year)34 int rtc_month_days(unsigned int month, unsigned int year)
35 {
36     return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
37 }
38 
39 /*
40  * The number of days since January 1. (0 to 365)
41  */
rtc_year_days(unsigned int day,unsigned int month,unsigned int year)42 int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
43 {
44     return rtc_ydays[is_leap_year(year)][month] + day - 1;
45 }
46 
47 /*
48  * Does the rtc_time represent a valid date/time?
49  */
rtc_valid_tm(struct rtc_time * tm)50 int rtc_valid_tm(struct rtc_time *tm)
51 {
52     if (tm->tm_year < 70
53         || ((unsigned)tm->tm_mon) >= 12
54         || tm->tm_mday < 1
55         || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
56         || ((unsigned)tm->tm_hour) >= 24
57         || ((unsigned)tm->tm_min) >= 60
58         || ((unsigned)tm->tm_sec) >= 60)
59     {
60         return -1;
61     }
62 
63     return 0;
64 }
65 
66 /*
67  * mktime64 - Converts date to seconds.
68  * Converts Gregorian date to seconds since 1970-01-01 00:00:00.
69  * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
70  * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
71  *
72  * [For the Julian calendar (which was used in Russia before 1917,
73  * Britain & colonies before 1752, anywhere else before 1582,
74  * and is still in use by some communities) leave out the
75  * -year/100+year/400 terms, and add 10.]
76  *
77  * This algorithm was first published by Gauss (I think).
78  *
79  * A leap second can be indicated by calling this function with sec as
80  * 60 (allowable under ISO 8601).  The leap second is treated the same
81  * as the following second since they don't exist in UNIX time.
82  *
83  * An encoding of midnight at the end of the day as 24:00:00 - ie. midnight
84  * tomorrow - (allowable under ISO 8601) is supported.
85  */
mktime64(const unsigned int year0,const unsigned int mon0,const unsigned int day,const unsigned int hour,const unsigned int min,const unsigned int sec)86 time64_t mktime64(const unsigned int year0, const unsigned int mon0,
87                   const unsigned int day, const unsigned int hour,
88                   const unsigned int min, const unsigned int sec)
89 {
90     unsigned int mon = mon0, year = year0;
91 
92     time64_t diff;
93     /* 1..12 -> 11,12,1..10 */
94     if (0 >= (int)(mon -= 2))
95     {
96         mon += 12;  /* Puts Feb last since it has leap day */
97         year -= 1;
98     }
99 
100     return ((((time64_t)
101               (year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) +
102               year * 365 - 719499
103              ) * 24 + hour /* now have hours - midnight tomorrow handled here */
104             ) * 60 + min /* now have minutes */
105            ) * 60 + sec; /* finally seconds */
106 }
107 
108 
109 /*
110  * rtc_tm_to_time64 - Converts rtc_time to time64_t.
111  * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
112  */
rtc_tm_to_time64(struct rtc_time * tm)113 time64_t rtc_tm_to_time64(struct rtc_time *tm)
114 {
115     return mktime64(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
116                     tm->tm_hour, tm->tm_min, tm->tm_sec);
117 }
118 
div_u64_rem(u64 dividend,u32 divisor,u32 * remainder)119 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
120 {
121     *remainder = dividend % divisor;
122     return dividend / divisor;
123 }
124 
div_s64_rem(s64 dividend,s32 divisor,unsigned int * remainder)125 static s64 div_s64_rem(s64 dividend, s32 divisor, unsigned int *remainder)
126 {
127     u64 quotient;
128 
129     if (dividend < 0)
130     {
131         quotient = div_u64_rem(-dividend, abs(divisor), (u32 *)remainder);
132         *remainder = -*remainder;
133         if (divisor > 0)
134         {
135             quotient = -quotient;
136         }
137     }
138     else
139     {
140         quotient = div_u64_rem(dividend, abs(divisor), (u32 *)remainder);
141         if (divisor < 0)
142         {
143             quotient = -quotient;
144         }
145     }
146     return quotient;
147 }
148 
149 /*
150  * rtc_time_to_tm64 - Converts time64_t to rtc_time.
151  * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
152  */
rtc_time64_to_tm(time64_t time,struct rtc_time * tm)153 void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
154 {
155     unsigned int month, year, secs;
156     int days;
157 
158     /* time must be positive */
159     days = div_s64_rem(time, 86400, &secs);
160 
161     /* day of the week, 1970-01-01 was a Thursday */
162     tm->tm_wday = (days + 4) % 7;
163 
164     year = 1970 + days / 365;
165     days -= (year - 1970) * 365
166             + LEAPS_THRU_END_OF(year - 1)
167             - LEAPS_THRU_END_OF(1970 - 1);
168     if (days < 0)
169     {
170         year -= 1;
171         days += 365 + is_leap_year(year);
172     }
173     tm->tm_year = year - 1900;
174     tm->tm_yday = days + 1;
175 
176     for (month = 0; month < 11; month++)
177     {
178         int newdays;
179 
180         newdays = days - rtc_month_days(month, year);
181         if (newdays < 0)
182         {
183             break;
184         }
185         days = newdays;
186     }
187     tm->tm_mon = month;
188     tm->tm_mday = days + 1;
189 
190     tm->tm_hour = secs / 3600;
191     secs -= tm->tm_hour * 3600;
192     tm->tm_min = secs / 60;
193     tm->tm_sec = secs - tm->tm_min * 60;
194 
195     tm->tm_isdst = 0;
196 }
197