Nicolas Borla
/
BBR_1Ebene
BBR 1 Ebene
mbed-os/platform/mbed_mktime.h@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | |
borlanic | 0:fbdae7e6d805 | 2 | /** \addtogroup platform */ |
borlanic | 0:fbdae7e6d805 | 3 | /** @{*/ |
borlanic | 0:fbdae7e6d805 | 4 | /* mbed Microcontroller Library |
borlanic | 0:fbdae7e6d805 | 5 | * Copyright (c) 2017-2017 ARM Limited |
borlanic | 0:fbdae7e6d805 | 6 | * |
borlanic | 0:fbdae7e6d805 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:fbdae7e6d805 | 8 | * you may not use this file except in compliance with the License. |
borlanic | 0:fbdae7e6d805 | 9 | * You may obtain a copy of the License at |
borlanic | 0:fbdae7e6d805 | 10 | * |
borlanic | 0:fbdae7e6d805 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:fbdae7e6d805 | 12 | * |
borlanic | 0:fbdae7e6d805 | 13 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:fbdae7e6d805 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:fbdae7e6d805 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:fbdae7e6d805 | 16 | * See the License for the specific language governing permissions and |
borlanic | 0:fbdae7e6d805 | 17 | * limitations under the License. |
borlanic | 0:fbdae7e6d805 | 18 | */ |
borlanic | 0:fbdae7e6d805 | 19 | |
borlanic | 0:fbdae7e6d805 | 20 | #ifndef MBED_MKTIME_H |
borlanic | 0:fbdae7e6d805 | 21 | #define MBED_MKTIME_H |
borlanic | 0:fbdae7e6d805 | 22 | |
borlanic | 0:fbdae7e6d805 | 23 | #include <time.h> |
borlanic | 0:fbdae7e6d805 | 24 | #include <stdbool.h> |
borlanic | 0:fbdae7e6d805 | 25 | #include <stdint.h> |
borlanic | 0:fbdae7e6d805 | 26 | |
borlanic | 0:fbdae7e6d805 | 27 | #ifdef __cplusplus |
borlanic | 0:fbdae7e6d805 | 28 | extern "C" { |
borlanic | 0:fbdae7e6d805 | 29 | #endif |
borlanic | 0:fbdae7e6d805 | 30 | |
borlanic | 0:fbdae7e6d805 | 31 | /** |
borlanic | 0:fbdae7e6d805 | 32 | * \defgroup platform_mktime mktime functions |
borlanic | 0:fbdae7e6d805 | 33 | * @{ |
borlanic | 0:fbdae7e6d805 | 34 | */ |
borlanic | 0:fbdae7e6d805 | 35 | |
borlanic | 0:fbdae7e6d805 | 36 | /* Time range across the whole 32-bit range should be supported which means that years in range 1970 - 2106 can be |
borlanic | 0:fbdae7e6d805 | 37 | * encoded. We have two types of RTC devices: |
borlanic | 0:fbdae7e6d805 | 38 | * a) RTCs which handles all leap years in the mentioned year range correctly. Leap year is determined by checking if |
borlanic | 0:fbdae7e6d805 | 39 | * the year counter value is divisible by 400, 100, and 4. No problem here. |
borlanic | 0:fbdae7e6d805 | 40 | * b) RTCs which handles leap years correctly up to 2100. The RTC does a simple bit comparison to see if the two |
borlanic | 0:fbdae7e6d805 | 41 | * lowest order bits of the year counter are zero. In this case 2100 year will be considered |
borlanic | 0:fbdae7e6d805 | 42 | * 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 |
borlanic | 0:fbdae7e6d805 | 43 | * 29.02.2100 (invalid). So after 28.02.2100 the day counter will be off by a day. |
borlanic | 0:fbdae7e6d805 | 44 | */ |
borlanic | 0:fbdae7e6d805 | 45 | typedef enum { |
borlanic | 0:fbdae7e6d805 | 46 | RTC_FULL_LEAP_YEAR_SUPPORT, |
borlanic | 0:fbdae7e6d805 | 47 | RTC_4_YEAR_LEAP_YEAR_SUPPORT |
borlanic | 0:fbdae7e6d805 | 48 | } rtc_leap_year_support_t; |
borlanic | 0:fbdae7e6d805 | 49 | |
borlanic | 0:fbdae7e6d805 | 50 | /** Compute if a year is a leap year or not. |
borlanic | 0:fbdae7e6d805 | 51 | * |
borlanic | 0:fbdae7e6d805 | 52 | * @param year The year to test it shall be in the range [70:206]. Year 0 is |
borlanic | 0:fbdae7e6d805 | 53 | * translated into year 1900 CE. |
borlanic | 0:fbdae7e6d805 | 54 | * @param leap_year_support use RTC_FULL_LEAP_YEAR_SUPPORT if RTC device is able |
borlanic | 0:fbdae7e6d805 | 55 | * to correctly detect all leap years in range [70:206] otherwise use RTC_4_YEAR_LEAP_YEAR_SUPPORT. |
borlanic | 0:fbdae7e6d805 | 56 | * |
borlanic | 0:fbdae7e6d805 | 57 | * @return true if the year in input is a leap year and false otherwise. |
borlanic | 0:fbdae7e6d805 | 58 | * |
borlanic | 0:fbdae7e6d805 | 59 | * @note For use by the HAL only |
borlanic | 0:fbdae7e6d805 | 60 | * @note Year 2100 is treated differently for devices with full leap year support and devices with |
borlanic | 0:fbdae7e6d805 | 61 | * partial leap year support. Devices with partial leap year support treats 2100 as a leap year. |
borlanic | 0:fbdae7e6d805 | 62 | */ |
borlanic | 0:fbdae7e6d805 | 63 | bool _rtc_is_leap_year(int year, rtc_leap_year_support_t leap_year_support); |
borlanic | 0:fbdae7e6d805 | 64 | |
borlanic | 0:fbdae7e6d805 | 65 | /* Convert a calendar time into time since UNIX epoch as a time_t. |
borlanic | 0:fbdae7e6d805 | 66 | * |
borlanic | 0:fbdae7e6d805 | 67 | * This function is a thread safe (partial) replacement for mktime. It is |
borlanic | 0:fbdae7e6d805 | 68 | * tailored around RTC peripherals needs and is not by any mean a complete |
borlanic | 0:fbdae7e6d805 | 69 | * replacement of mktime. |
borlanic | 0:fbdae7e6d805 | 70 | * |
borlanic | 0:fbdae7e6d805 | 71 | * @param time The calendar time to convert into a time_t since epoch. |
borlanic | 0:fbdae7e6d805 | 72 | * The fields from tm used for the computation are: |
borlanic | 0:fbdae7e6d805 | 73 | * - tm_sec |
borlanic | 0:fbdae7e6d805 | 74 | * - tm_min |
borlanic | 0:fbdae7e6d805 | 75 | * - tm_hour |
borlanic | 0:fbdae7e6d805 | 76 | * - tm_mday |
borlanic | 0:fbdae7e6d805 | 77 | * - tm_mon |
borlanic | 0:fbdae7e6d805 | 78 | * - tm_year |
borlanic | 0:fbdae7e6d805 | 79 | * Other fields are ignored and won't be renormalized by a call to this function. |
borlanic | 0:fbdae7e6d805 | 80 | * A valid calendar time is comprised between: |
borlanic | 0:fbdae7e6d805 | 81 | * the 1st of January 1970 at 00:00:00 to the 7th of February 2106 at 06:28:15. |
borlanic | 0:fbdae7e6d805 | 82 | * @param leap_year_support use RTC_FULL_LEAP_YEAR_SUPPORT if RTC device is able |
borlanic | 0:fbdae7e6d805 | 83 | * to correctly detect all leap years in range [70:206] otherwise use RTC_4_YEAR_LEAP_YEAR_SUPPORT. |
borlanic | 0:fbdae7e6d805 | 84 | * @param seconds holder for the result - calendar time as seconds since UNIX epoch. |
borlanic | 0:fbdae7e6d805 | 85 | * |
borlanic | 0:fbdae7e6d805 | 86 | * @return true on success, false if conversion error occurred. |
borlanic | 0:fbdae7e6d805 | 87 | * |
borlanic | 0:fbdae7e6d805 | 88 | * @note Leap seconds are not supported. |
borlanic | 0:fbdae7e6d805 | 89 | * @note Values in output range from 0 to UINT_MAX. |
borlanic | 0:fbdae7e6d805 | 90 | * @note Full and partial leap years support. |
borlanic | 0:fbdae7e6d805 | 91 | * @note For use by the HAL only |
borlanic | 0:fbdae7e6d805 | 92 | */ |
borlanic | 0:fbdae7e6d805 | 93 | bool _rtc_maketime(const struct tm* time, time_t * seconds, rtc_leap_year_support_t leap_year_support); |
borlanic | 0:fbdae7e6d805 | 94 | |
borlanic | 0:fbdae7e6d805 | 95 | /* Convert a given time in seconds since epoch into calendar time. |
borlanic | 0:fbdae7e6d805 | 96 | * |
borlanic | 0:fbdae7e6d805 | 97 | * This function is a thread safe (partial) replacement for localtime. It is |
borlanic | 0:fbdae7e6d805 | 98 | * tailored around RTC peripherals specification and is not by any means a |
borlanic | 0:fbdae7e6d805 | 99 | * complete of localtime. |
borlanic | 0:fbdae7e6d805 | 100 | * |
borlanic | 0:fbdae7e6d805 | 101 | * @param timestamp The time (in seconds) to convert into calendar time. Valid |
borlanic | 0:fbdae7e6d805 | 102 | * input are in the range [0 : UINT32_MAX]. |
borlanic | 0:fbdae7e6d805 | 103 | * @param calendar_time Pointer to the object which will contain the result of |
borlanic | 0:fbdae7e6d805 | 104 | * the conversion. The tm fields filled by this function are: |
borlanic | 0:fbdae7e6d805 | 105 | * - tm_sec |
borlanic | 0:fbdae7e6d805 | 106 | * - tm_min |
borlanic | 0:fbdae7e6d805 | 107 | * - tm_hour |
borlanic | 0:fbdae7e6d805 | 108 | * - tm_mday |
borlanic | 0:fbdae7e6d805 | 109 | * - tm_mon |
borlanic | 0:fbdae7e6d805 | 110 | * - tm_year |
borlanic | 0:fbdae7e6d805 | 111 | * - tm_wday |
borlanic | 0:fbdae7e6d805 | 112 | * - tm_yday |
borlanic | 0:fbdae7e6d805 | 113 | * The object remains untouched if the time in input is invalid. |
borlanic | 0:fbdae7e6d805 | 114 | * @param leap_year_support use RTC_FULL_LEAP_YEAR_SUPPORT if RTC device is able |
borlanic | 0:fbdae7e6d805 | 115 | * to correctly detect all leap years in range [70:206] otherwise use RTC_4_YEAR_LEAP_YEAR_SUPPORT. |
borlanic | 0:fbdae7e6d805 | 116 | * @return true if the conversion was successful, false otherwise. |
borlanic | 0:fbdae7e6d805 | 117 | * |
borlanic | 0:fbdae7e6d805 | 118 | * @note For use by the HAL only. |
borlanic | 0:fbdae7e6d805 | 119 | * @note Full and partial leap years support. |
borlanic | 0:fbdae7e6d805 | 120 | */ |
borlanic | 0:fbdae7e6d805 | 121 | bool _rtc_localtime(time_t timestamp, struct tm* time_info, rtc_leap_year_support_t leap_year_support); |
borlanic | 0:fbdae7e6d805 | 122 | |
borlanic | 0:fbdae7e6d805 | 123 | /** @}*/ |
borlanic | 0:fbdae7e6d805 | 124 | |
borlanic | 0:fbdae7e6d805 | 125 | #ifdef __cplusplus |
borlanic | 0:fbdae7e6d805 | 126 | } |
borlanic | 0:fbdae7e6d805 | 127 | #endif |
borlanic | 0:fbdae7e6d805 | 128 | |
borlanic | 0:fbdae7e6d805 | 129 | #endif /* MBED_MKTIME_H */ |
borlanic | 0:fbdae7e6d805 | 130 | |
borlanic | 0:fbdae7e6d805 | 131 | /** @}*/ |