mbed_mktime.h 4.9 KB

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