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