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