| 1 | //===-- Implementation of mktime function ---------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/time/time_utils.h" |
| 10 | #include "src/__support/CPP/limits.h" // INT_MIN, INT_MAX |
| 11 | #include "src/__support/common.h" |
| 12 | #include "src/__support/macros/config.h" |
| 13 | #include "src/time/time_constants.h" |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | namespace time_utils { |
| 19 | |
| 20 | // TODO: clean this up in a followup patch |
| 21 | cpp::optional<time_t> mktime_internal(const tm *tm_out) { |
| 22 | // Unlike most C Library functions, mktime doesn't just die on bad input. |
| 23 | // TODO(rtenneti); Handle leap seconds. |
| 24 | int64_t tm_year_from_base = tm_out->tm_year + time_constants::TIME_YEAR_BASE; |
| 25 | |
| 26 | // 32-bit end-of-the-world is 03:14:07 UTC on 19 January 2038. |
| 27 | if (sizeof(time_t) == 4 && |
| 28 | tm_year_from_base >= time_constants::END_OF32_BIT_EPOCH_YEAR) { |
| 29 | if (tm_year_from_base > time_constants::END_OF32_BIT_EPOCH_YEAR) |
| 30 | return cpp::nullopt; |
| 31 | if (tm_out->tm_mon > 0) |
| 32 | return cpp::nullopt; |
| 33 | if (tm_out->tm_mday > 19) |
| 34 | return cpp::nullopt; |
| 35 | else if (tm_out->tm_mday == 19) { |
| 36 | if (tm_out->tm_hour > 3) |
| 37 | return cpp::nullopt; |
| 38 | else if (tm_out->tm_hour == 3) { |
| 39 | if (tm_out->tm_min > 14) |
| 40 | return cpp::nullopt; |
| 41 | else if (tm_out->tm_min == 14) { |
| 42 | if (tm_out->tm_sec > 7) |
| 43 | return cpp::nullopt; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Years are ints. A 32-bit year will fit into a 64-bit time_t. |
| 50 | // A 64-bit year will not. |
| 51 | static_assert( |
| 52 | sizeof(int) == 4, |
| 53 | "ILP64 is unimplemented. This implementation requires 32-bit integers." ); |
| 54 | |
| 55 | // Calculate number of months and years from tm_mon. |
| 56 | int64_t month = tm_out->tm_mon; |
| 57 | if (month < 0 || month >= time_constants::MONTHS_PER_YEAR - 1) { |
| 58 | int64_t years = month / 12; |
| 59 | month %= 12; |
| 60 | if (month < 0) { |
| 61 | years--; |
| 62 | month += 12; |
| 63 | } |
| 64 | tm_year_from_base += years; |
| 65 | } |
| 66 | bool tm_year_is_leap = time_utils::is_leap_year(tm_year_from_base); |
| 67 | |
| 68 | // Calculate total number of days based on the month and the day (tm_mday). |
| 69 | int64_t total_days = tm_out->tm_mday - 1; |
| 70 | for (int64_t i = 0; i < month; ++i) |
| 71 | total_days += time_constants::NON_LEAP_YEAR_DAYS_IN_MONTH[i]; |
| 72 | // Add one day if it is a leap year and the month is after February. |
| 73 | if (tm_year_is_leap && month > 1) |
| 74 | total_days++; |
| 75 | |
| 76 | // Calculate total numbers of days based on the year. |
| 77 | total_days += (tm_year_from_base - time_constants::EPOCH_YEAR) * |
| 78 | time_constants::DAYS_PER_NON_LEAP_YEAR; |
| 79 | if (tm_year_from_base >= time_constants::EPOCH_YEAR) { |
| 80 | total_days += |
| 81 | time_utils::get_num_of_leap_years_before(tm_year_from_base - 1) - |
| 82 | time_utils::get_num_of_leap_years_before(time_constants::EPOCH_YEAR); |
| 83 | } else if (tm_year_from_base >= 1) { |
| 84 | total_days -= |
| 85 | time_utils::get_num_of_leap_years_before(time_constants::EPOCH_YEAR) - |
| 86 | time_utils::get_num_of_leap_years_before(tm_year_from_base - 1); |
| 87 | } else { |
| 88 | // Calculate number of leap years until 0th year. |
| 89 | total_days -= |
| 90 | time_utils::get_num_of_leap_years_before(time_constants::EPOCH_YEAR) - |
| 91 | time_utils::get_num_of_leap_years_before(0); |
| 92 | if (tm_year_from_base <= 0) { |
| 93 | total_days -= 1; // Subtract 1 for 0th year. |
| 94 | // Calculate number of leap years until -1 year |
| 95 | if (tm_year_from_base < 0) { |
| 96 | total_days -= |
| 97 | time_utils::get_num_of_leap_years_before(-tm_year_from_base) - |
| 98 | time_utils::get_num_of_leap_years_before(1); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // TODO: https://github.com/llvm/llvm-project/issues/121962 |
| 104 | // Need to handle timezone and update of tm_isdst. |
| 105 | time_t seconds = static_cast<time_t>( |
| 106 | tm_out->tm_sec + tm_out->tm_min * time_constants::SECONDS_PER_MIN + |
| 107 | tm_out->tm_hour * time_constants::SECONDS_PER_HOUR + |
| 108 | total_days * time_constants::SECONDS_PER_DAY); |
| 109 | return seconds; |
| 110 | } |
| 111 | |
| 112 | static int64_t computeRemainingYears(int64_t daysPerYears, |
| 113 | int64_t quotientYears, |
| 114 | int64_t *remainingDays) { |
| 115 | int64_t years = *remainingDays / daysPerYears; |
| 116 | if (years == quotientYears) |
| 117 | years--; |
| 118 | *remainingDays -= years * daysPerYears; |
| 119 | return years; |
| 120 | } |
| 121 | |
| 122 | // First, divide "total_seconds" by the number of seconds in a day to get the |
| 123 | // number of days since Jan 1 1970. The remainder will be used to calculate the |
| 124 | // number of Hours, Minutes and Seconds. |
| 125 | // |
| 126 | // Then, adjust that number of days by a constant to be the number of days |
| 127 | // since Mar 1 2000. Year 2000 is a multiple of 400, the leap year cycle. This |
| 128 | // makes it easier to count how many leap years have passed using division. |
| 129 | // |
| 130 | // While calculating numbers of years in the days, the following algorithm |
| 131 | // subdivides the days into the number of 400 years, the number of 100 years and |
| 132 | // the number of 4 years. These numbers of cycle years are used in calculating |
| 133 | // leap day. This is similar to the algorithm used in getNumOfLeapYearsBefore() |
| 134 | // and isLeapYear(). Then compute the total number of years in days from these |
| 135 | // subdivided units. |
| 136 | // |
| 137 | // Compute the number of months from the remaining days. Finally, adjust years |
| 138 | // to be 1900 and months to be from January. |
| 139 | int64_t update_from_seconds(time_t total_seconds, tm *tm) { |
| 140 | // Days in month starting from March in the year 2000. |
| 141 | static const char daysInMonth[] = {31 /* Mar */, 30, 31, 30, 31, 31, |
| 142 | 30, 31, 30, 31, 31, 29}; |
| 143 | |
| 144 | constexpr time_t time_min = |
| 145 | (sizeof(time_t) == 4) |
| 146 | ? INT_MIN |
| 147 | : INT_MIN * static_cast<int64_t>( |
| 148 | time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); |
| 149 | constexpr time_t time_max = |
| 150 | (sizeof(time_t) == 4) |
| 151 | ? INT_MAX |
| 152 | : INT_MAX * static_cast<int64_t>( |
| 153 | time_constants::NUMBER_OF_SECONDS_IN_LEAP_YEAR); |
| 154 | |
| 155 | if (total_seconds < time_min || total_seconds > time_max) |
| 156 | return time_utils::out_of_range(); |
| 157 | |
| 158 | int64_t seconds = |
| 159 | total_seconds - time_constants::SECONDS_UNTIL2000_MARCH_FIRST; |
| 160 | int64_t days = seconds / time_constants::SECONDS_PER_DAY; |
| 161 | int64_t remainingSeconds = seconds % time_constants::SECONDS_PER_DAY; |
| 162 | if (remainingSeconds < 0) { |
| 163 | remainingSeconds += time_constants::SECONDS_PER_DAY; |
| 164 | days--; |
| 165 | } |
| 166 | |
| 167 | int64_t wday = (time_constants::WEEK_DAY_OF2000_MARCH_FIRST + days) % |
| 168 | time_constants::DAYS_PER_WEEK; |
| 169 | if (wday < 0) |
| 170 | wday += time_constants::DAYS_PER_WEEK; |
| 171 | |
| 172 | // Compute the number of 400 year cycles. |
| 173 | int64_t numOfFourHundredYearCycles = days / time_constants::DAYS_PER400_YEARS; |
| 174 | int64_t remainingDays = days % time_constants::DAYS_PER400_YEARS; |
| 175 | if (remainingDays < 0) { |
| 176 | remainingDays += time_constants::DAYS_PER400_YEARS; |
| 177 | numOfFourHundredYearCycles--; |
| 178 | } |
| 179 | |
| 180 | // The remaining number of years after computing the number of |
| 181 | // "four hundred year cycles" will be 4 hundred year cycles or less in 400 |
| 182 | // years. |
| 183 | int64_t numOfHundredYearCycles = computeRemainingYears( |
| 184 | time_constants::DAYS_PER100_YEARS, 4, &remainingDays); |
| 185 | |
| 186 | // The remaining number of years after computing the number of |
| 187 | // "hundred year cycles" will be 25 four year cycles or less in 100 years. |
| 188 | int64_t numOfFourYearCycles = computeRemainingYears( |
| 189 | time_constants::DAYS_PER4_YEARS, 25, &remainingDays); |
| 190 | |
| 191 | // The remaining number of years after computing the number of |
| 192 | // "four year cycles" will be 4 one year cycles or less in 4 years. |
| 193 | int64_t remainingYears = computeRemainingYears( |
| 194 | time_constants::DAYS_PER_NON_LEAP_YEAR, 4, &remainingDays); |
| 195 | |
| 196 | // Calculate number of years from year 2000. |
| 197 | int64_t years = remainingYears + 4 * numOfFourYearCycles + |
| 198 | 100 * numOfHundredYearCycles + |
| 199 | 400LL * numOfFourHundredYearCycles; |
| 200 | |
| 201 | int leapDay = |
| 202 | !remainingYears && (numOfFourYearCycles || !numOfHundredYearCycles); |
| 203 | |
| 204 | // We add 31 and 28 for the number of days in January and February, since our |
| 205 | // starting point was March 1st. |
| 206 | int64_t yday = remainingDays + 31 + 28 + leapDay; |
| 207 | if (yday >= time_constants::DAYS_PER_NON_LEAP_YEAR + leapDay) |
| 208 | yday -= time_constants::DAYS_PER_NON_LEAP_YEAR + leapDay; |
| 209 | |
| 210 | int64_t months = 0; |
| 211 | while (daysInMonth[months] <= remainingDays) { |
| 212 | remainingDays -= daysInMonth[months]; |
| 213 | months++; |
| 214 | } |
| 215 | |
| 216 | if (months >= time_constants::MONTHS_PER_YEAR - 2) { |
| 217 | months -= time_constants::MONTHS_PER_YEAR; |
| 218 | years++; |
| 219 | } |
| 220 | |
| 221 | if (years > INT_MAX || years < INT_MIN) |
| 222 | return time_utils::out_of_range(); |
| 223 | |
| 224 | // All the data (years, month and remaining days) was calculated from |
| 225 | // March, 2000. Thus adjust the data to be from January, 1900. |
| 226 | tm->tm_year = static_cast<int>(years + 2000 - time_constants::TIME_YEAR_BASE); |
| 227 | tm->tm_mon = static_cast<int>(months + 2); |
| 228 | tm->tm_mday = static_cast<int>(remainingDays + 1); |
| 229 | tm->tm_wday = static_cast<int>(wday); |
| 230 | tm->tm_yday = static_cast<int>(yday); |
| 231 | |
| 232 | tm->tm_hour = |
| 233 | static_cast<int>(remainingSeconds / time_constants::SECONDS_PER_HOUR); |
| 234 | tm->tm_min = |
| 235 | static_cast<int>(remainingSeconds / time_constants::SECONDS_PER_MIN % |
| 236 | time_constants::SECONDS_PER_MIN); |
| 237 | tm->tm_sec = |
| 238 | static_cast<int>(remainingSeconds % time_constants::SECONDS_PER_MIN); |
| 239 | // TODO(rtenneti): Need to handle timezone and update of tm_isdst. |
| 240 | tm->tm_isdst = 0; |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | } // namespace time_utils |
| 246 | } // namespace LIBC_NAMESPACE_DECL |
| 247 | |