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/mktime.h" 10 #include "src/__support/common.h" 11 #include "src/time/time_utils.h" 12 13 #include <limits.h> 14 15 namespace __llvm_libc { 16 17 using __llvm_libc::time_utils::TimeConstants; 18 19 static constexpr int NonLeapYearDaysInMonth[] = {31, 28, 31, 30, 31, 30, 20 31, 31, 30, 31, 30, 31}; 21 22 // Returns number of years from (1, year). 23 static constexpr int64_t getNumOfLeapYearsBefore(int64_t year) { 24 return (year / 4) - (year / 100) + (year / 400); 25 } 26 27 // Returns True if year is a leap year. 28 static constexpr bool isLeapYear(const int64_t year) { 29 return (((year) % 4) == 0 && (((year) % 100) != 0 || ((year) % 400) == 0)); 30 } 31 32 LLVM_LIBC_FUNCTION(time_t, mktime, (struct tm * tm_out)) { 33 // Unlike most C Library functions, mktime doesn't just die on bad input. 34 // TODO(rtenneti); Handle leap seconds. 35 int64_t tmYearFromBase = tm_out->tm_year + TimeConstants::TimeYearBase; 36 37 // 32-bit end-of-the-world is 03:14:07 UTC on 19 January 2038. 38 if (sizeof(time_t) == 4 && 39 tmYearFromBase >= TimeConstants::EndOf32BitEpochYear) { 40 if (tmYearFromBase > TimeConstants::EndOf32BitEpochYear) 41 return time_utils::OutOfRange(); 42 if (tm_out->tm_mon > 0) 43 return time_utils::OutOfRange(); 44 if (tm_out->tm_mday > 19) 45 return time_utils::OutOfRange(); 46 if (tm_out->tm_hour > 3) 47 return time_utils::OutOfRange(); 48 if (tm_out->tm_min > 14) 49 return time_utils::OutOfRange(); 50 if (tm_out->tm_sec > 7) 51 return time_utils::OutOfRange(); 52 } 53 54 // Years are ints. A 32-bit year will fit into a 64-bit time_t. 55 // A 64-bit year will not. 56 static_assert(sizeof(int) == 4, 57 "ILP64 is unimplemented. This implementation requires " 58 "32-bit integers."); 59 60 // Calculate number of months and years from tm_mon. 61 int64_t month = tm_out->tm_mon; 62 if (month < 0 || month >= TimeConstants::MonthsPerYear - 1) { 63 int64_t years = month / 12; 64 month %= 12; 65 if (month < 0) { 66 years--; 67 month += 12; 68 } 69 tmYearFromBase += years; 70 } 71 bool tmYearIsLeap = isLeapYear(tmYearFromBase); 72 73 // Calculate total number of days based on the month and the day (tm_mday). 74 int64_t totalDays = tm_out->tm_mday - 1; 75 for (int64_t i = 0; i < month; ++i) 76 totalDays += NonLeapYearDaysInMonth[i]; 77 // Add one day if it is a leap year and the month is after February. 78 if (tmYearIsLeap && month > 1) 79 totalDays++; 80 81 // Calculate total numbers of days based on the year. 82 totalDays += (tmYearFromBase - TimeConstants::EpochYear) * 83 TimeConstants::DaysPerNonLeapYear; 84 if (tmYearFromBase >= TimeConstants::EpochYear) { 85 totalDays += getNumOfLeapYearsBefore(tmYearFromBase - 1) - 86 getNumOfLeapYearsBefore(TimeConstants::EpochYear); 87 } else if (tmYearFromBase >= 1) { 88 totalDays -= getNumOfLeapYearsBefore(TimeConstants::EpochYear) - 89 getNumOfLeapYearsBefore(tmYearFromBase - 1); 90 } else { 91 // Calculate number of leap years until 0th year. 92 totalDays -= getNumOfLeapYearsBefore(TimeConstants::EpochYear) - 93 getNumOfLeapYearsBefore(0); 94 if (tmYearFromBase <= 0) { 95 totalDays -= 1; // Subtract 1 for 0th year. 96 // Calculate number of leap years until -1 year 97 if (tmYearFromBase < 0) { 98 totalDays -= getNumOfLeapYearsBefore(-tmYearFromBase) - 99 getNumOfLeapYearsBefore(1); 100 } 101 } 102 } 103 104 // TODO(rtenneti): Need to handle timezone and update of tm_isdst. 105 int64_t seconds = tm_out->tm_sec + 106 tm_out->tm_min * TimeConstants::SecondsPerMin + 107 tm_out->tm_hour * TimeConstants::SecondsPerHour + 108 totalDays * TimeConstants::SecondsPerDay; 109 110 // Update the tm structure's year, month, day, etc. from seconds. 111 if (time_utils::UpdateFromSeconds(seconds, tm_out) < 0) 112 return time_utils::OutOfRange(); 113 114 return static_cast<time_t>(seconds); 115 } 116 117 } // namespace __llvm_libc 118