00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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