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