Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_mktime.h Source File

mbed_mktime.h

00001 
00002 /** \addtogroup platform */
00003 /** @{*/
00004 /* mbed Microcontroller Library
00005  * Copyright (c) 2017-2017 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 #ifndef MBED_MKTIME_H
00021 #define MBED_MKTIME_H
00022 
00023 #include <time.h>
00024 #include <stdbool.h>
00025 #include <stdint.h>
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00031 /**
00032  * \defgroup platform_mktime mktime functions
00033  * @{
00034  */
00035 
00036 /* Time range across the whole 32-bit range should be supported which means that years in range 1970 - 2106 can be
00037  * encoded. We have two types of RTC devices:
00038  * a) RTCs which handles all leap years in the mentioned year range correctly. Leap year is determined by checking if
00039  *    the year counter value is divisible by 400, 100, and 4. No problem here.
00040  * b) RTCs which handles leap years correctly up to 2100. The RTC does a simple bit comparison to see if the two
00041  *    lowest order bits of the year counter are zero. In this case 2100 year will be considered
00042  *    incorrectly as a leap year, so the last valid point in time will be 28.02.2100 23:59:59 and next day will be
00043  *    29.02.2100 (invalid). So after 28.02.2100 the day counter will be off by a day.
00044  */
00045 typedef enum {
00046     RTC_FULL_LEAP_YEAR_SUPPORT,
00047     RTC_4_YEAR_LEAP_YEAR_SUPPORT
00048 } rtc_leap_year_support_t;
00049 
00050 /** Compute if a year is a leap year or not.
00051  *
00052  * @param year The year to test it shall be in the range [70:206]. Year 0 is
00053  * translated into year 1900 CE.
00054  * @param leap_year_support use RTC_FULL_LEAP_YEAR_SUPPORT if RTC device is able
00055  * to correctly detect all leap years in range [70:206] otherwise use RTC_4_YEAR_LEAP_YEAR_SUPPORT.
00056  *
00057  * @return true if the year in input is a leap year and false otherwise.
00058  *
00059  * @note For use by the HAL only
00060  * @note Year 2100 is treated differently for devices with full leap year support and devices with
00061  * partial leap year support. Devices with partial leap year support treats 2100 as a leap year.
00062  */
00063 bool _rtc_is_leap_year(int year, rtc_leap_year_support_t leap_year_support);
00064 
00065 /* Convert a calendar time into time since UNIX epoch as a time_t.
00066  *
00067  * This function is a thread safe (partial) replacement for mktime. It is
00068  * tailored around RTC peripherals needs and is not by any mean a complete
00069  * replacement of mktime.
00070  *
00071  * @param time The calendar time to convert into a time_t since epoch.
00072  * The fields from tm used for the computation are:
00073  *   - tm_sec
00074  *   - tm_min
00075  *   - tm_hour
00076  *   - tm_mday
00077  *   - tm_mon
00078  *   - tm_year
00079  * Other fields are ignored and won't be renormalized by a call to this function.
00080  * A valid calendar time is comprised between:
00081  * the 1st of January 1970 at 00:00:00 to the 7th of February 2106 at 06:28:15.
00082  * @param leap_year_support use RTC_FULL_LEAP_YEAR_SUPPORT if RTC device is able
00083  * to correctly detect all leap years in range [70:206] otherwise use RTC_4_YEAR_LEAP_YEAR_SUPPORT.
00084  * @param seconds holder for the result - calendar time as seconds since UNIX epoch.
00085  *
00086  * @return true on success, false if conversion error occurred.
00087  *
00088  * @note Leap seconds are not supported.
00089  * @note Values in output range from 0 to UINT_MAX.
00090  * @note Full and partial leap years support.
00091  * @note For use by the HAL only
00092  */
00093 bool _rtc_maketime(const struct tm* time, time_t * seconds, rtc_leap_year_support_t leap_year_support);
00094 
00095 /* Convert a given time in seconds since epoch into calendar time.
00096  *
00097  * This function is a thread safe (partial) replacement for localtime. It is
00098  * tailored around RTC peripherals specification and is not by any means a
00099  * complete of localtime.
00100  *
00101  * @param timestamp The time (in seconds) to convert into calendar time. Valid
00102  * input are in the range [0 : UINT32_MAX].
00103  * @param calendar_time Pointer to the object which will contain the result of
00104  * the conversion. The tm fields filled by this function are:
00105  *   - tm_sec
00106  *   - tm_min
00107  *   - tm_hour
00108  *   - tm_mday
00109  *   - tm_mon
00110  *   - tm_year
00111  *   - tm_wday
00112  *   - tm_yday
00113  * The object remains untouched if the time in input is invalid.
00114  * @param leap_year_support use RTC_FULL_LEAP_YEAR_SUPPORT if RTC device is able
00115  * to correctly detect all leap years in range [70:206] otherwise use RTC_4_YEAR_LEAP_YEAR_SUPPORT.
00116  * @return true if the conversion was successful, false otherwise.
00117  *
00118  * @note For use by the HAL only.
00119  * @note Full and partial leap years support.
00120  */
00121 bool _rtc_localtime(time_t timestamp, struct tm* time_info, rtc_leap_year_support_t leap_year_support);
00122 
00123 /** @}*/
00124 
00125 #ifdef __cplusplus
00126 }
00127 #endif
00128 
00129 #endif /* MBED_MKTIME_H */
00130 
00131 /** @}*/