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 NON_LEAP_YEAR_DAYS_IN_MONTH[] = {31, 28, 31, 30, 31, 30,
20                                                       31, 31, 30, 31, 30, 31};
21 
22 // Returns number of years from (1, year).
get_num_of_leap_years_before(int64_t year)23 static constexpr int64_t get_num_of_leap_years_before(int64_t year) {
24   return (year / 4) - (year / 100) + (year / 400);
25 }
26 
27 // Returns True if year is a leap year.
is_leap_year(const int64_t year)28 static constexpr bool is_leap_year(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 tm_year_from_base = tm_out->tm_year + TimeConstants::TIME_YEAR_BASE;
36 
37   // 32-bit end-of-the-world is 03:14:07 UTC on 19 January 2038.
38   if (sizeof(time_t) == 4 &&
39       tm_year_from_base >= TimeConstants::END_OF32_BIT_EPOCH_YEAR) {
40     if (tm_year_from_base > TimeConstants::END_OF32_BIT_EPOCH_YEAR)
41       return time_utils::out_of_range();
42     if (tm_out->tm_mon > 0)
43       return time_utils::out_of_range();
44     if (tm_out->tm_mday > 19)
45       return time_utils::out_of_range();
46     if (tm_out->tm_hour > 3)
47       return time_utils::out_of_range();
48     if (tm_out->tm_min > 14)
49       return time_utils::out_of_range();
50     if (tm_out->tm_sec > 7)
51       return time_utils::out_of_range();
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::MONTHS_PER_YEAR - 1) {
63     int64_t years = month / 12;
64     month %= 12;
65     if (month < 0) {
66       years--;
67       month += 12;
68     }
69     tm_year_from_base += years;
70   }
71   bool tm_year_is_leap = is_leap_year(tm_year_from_base);
72 
73   // Calculate total number of days based on the month and the day (tm_mday).
74   int64_t total_days = tm_out->tm_mday - 1;
75   for (int64_t i = 0; i < month; ++i)
76     total_days += NON_LEAP_YEAR_DAYS_IN_MONTH[i];
77   // Add one day if it is a leap year and the month is after February.
78   if (tm_year_is_leap && month > 1)
79     total_days++;
80 
81   // Calculate total numbers of days based on the year.
82   total_days += (tm_year_from_base - TimeConstants::EPOCH_YEAR) *
83                 TimeConstants::DAYS_PER_NON_LEAP_YEAR;
84   if (tm_year_from_base >= TimeConstants::EPOCH_YEAR) {
85     total_days += get_num_of_leap_years_before(tm_year_from_base - 1) -
86                   get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR);
87   } else if (tm_year_from_base >= 1) {
88     total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) -
89                   get_num_of_leap_years_before(tm_year_from_base - 1);
90   } else {
91     // Calculate number of leap years until 0th year.
92     total_days -= get_num_of_leap_years_before(TimeConstants::EPOCH_YEAR) -
93                   get_num_of_leap_years_before(0);
94     if (tm_year_from_base <= 0) {
95       total_days -= 1; // Subtract 1 for 0th year.
96       // Calculate number of leap years until -1 year
97       if (tm_year_from_base < 0) {
98         total_days -= get_num_of_leap_years_before(-tm_year_from_base) -
99                       get_num_of_leap_years_before(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::SECONDS_PER_MIN +
107                     tm_out->tm_hour * TimeConstants::SECONDS_PER_HOUR +
108                     total_days * TimeConstants::SECONDS_PER_DAY;
109 
110   // Update the tm structure's year, month, day, etc. from seconds.
111   if (time_utils::update_from_seconds(seconds, tm_out) < 0)
112     return time_utils::out_of_range();
113 
114   return static_cast<time_t>(seconds);
115 }
116 
117 } // namespace __llvm_libc
118