inport from local
Dependents: Hobbyking_Cheetah_0511
platform/mbed_mktime.c@0:85b3fd62ea1a, 2020-03-16 (annotated)
- Committer:
- NYX
- Date:
- Mon Mar 16 06:35:48 2020 +0000
- Revision:
- 0:85b3fd62ea1a
reinport to mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NYX | 0:85b3fd62ea1a | 1 | /* mbed Microcontroller Library |
NYX | 0:85b3fd62ea1a | 2 | * Copyright (c) 2017-2017 ARM Limited |
NYX | 0:85b3fd62ea1a | 3 | * |
NYX | 0:85b3fd62ea1a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
NYX | 0:85b3fd62ea1a | 5 | * you may not use this file except in compliance with the License. |
NYX | 0:85b3fd62ea1a | 6 | * You may obtain a copy of the License at |
NYX | 0:85b3fd62ea1a | 7 | * |
NYX | 0:85b3fd62ea1a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
NYX | 0:85b3fd62ea1a | 9 | * |
NYX | 0:85b3fd62ea1a | 10 | * Unless required by applicable law or agreed to in writing, software |
NYX | 0:85b3fd62ea1a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
NYX | 0:85b3fd62ea1a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
NYX | 0:85b3fd62ea1a | 13 | * See the License for the specific language governing permissions and |
NYX | 0:85b3fd62ea1a | 14 | * limitations under the License. |
NYX | 0:85b3fd62ea1a | 15 | */ |
NYX | 0:85b3fd62ea1a | 16 | |
NYX | 0:85b3fd62ea1a | 17 | #include "mbed_mktime.h" |
NYX | 0:85b3fd62ea1a | 18 | |
NYX | 0:85b3fd62ea1a | 19 | /* |
NYX | 0:85b3fd62ea1a | 20 | * time constants |
NYX | 0:85b3fd62ea1a | 21 | */ |
NYX | 0:85b3fd62ea1a | 22 | #define SECONDS_BY_MINUTES 60 |
NYX | 0:85b3fd62ea1a | 23 | #define MINUTES_BY_HOUR 60 |
NYX | 0:85b3fd62ea1a | 24 | #define SECONDS_BY_HOUR (SECONDS_BY_MINUTES * MINUTES_BY_HOUR) |
NYX | 0:85b3fd62ea1a | 25 | #define HOURS_BY_DAY 24 |
NYX | 0:85b3fd62ea1a | 26 | #define SECONDS_BY_DAY (SECONDS_BY_HOUR * HOURS_BY_DAY) |
NYX | 0:85b3fd62ea1a | 27 | |
NYX | 0:85b3fd62ea1a | 28 | /* |
NYX | 0:85b3fd62ea1a | 29 | * 2 dimensional array containing the number of seconds elapsed before a given |
NYX | 0:85b3fd62ea1a | 30 | * month. |
NYX | 0:85b3fd62ea1a | 31 | * The second index map to the month while the first map to the type of year: |
NYX | 0:85b3fd62ea1a | 32 | * - 0: non leap year |
NYX | 0:85b3fd62ea1a | 33 | * - 1: leap year |
NYX | 0:85b3fd62ea1a | 34 | */ |
NYX | 0:85b3fd62ea1a | 35 | static const uint32_t seconds_before_month[2][12] = { |
NYX | 0:85b3fd62ea1a | 36 | { |
NYX | 0:85b3fd62ea1a | 37 | 0, |
NYX | 0:85b3fd62ea1a | 38 | 31 * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 39 | (31 + 28) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 40 | (31 + 28 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 41 | (31 + 28 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 42 | (31 + 28 + 31 + 30 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 43 | (31 + 28 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 44 | (31 + 28 + 31 + 30 + 31 + 30 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 45 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 46 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 47 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 48 | (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 49 | }, |
NYX | 0:85b3fd62ea1a | 50 | { |
NYX | 0:85b3fd62ea1a | 51 | 0, |
NYX | 0:85b3fd62ea1a | 52 | 31 * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 53 | (31 + 29) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 54 | (31 + 29 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 55 | (31 + 29 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 56 | (31 + 29 + 31 + 30 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 57 | (31 + 29 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 58 | (31 + 29 + 31 + 30 + 31 + 30 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 59 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 60 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 61 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 62 | (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) * SECONDS_BY_DAY, |
NYX | 0:85b3fd62ea1a | 63 | } |
NYX | 0:85b3fd62ea1a | 64 | }; |
NYX | 0:85b3fd62ea1a | 65 | |
NYX | 0:85b3fd62ea1a | 66 | bool _rtc_is_leap_year(int year) { |
NYX | 0:85b3fd62ea1a | 67 | /* |
NYX | 0:85b3fd62ea1a | 68 | * since in practice, the value manipulated by this algorithm lie in the |
NYX | 0:85b3fd62ea1a | 69 | * range [70 : 138], the algorith can be reduced to: year % 4. |
NYX | 0:85b3fd62ea1a | 70 | * The algorithm valid over the full range of value is: |
NYX | 0:85b3fd62ea1a | 71 | |
NYX | 0:85b3fd62ea1a | 72 | year = 1900 + year; |
NYX | 0:85b3fd62ea1a | 73 | if (year % 4) { |
NYX | 0:85b3fd62ea1a | 74 | return false; |
NYX | 0:85b3fd62ea1a | 75 | } else if (year % 100) { |
NYX | 0:85b3fd62ea1a | 76 | return true; |
NYX | 0:85b3fd62ea1a | 77 | } else if (year % 400) { |
NYX | 0:85b3fd62ea1a | 78 | return false; |
NYX | 0:85b3fd62ea1a | 79 | } |
NYX | 0:85b3fd62ea1a | 80 | return true; |
NYX | 0:85b3fd62ea1a | 81 | |
NYX | 0:85b3fd62ea1a | 82 | */ |
NYX | 0:85b3fd62ea1a | 83 | return (year) % 4 ? false : true; |
NYX | 0:85b3fd62ea1a | 84 | } |
NYX | 0:85b3fd62ea1a | 85 | |
NYX | 0:85b3fd62ea1a | 86 | time_t _rtc_mktime(const struct tm* time) { |
NYX | 0:85b3fd62ea1a | 87 | // partial check for the upper bound of the range |
NYX | 0:85b3fd62ea1a | 88 | // normalization might happen at the end of the function |
NYX | 0:85b3fd62ea1a | 89 | // this solution is faster than checking if the input is after the 19th of |
NYX | 0:85b3fd62ea1a | 90 | // january 2038 at 03:14:07. |
NYX | 0:85b3fd62ea1a | 91 | if ((time->tm_year < 70) || (time->tm_year > 138)) { |
NYX | 0:85b3fd62ea1a | 92 | return ((time_t) -1); |
NYX | 0:85b3fd62ea1a | 93 | } |
NYX | 0:85b3fd62ea1a | 94 | |
NYX | 0:85b3fd62ea1a | 95 | uint32_t result = time->tm_sec; |
NYX | 0:85b3fd62ea1a | 96 | result += time->tm_min * SECONDS_BY_MINUTES; |
NYX | 0:85b3fd62ea1a | 97 | result += time->tm_hour * SECONDS_BY_HOUR; |
NYX | 0:85b3fd62ea1a | 98 | result += (time->tm_mday - 1) * SECONDS_BY_DAY; |
NYX | 0:85b3fd62ea1a | 99 | result += seconds_before_month[_rtc_is_leap_year(time->tm_year)][time->tm_mon]; |
NYX | 0:85b3fd62ea1a | 100 | |
NYX | 0:85b3fd62ea1a | 101 | if (time->tm_year > 70) { |
NYX | 0:85b3fd62ea1a | 102 | // valid in the range [70:138] |
NYX | 0:85b3fd62ea1a | 103 | uint32_t count_of_leap_days = ((time->tm_year - 1) / 4) - (70 / 4); |
NYX | 0:85b3fd62ea1a | 104 | result += (((time->tm_year - 70) * 365) + count_of_leap_days) * SECONDS_BY_DAY; |
NYX | 0:85b3fd62ea1a | 105 | } |
NYX | 0:85b3fd62ea1a | 106 | |
NYX | 0:85b3fd62ea1a | 107 | if (result > INT32_MAX) { |
NYX | 0:85b3fd62ea1a | 108 | return (time_t) -1; |
NYX | 0:85b3fd62ea1a | 109 | } |
NYX | 0:85b3fd62ea1a | 110 | |
NYX | 0:85b3fd62ea1a | 111 | return result; |
NYX | 0:85b3fd62ea1a | 112 | } |
NYX | 0:85b3fd62ea1a | 113 | |
NYX | 0:85b3fd62ea1a | 114 | bool _rtc_localtime(time_t timestamp, struct tm* time_info) { |
NYX | 0:85b3fd62ea1a | 115 | if (((int32_t) timestamp) < 0) { |
NYX | 0:85b3fd62ea1a | 116 | return false; |
NYX | 0:85b3fd62ea1a | 117 | } |
NYX | 0:85b3fd62ea1a | 118 | |
NYX | 0:85b3fd62ea1a | 119 | time_info->tm_sec = timestamp % 60; |
NYX | 0:85b3fd62ea1a | 120 | timestamp = timestamp / 60; // timestamp in minutes |
NYX | 0:85b3fd62ea1a | 121 | time_info->tm_min = timestamp % 60; |
NYX | 0:85b3fd62ea1a | 122 | timestamp = timestamp / 60; // timestamp in hours |
NYX | 0:85b3fd62ea1a | 123 | time_info->tm_hour = timestamp % 24; |
NYX | 0:85b3fd62ea1a | 124 | timestamp = timestamp / 24; // timestamp in days; |
NYX | 0:85b3fd62ea1a | 125 | |
NYX | 0:85b3fd62ea1a | 126 | // compute the weekday |
NYX | 0:85b3fd62ea1a | 127 | // The 1st of January 1970 was a Thursday which is equal to 4 in the weekday |
NYX | 0:85b3fd62ea1a | 128 | // representation ranging from [0:6] |
NYX | 0:85b3fd62ea1a | 129 | time_info->tm_wday = (timestamp + 4) % 7; |
NYX | 0:85b3fd62ea1a | 130 | |
NYX | 0:85b3fd62ea1a | 131 | // years start at 70 |
NYX | 0:85b3fd62ea1a | 132 | time_info->tm_year = 70; |
NYX | 0:85b3fd62ea1a | 133 | while (true) { |
NYX | 0:85b3fd62ea1a | 134 | if (_rtc_is_leap_year(time_info->tm_year) && timestamp >= 366) { |
NYX | 0:85b3fd62ea1a | 135 | ++time_info->tm_year; |
NYX | 0:85b3fd62ea1a | 136 | timestamp -= 366; |
NYX | 0:85b3fd62ea1a | 137 | } else if (!_rtc_is_leap_year(time_info->tm_year) && timestamp >= 365) { |
NYX | 0:85b3fd62ea1a | 138 | ++time_info->tm_year; |
NYX | 0:85b3fd62ea1a | 139 | timestamp -= 365; |
NYX | 0:85b3fd62ea1a | 140 | } else { |
NYX | 0:85b3fd62ea1a | 141 | // the remaining days are less than a years |
NYX | 0:85b3fd62ea1a | 142 | break; |
NYX | 0:85b3fd62ea1a | 143 | } |
NYX | 0:85b3fd62ea1a | 144 | } |
NYX | 0:85b3fd62ea1a | 145 | |
NYX | 0:85b3fd62ea1a | 146 | time_info->tm_yday = timestamp; |
NYX | 0:85b3fd62ea1a | 147 | |
NYX | 0:85b3fd62ea1a | 148 | // convert days into seconds and find the current month |
NYX | 0:85b3fd62ea1a | 149 | timestamp *= SECONDS_BY_DAY; |
NYX | 0:85b3fd62ea1a | 150 | time_info->tm_mon = 11; |
NYX | 0:85b3fd62ea1a | 151 | bool leap = _rtc_is_leap_year(time_info->tm_year); |
NYX | 0:85b3fd62ea1a | 152 | for (uint32_t i = 0; i < 12; ++i) { |
NYX | 0:85b3fd62ea1a | 153 | if ((uint32_t) timestamp < seconds_before_month[leap][i]) { |
NYX | 0:85b3fd62ea1a | 154 | time_info->tm_mon = i - 1; |
NYX | 0:85b3fd62ea1a | 155 | break; |
NYX | 0:85b3fd62ea1a | 156 | } |
NYX | 0:85b3fd62ea1a | 157 | } |
NYX | 0:85b3fd62ea1a | 158 | |
NYX | 0:85b3fd62ea1a | 159 | // remove month from timestamp and compute the number of days. |
NYX | 0:85b3fd62ea1a | 160 | // note: unlike other fields, days are not 0 indexed. |
NYX | 0:85b3fd62ea1a | 161 | timestamp -= seconds_before_month[leap][time_info->tm_mon]; |
NYX | 0:85b3fd62ea1a | 162 | time_info->tm_mday = (timestamp / SECONDS_BY_DAY) + 1; |
NYX | 0:85b3fd62ea1a | 163 | |
NYX | 0:85b3fd62ea1a | 164 | return true; |
NYX | 0:85b3fd62ea1a | 165 | } |