• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
mktime_internal(const tm * tm_out)21 int64_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 time_utils::out_of_range();
31     if (tm_out->tm_mon > 0)
32       return time_utils::out_of_range();
33     if (tm_out->tm_mday > 19)
34       return time_utils::out_of_range();
35     else if (tm_out->tm_mday == 19) {
36       if (tm_out->tm_hour > 3)
37         return time_utils::out_of_range();
38       else if (tm_out->tm_hour == 3) {
39         if (tm_out->tm_min > 14)
40           return time_utils::out_of_range();
41         else if (tm_out->tm_min == 14) {
42           if (tm_out->tm_sec > 7)
43             return time_utils::out_of_range();
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   int64_t seconds = tm_out->tm_sec +
106                     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 
computeRemainingYears(int64_t daysPerYears,int64_t quotientYears,int64_t * remainingDays)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.
update_from_seconds(int64_t total_seconds,tm * tm)139 int64_t update_from_seconds(int64_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   time_t ts = static_cast<time_t>(total_seconds);
156   if (ts < time_min || ts > time_max)
157     return time_utils::out_of_range();
158 
159   int64_t seconds =
160       total_seconds - time_constants::SECONDS_UNTIL2000_MARCH_FIRST;
161   int64_t days = seconds / time_constants::SECONDS_PER_DAY;
162   int64_t remainingSeconds = seconds % time_constants::SECONDS_PER_DAY;
163   if (remainingSeconds < 0) {
164     remainingSeconds += time_constants::SECONDS_PER_DAY;
165     days--;
166   }
167 
168   int64_t wday = (time_constants::WEEK_DAY_OF2000_MARCH_FIRST + days) %
169                  time_constants::DAYS_PER_WEEK;
170   if (wday < 0)
171     wday += time_constants::DAYS_PER_WEEK;
172 
173   // Compute the number of 400 year cycles.
174   int64_t numOfFourHundredYearCycles = days / time_constants::DAYS_PER400_YEARS;
175   int64_t remainingDays = days % time_constants::DAYS_PER400_YEARS;
176   if (remainingDays < 0) {
177     remainingDays += time_constants::DAYS_PER400_YEARS;
178     numOfFourHundredYearCycles--;
179   }
180 
181   // The remaining number of years after computing the number of
182   // "four hundred year cycles" will be 4 hundred year cycles or less in 400
183   // years.
184   int64_t numOfHundredYearCycles = computeRemainingYears(
185       time_constants::DAYS_PER100_YEARS, 4, &remainingDays);
186 
187   // The remaining number of years after computing the number of
188   // "hundred year cycles" will be 25 four year cycles or less in 100 years.
189   int64_t numOfFourYearCycles = computeRemainingYears(
190       time_constants::DAYS_PER4_YEARS, 25, &remainingDays);
191 
192   // The remaining number of years after computing the number of
193   // "four year cycles" will be 4 one year cycles or less in 4 years.
194   int64_t remainingYears = computeRemainingYears(
195       time_constants::DAYS_PER_NON_LEAP_YEAR, 4, &remainingDays);
196 
197   // Calculate number of years from year 2000.
198   int64_t years = remainingYears + 4 * numOfFourYearCycles +
199                   100 * numOfHundredYearCycles +
200                   400LL * numOfFourHundredYearCycles;
201 
202   int leapDay =
203       !remainingYears && (numOfFourYearCycles || !numOfHundredYearCycles);
204 
205   // We add 31 and 28 for the number of days in January and February, since our
206   // starting point was March 1st.
207   int64_t yday = remainingDays + 31 + 28 + leapDay;
208   if (yday >= time_constants::DAYS_PER_NON_LEAP_YEAR + leapDay)
209     yday -= time_constants::DAYS_PER_NON_LEAP_YEAR + leapDay;
210 
211   int64_t months = 0;
212   while (daysInMonth[months] <= remainingDays) {
213     remainingDays -= daysInMonth[months];
214     months++;
215   }
216 
217   if (months >= time_constants::MONTHS_PER_YEAR - 2) {
218     months -= time_constants::MONTHS_PER_YEAR;
219     years++;
220   }
221 
222   if (years > INT_MAX || years < INT_MIN)
223     return time_utils::out_of_range();
224 
225   // All the data (years, month and remaining days) was calculated from
226   // March, 2000. Thus adjust the data to be from January, 1900.
227   tm->tm_year = static_cast<int>(years + 2000 - time_constants::TIME_YEAR_BASE);
228   tm->tm_mon = static_cast<int>(months + 2);
229   tm->tm_mday = static_cast<int>(remainingDays + 1);
230   tm->tm_wday = static_cast<int>(wday);
231   tm->tm_yday = static_cast<int>(yday);
232 
233   tm->tm_hour =
234       static_cast<int>(remainingSeconds / time_constants::SECONDS_PER_HOUR);
235   tm->tm_min =
236       static_cast<int>(remainingSeconds / time_constants::SECONDS_PER_MIN %
237                        time_constants::SECONDS_PER_MIN);
238   tm->tm_sec =
239       static_cast<int>(remainingSeconds % time_constants::SECONDS_PER_MIN);
240   // TODO(rtenneti): Need to handle timezone and update of tm_isdst.
241   tm->tm_isdst = 0;
242 
243   return 0;
244 }
245 
246 } // namespace time_utils
247 } // namespace LIBC_NAMESPACE_DECL
248