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