1 | /* mbed Microcontroller Library
|
---|
2 | * Copyright (c) 2017-2017 ARM Limited
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include "mbed_mktime.h"
|
---|
18 |
|
---|
19 | /* Time constants. */
|
---|
20 | #define SECONDS_BY_MINUTES 60
|
---|
21 | #define MINUTES_BY_HOUR 60
|
---|
22 | #define SECONDS_BY_HOUR (SECONDS_BY_MINUTES * MINUTES_BY_HOUR)
|
---|
23 | #define HOURS_BY_DAY 24
|
---|
24 | #define SECONDS_BY_DAY (SECONDS_BY_HOUR * HOURS_BY_DAY)
|
---|
25 | #define LAST_VALID_YEAR 206
|
---|
26 |
|
---|
27 | /* Macros which will be used to determine if we are within valid range. */
|
---|
28 | #define EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT 3220095 // 7th of February 1970 at 06:28:15
|
---|
29 | #define EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT 3133695 // 6th of February 1970 at 06:28:15
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * 2 dimensional array containing the number of seconds elapsed before a given
|
---|
33 | * month.
|
---|
34 | * The second index map to the month while the first map to the type of year:
|
---|
35 | * - 0: non leap year
|
---|
36 | * - 1: leap year
|
---|
37 | */
|
---|
38 | static const uint32_t seconds_before_month[2][12] = {
|
---|
39 | {
|
---|
40 | 0,
|
---|
41 | 31 * SECONDS_BY_DAY,
|
---|
42 | (31 + 28) *SECONDS_BY_DAY,
|
---|
43 | (31 + 28 + 31) *SECONDS_BY_DAY,
|
---|
44 | (31 + 28 + 31 + 30) *SECONDS_BY_DAY,
|
---|
45 | (31 + 28 + 31 + 30 + 31) *SECONDS_BY_DAY,
|
---|
46 | (31 + 28 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
|
---|
47 | (31 + 28 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
|
---|
48 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
|
---|
49 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
|
---|
50 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
|
---|
51 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
|
---|
52 | },
|
---|
53 | {
|
---|
54 | 0,
|
---|
55 | 31 * SECONDS_BY_DAY,
|
---|
56 | (31 + 29) *SECONDS_BY_DAY,
|
---|
57 | (31 + 29 + 31) *SECONDS_BY_DAY,
|
---|
58 | (31 + 29 + 31 + 30) *SECONDS_BY_DAY,
|
---|
59 | (31 + 29 + 31 + 30 + 31) *SECONDS_BY_DAY,
|
---|
60 | (31 + 29 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
|
---|
61 | (31 + 29 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
|
---|
62 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
|
---|
63 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
|
---|
64 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
|
---|
65 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
|
---|
66 | }
|
---|
67 | };
|
---|
68 |
|
---|
69 | bool _rtc_is_leap_year(int year, rtc_leap_year_support_t leap_year_support)
|
---|
70 | {
|
---|
71 | /*
|
---|
72 | * since in practice, the value manipulated by this algorithm lie in the
|
---|
73 | * range: [70 : 206] the algorithm can be reduced to: year % 4 with exception for 200 (year 2100 is not leap year).
|
---|
74 | * The algorithm valid over the full range of value is:
|
---|
75 |
|
---|
76 | year = 1900 + year;
|
---|
77 | if (year % 4) {
|
---|
78 | return false;
|
---|
79 | } else if (year % 100) {
|
---|
80 | return true;
|
---|
81 | } else if (year % 400) {
|
---|
82 | return false;
|
---|
83 | }
|
---|
84 | return true;
|
---|
85 |
|
---|
86 | */
|
---|
87 | if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && year == 200) {
|
---|
88 | return false; // 2100 is not a leap year
|
---|
89 | }
|
---|
90 |
|
---|
91 | return (year) % 4 ? false : true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | bool _rtc_maketime(const struct tm *time, time_t *seconds, rtc_leap_year_support_t leap_year_support)
|
---|
95 | {
|
---|
96 | if (seconds == NULL || time == NULL) {
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* Partial check for the upper bound of the range - check years only. Full check will be performed after the
|
---|
101 | * elapsed time since the beginning of the year is calculated.
|
---|
102 | */
|
---|
103 | if ((time->tm_year < 70) || (time->tm_year > LAST_VALID_YEAR)) {
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 |
|
---|
107 | uint32_t result = time->tm_sec;
|
---|
108 | result += time->tm_min * SECONDS_BY_MINUTES;
|
---|
109 | result += time->tm_hour * SECONDS_BY_HOUR;
|
---|
110 | result += (time->tm_mday - 1) * SECONDS_BY_DAY;
|
---|
111 | result += seconds_before_month[_rtc_is_leap_year(time->tm_year, leap_year_support)][time->tm_mon];
|
---|
112 |
|
---|
113 | /* Check if we are within valid range. */
|
---|
114 | if (time->tm_year == LAST_VALID_YEAR) {
|
---|
115 | if ((leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT) ||
|
---|
116 | (leap_year_support == RTC_4_YEAR_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT)) {
|
---|
117 | return false;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (time->tm_year > 70) {
|
---|
122 | /* Valid in the range [70:206]. */
|
---|
123 | uint32_t count_of_leap_days = ((time->tm_year - 1) / 4) - (70 / 4);
|
---|
124 | if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT) {
|
---|
125 | if (time->tm_year > 200) {
|
---|
126 | count_of_leap_days--; // 2100 is not a leap year
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | result += (((time->tm_year - 70) * 365) + count_of_leap_days) * SECONDS_BY_DAY;
|
---|
131 | }
|
---|
132 |
|
---|
133 | *seconds = result;
|
---|
134 |
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool _rtc_localtime(time_t timestamp, struct tm *time_info, rtc_leap_year_support_t leap_year_support)
|
---|
139 | {
|
---|
140 | if (time_info == NULL) {
|
---|
141 | return false;
|
---|
142 | }
|
---|
143 |
|
---|
144 | uint32_t seconds = (uint32_t)timestamp;
|
---|
145 |
|
---|
146 | time_info->tm_sec = seconds % 60;
|
---|
147 | seconds = seconds / 60; // timestamp in minutes
|
---|
148 | time_info->tm_min = seconds % 60;
|
---|
149 | seconds = seconds / 60; // timestamp in hours
|
---|
150 | time_info->tm_hour = seconds % 24;
|
---|
151 | seconds = seconds / 24; // timestamp in days;
|
---|
152 |
|
---|
153 | /* Compute the weekday.
|
---|
154 | * The 1st of January 1970 was a Thursday which is equal to 4 in the weekday representation ranging from [0:6].
|
---|
155 | */
|
---|
156 | time_info->tm_wday = (seconds + 4) % 7;
|
---|
157 |
|
---|
158 | /* Years start at 70. */
|
---|
159 | time_info->tm_year = 70;
|
---|
160 | while (true) {
|
---|
161 | if (_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 366) {
|
---|
162 | ++time_info->tm_year;
|
---|
163 | seconds -= 366;
|
---|
164 | } else if (!_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 365) {
|
---|
165 | ++time_info->tm_year;
|
---|
166 | seconds -= 365;
|
---|
167 | } else {
|
---|
168 | /* The remaining days are less than a years. */
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | time_info->tm_yday = seconds;
|
---|
174 |
|
---|
175 | /* Convert days into seconds and find the current month. */
|
---|
176 | seconds *= SECONDS_BY_DAY;
|
---|
177 | time_info->tm_mon = 11;
|
---|
178 | bool leap = _rtc_is_leap_year(time_info->tm_year, leap_year_support);
|
---|
179 | for (uint32_t i = 0; i < 12; ++i) {
|
---|
180 | if ((uint32_t) seconds < seconds_before_month[leap][i]) {
|
---|
181 | time_info->tm_mon = i - 1;
|
---|
182 | break;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* Remove month from timestamp and compute the number of days.
|
---|
187 | * Note: unlike other fields, days are not 0 indexed.
|
---|
188 | */
|
---|
189 | seconds -= seconds_before_month[leap][time_info->tm_mon];
|
---|
190 | time_info->tm_mday = (seconds / SECONDS_BY_DAY) + 1;
|
---|
191 |
|
---|
192 | return true;
|
---|
193 | }
|
---|