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

source code of libc/src/time/mktime.cpp