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