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