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