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