mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Apr 19 17:12:19 2018 +0100
Revision:
184:08ed48f1de7f
Parent:
169:e3b6fe271b81
mbed-dev library. Release version 161

Who changed what in which revision?

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