#include "src/time/mktime.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/time/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
using LIBC_NAMESPACE::time_utils::TimeConstants;
static constexpr int NON_LEAP_YEAR_DAYS_IN_MONTH[] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
static constexpr int64_t get_num_of_leap_years_before(int64_t year) {
return (year / 4) - (year / 100) + (year / 400);
}
static constexpr bool is_leap_year(const int64_t year) {
return (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0));
}
LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) {
int64_t tm_year_from_base = tm_out->tm_year + TimeConstants::TIME_YEAR_BASE;
if (sizeof(time_t) == 4 &&
tm_year_from_base >= TimeConstants::END_OF32_BIT_EPOCH_YEAR) {
if (tm_year_from_base > TimeConstants::END_OF32_BIT_EPOCH_YEAR)
return time_utils::out_of_range();
if (tm_out->tm_mon > 0)
return time_utils::out_of_range();
if (tm_out->tm_mday > 19)
return time_utils::out_of_range();
if (tm_out->tm_hour > 3)
return time_utils::out_of_range();
if (tm_out->tm_min > 14)
return time_utils::out_of_range();
if (tm_out->tm_sec > 7)
return time_utils::out_of_range();
}
static_assert(
sizeof(int) == 4,
"ILP64 is unimplemented. This implementation requires 32-bit integers.");
int64_t month = tm_out->tm_mon;
if (month < 0 || month >= TimeConstants::MONTHS_PER_YEAR - 1) {
int64_t years = month / 12;
month %= 12;
if (month < 0) {
years--;
month += 12;
}
tm_year_from_base += years;
}
bool tm_year_is_leap = is_leap_year(tm_year_from_base);
int64_t total_days = tm_out->tm_mday - 1;
for (int64_t i = 0; i < month; ++i)
total_days += NON_LEAP_YEAR_DAYS_IN_MONTH[i];
if (tm_year_is_leap && month > 1)
total_days++;
total_days += (tm_year_from_base - TimeConstants::EPOCH_YEAR) *
TimeConstants::DAYS_PER_NON_LEAP_YEAR;
if (tm_year_from_base >= TimeConstants::EPOCH_YEAR) {
total_days += get_num_of_leap_years_before(tm_year_from_base - 1) -
get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR);
} else if (tm_year_from_base >= 1) {
total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) -
get_num_of_leap_years_before(tm_year_from_base - 1);
} else {
total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) -
get_num_of_leap_years_before(0);
if (tm_year_from_base <= 0) {
total_days -= 1;
if (tm_year_from_base < 0) {
total_days -= get_num_of_leap_years_before(-tm_year_from_base) -
get_num_of_leap_years_before(1);
}
}
}
int64_t seconds = tm_out->tm_sec +
tm_out->tm_min * TimeConstants::SECONDS_PER_MIN +
tm_out->tm_hour * TimeConstants::SECONDS_PER_HOUR +
total_days * TimeConstants::SECONDS_PER_DAY;
if (time_utils::update_from_seconds(seconds, tm_out) < 0)
return time_utils::out_of_range();
return static_cast<time_t>(seconds);
}
}