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