BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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