takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_mktime.c Source File

mbed_mktime.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017-2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed_mktime.h"
00018 
00019 /* Time constants. */
00020 #define SECONDS_BY_MINUTES 60
00021 #define MINUTES_BY_HOUR 60
00022 #define SECONDS_BY_HOUR (SECONDS_BY_MINUTES * MINUTES_BY_HOUR)
00023 #define HOURS_BY_DAY 24
00024 #define SECONDS_BY_DAY (SECONDS_BY_HOUR * HOURS_BY_DAY)
00025 #define LAST_VALID_YEAR 206
00026 
00027 /* Macros which will be used to determine if we are within valid range. */
00028 #define EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT 3220095     // 7th of February 1970 at 06:28:15
00029 #define EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT 3133695  // 6th of February 1970 at 06:28:15
00030 
00031 /*
00032  * 2 dimensional array containing the number of seconds elapsed before a given
00033  * month.
00034  * The second index map to the month while the first map to the type of year:
00035  *   - 0: non leap year
00036  *   - 1: leap year
00037  */
00038 static const uint32_t seconds_before_month[2][12] = {
00039     {
00040         0,
00041         31 * SECONDS_BY_DAY,
00042         (31 + 28) *SECONDS_BY_DAY,
00043         (31 + 28 + 31) *SECONDS_BY_DAY,
00044         (31 + 28 + 31 + 30) *SECONDS_BY_DAY,
00045         (31 + 28 + 31 + 30 + 31) *SECONDS_BY_DAY,
00046         (31 + 28 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
00047         (31 + 28 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
00048         (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
00049         (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
00050         (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
00051         (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
00052     },
00053     {
00054         0,
00055         31 * SECONDS_BY_DAY,
00056         (31 + 29) *SECONDS_BY_DAY,
00057         (31 + 29 + 31) *SECONDS_BY_DAY,
00058         (31 + 29 + 31 + 30) *SECONDS_BY_DAY,
00059         (31 + 29 + 31 + 30 + 31) *SECONDS_BY_DAY,
00060         (31 + 29 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
00061         (31 + 29 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
00062         (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
00063         (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
00064         (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
00065         (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
00066     }
00067 };
00068 
00069 bool _rtc_is_leap_year(int year, rtc_leap_year_support_t leap_year_support)
00070 {
00071     /*
00072      * since in practice, the value manipulated by this algorithm lie in the
00073      * range: [70 : 206] the algorithm can be reduced to: year % 4 with exception for 200 (year 2100 is not leap year).
00074      * The algorithm valid over the full range of value is:
00075 
00076         year = 1900 + year;
00077         if (year % 4) {
00078             return false;
00079         } else if (year % 100) {
00080             return true;
00081         } else if (year % 400) {
00082             return false;
00083         }
00084         return true;
00085 
00086      */
00087     if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && year == 200) {
00088         return false; // 2100 is not a leap year
00089     }
00090 
00091     return (year) % 4 ? false : true;
00092 }
00093 
00094 bool _rtc_maketime(const struct tm *time, time_t *seconds, rtc_leap_year_support_t leap_year_support)
00095 {
00096     if (seconds == NULL || time == NULL) {
00097         return false;
00098     }
00099 
00100     /* Partial check for the upper bound of the range - check years only. Full check will be performed after the
00101      * elapsed time since the beginning of the year is calculated.
00102      */
00103     if ((time->tm_year < 70) || (time->tm_year > LAST_VALID_YEAR)) {
00104         return false;
00105     }
00106 
00107     uint32_t result = time->tm_sec;
00108     result += time->tm_min * SECONDS_BY_MINUTES;
00109     result += time->tm_hour * SECONDS_BY_HOUR;
00110     result += (time->tm_mday - 1) * SECONDS_BY_DAY;
00111     result += seconds_before_month[_rtc_is_leap_year(time->tm_year, leap_year_support)][time->tm_mon];
00112 
00113     /* Check if we are within valid range. */
00114     if (time->tm_year == LAST_VALID_YEAR) {
00115         if ((leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT) ||
00116                 (leap_year_support == RTC_4_YEAR_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT)) {
00117             return false;
00118         }
00119     }
00120 
00121     if (time->tm_year > 70) {
00122         /* Valid in the range [70:206]. */
00123         uint32_t count_of_leap_days = ((time->tm_year - 1) / 4) - (70 / 4);
00124         if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT) {
00125             if (time->tm_year > 200) {
00126                 count_of_leap_days--; // 2100 is not a leap year
00127             }
00128         }
00129 
00130         result += (((time->tm_year - 70) * 365) + count_of_leap_days) * SECONDS_BY_DAY;
00131     }
00132 
00133     *seconds = result;
00134 
00135     return true;
00136 }
00137 
00138 bool _rtc_localtime(time_t timestamp, struct tm *time_info, rtc_leap_year_support_t leap_year_support)
00139 {
00140     if (time_info == NULL) {
00141         return false;
00142     }
00143 
00144     uint32_t seconds = (uint32_t)timestamp;
00145 
00146     time_info->tm_sec = seconds % 60;
00147     seconds = seconds / 60;   // timestamp in minutes
00148     time_info->tm_min = seconds % 60;
00149     seconds = seconds / 60;  // timestamp in hours
00150     time_info->tm_hour = seconds % 24;
00151     seconds = seconds / 24;  // timestamp in days;
00152 
00153     /* Compute the weekday.
00154      * The 1st of January 1970 was a Thursday which is equal to 4 in the weekday representation ranging from [0:6].
00155      */
00156     time_info->tm_wday = (seconds + 4) % 7;
00157 
00158     /* Years start at 70. */
00159     time_info->tm_year = 70;
00160     while (true) {
00161         if (_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 366) {
00162             ++time_info->tm_year;
00163             seconds -= 366;
00164         } else if (!_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 365) {
00165             ++time_info->tm_year;
00166             seconds -= 365;
00167         } else {
00168             /* The remaining days are less than a years. */
00169             break;
00170         }
00171     }
00172 
00173     time_info->tm_yday = seconds;
00174 
00175     /* Convert days into seconds and find the current month. */
00176     seconds *= SECONDS_BY_DAY;
00177     time_info->tm_mon = 11;
00178     bool leap = _rtc_is_leap_year(time_info->tm_year, leap_year_support);
00179     for (uint32_t i = 0; i < 12; ++i) {
00180         if ((uint32_t) seconds < seconds_before_month[leap][i]) {
00181             time_info->tm_mon = i - 1;
00182             break;
00183         }
00184     }
00185 
00186     /* Remove month from timestamp and compute the number of days.
00187      * Note: unlike other fields, days are not 0 indexed.
00188      */
00189     seconds -= seconds_before_month[leap][time_info->tm_mon];
00190     time_info->tm_mday = (seconds / SECONDS_BY_DAY) + 1;
00191 
00192     return true;
00193 }