mbed_mktime.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2017-2017 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "mbed_mktime.h"
  17. /* Time constants. */
  18. #define SECONDS_BY_MINUTES 60
  19. #define MINUTES_BY_HOUR 60
  20. #define SECONDS_BY_HOUR (SECONDS_BY_MINUTES * MINUTES_BY_HOUR)
  21. #define HOURS_BY_DAY 24
  22. #define SECONDS_BY_DAY (SECONDS_BY_HOUR * HOURS_BY_DAY)
  23. #define LAST_VALID_YEAR 206
  24. /* Macros which will be used to determine if we are within valid range. */
  25. #define EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT 3220095 // 7th of February 1970 at 06:28:15
  26. #define EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT 3133695 // 6th of February 1970 at 06:28:15
  27. /*
  28. * 2 dimensional array containing the number of seconds elapsed before a given
  29. * month.
  30. * The second index map to the month while the first map to the type of year:
  31. * - 0: non leap year
  32. * - 1: leap year
  33. */
  34. static const uint32_t seconds_before_month[2][12] = {
  35. {
  36. 0,
  37. 31 * SECONDS_BY_DAY,
  38. (31 + 28) *SECONDS_BY_DAY,
  39. (31 + 28 + 31) *SECONDS_BY_DAY,
  40. (31 + 28 + 31 + 30) *SECONDS_BY_DAY,
  41. (31 + 28 + 31 + 30 + 31) *SECONDS_BY_DAY,
  42. (31 + 28 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
  43. (31 + 28 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
  44. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
  45. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
  46. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
  47. (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
  48. },
  49. {
  50. 0,
  51. 31 * SECONDS_BY_DAY,
  52. (31 + 29) *SECONDS_BY_DAY,
  53. (31 + 29 + 31) *SECONDS_BY_DAY,
  54. (31 + 29 + 31 + 30) *SECONDS_BY_DAY,
  55. (31 + 29 + 31 + 30 + 31) *SECONDS_BY_DAY,
  56. (31 + 29 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
  57. (31 + 29 + 31 + 30 + 31 + 30 + 31) *SECONDS_BY_DAY,
  58. (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31) *SECONDS_BY_DAY,
  59. (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) *SECONDS_BY_DAY,
  60. (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31) *SECONDS_BY_DAY,
  61. (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30) *SECONDS_BY_DAY,
  62. }
  63. };
  64. bool _rtc_is_leap_year(int year, rtc_leap_year_support_t leap_year_support)
  65. {
  66. /*
  67. * since in practice, the value manipulated by this algorithm lie in the
  68. * range: [70 : 206] the algorithm can be reduced to: year % 4 with exception for 200 (year 2100 is not leap year).
  69. * The algorithm valid over the full range of value is:
  70. year = 1900 + year;
  71. if (year % 4) {
  72. return false;
  73. } else if (year % 100) {
  74. return true;
  75. } else if (year % 400) {
  76. return false;
  77. }
  78. return true;
  79. */
  80. if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && year == 200) {
  81. return false; // 2100 is not a leap year
  82. }
  83. return (year) % 4 ? false : true;
  84. }
  85. bool _rtc_maketime(const struct tm *time, time_t *seconds, rtc_leap_year_support_t leap_year_support)
  86. {
  87. if (seconds == NULL || time == NULL) {
  88. return false;
  89. }
  90. /* Partial check for the upper bound of the range - check years only. Full check will be performed after the
  91. * elapsed time since the beginning of the year is calculated.
  92. */
  93. if ((time->tm_year < 70) || (time->tm_year > LAST_VALID_YEAR)) {
  94. return false;
  95. }
  96. uint32_t result = time->tm_sec;
  97. result += time->tm_min * SECONDS_BY_MINUTES;
  98. result += time->tm_hour * SECONDS_BY_HOUR;
  99. result += (time->tm_mday - 1) * SECONDS_BY_DAY;
  100. result += seconds_before_month[_rtc_is_leap_year(time->tm_year, leap_year_support)][time->tm_mon];
  101. /* Check if we are within valid range. */
  102. if (time->tm_year == LAST_VALID_YEAR) {
  103. if ((leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_FULL_LEAP_YEAR_SUPPORT) ||
  104. (leap_year_support == RTC_4_YEAR_LEAP_YEAR_SUPPORT && result > EDGE_TIMESTAMP_4_YEAR_LEAP_YEAR_SUPPORT)) {
  105. return false;
  106. }
  107. }
  108. if (time->tm_year > 70) {
  109. /* Valid in the range [70:206]. */
  110. uint32_t count_of_leap_days = ((time->tm_year - 1) / 4) - (70 / 4);
  111. if (leap_year_support == RTC_FULL_LEAP_YEAR_SUPPORT) {
  112. if (time->tm_year > 200) {
  113. count_of_leap_days--; // 2100 is not a leap year
  114. }
  115. }
  116. result += (((time->tm_year - 70) * 365) + count_of_leap_days) * SECONDS_BY_DAY;
  117. }
  118. *seconds = result;
  119. return true;
  120. }
  121. bool _rtc_localtime(time_t timestamp, struct tm *time_info, rtc_leap_year_support_t leap_year_support)
  122. {
  123. if (time_info == NULL) {
  124. return false;
  125. }
  126. uint32_t seconds = (uint32_t)timestamp;
  127. time_info->tm_sec = seconds % 60;
  128. seconds = seconds / 60; // timestamp in minutes
  129. time_info->tm_min = seconds % 60;
  130. seconds = seconds / 60; // timestamp in hours
  131. time_info->tm_hour = seconds % 24;
  132. seconds = seconds / 24; // timestamp in days;
  133. /* Compute the weekday.
  134. * The 1st of January 1970 was a Thursday which is equal to 4 in the weekday representation ranging from [0:6].
  135. */
  136. time_info->tm_wday = (seconds + 4) % 7;
  137. /* Years start at 70. */
  138. time_info->tm_year = 70;
  139. while (true) {
  140. if (_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 366) {
  141. ++time_info->tm_year;
  142. seconds -= 366;
  143. } else if (!_rtc_is_leap_year(time_info->tm_year, leap_year_support) && seconds >= 365) {
  144. ++time_info->tm_year;
  145. seconds -= 365;
  146. } else {
  147. /* The remaining days are less than a years. */
  148. break;
  149. }
  150. }
  151. time_info->tm_yday = seconds;
  152. /* Convert days into seconds and find the current month. */
  153. seconds *= SECONDS_BY_DAY;
  154. time_info->tm_mon = 11;
  155. bool leap = _rtc_is_leap_year(time_info->tm_year, leap_year_support);
  156. for (uint32_t i = 0; i < 12; ++i) {
  157. if ((uint32_t) seconds < seconds_before_month[leap][i]) {
  158. time_info->tm_mon = i - 1;
  159. break;
  160. }
  161. }
  162. /* Remove month from timestamp and compute the number of days.
  163. * Note: unlike other fields, days are not 0 indexed.
  164. */
  165. seconds -= seconds_before_month[leap][time_info->tm_mon];
  166. time_info->tm_mday = (seconds / SECONDS_BY_DAY) + 1;
  167. return true;
  168. }