xref: /aosp_15_r20/external/llvm-libc/src/time/time_utils.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
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 
14 namespace LIBC_NAMESPACE_DECL {
15 namespace time_utils {
16 
17 using LIBC_NAMESPACE::time_utils::TimeConstants;
18 
computeRemainingYears(int64_t daysPerYears,int64_t quotientYears,int64_t * remainingDays)19 static int64_t computeRemainingYears(int64_t daysPerYears,
20                                      int64_t quotientYears,
21                                      int64_t *remainingDays) {
22   int64_t years = *remainingDays / daysPerYears;
23   if (years == quotientYears)
24     years--;
25   *remainingDays -= years * daysPerYears;
26   return years;
27 }
28 
29 // First, divide "total_seconds" by the number of seconds in a day to get the
30 // number of days since Jan 1 1970. The remainder will be used to calculate the
31 // number of Hours, Minutes and Seconds.
32 //
33 // Then, adjust that number of days by a constant to be the number of days
34 // since Mar 1 2000. Year 2000 is a multiple of 400, the leap year cycle. This
35 // makes it easier to count how many leap years have passed using division.
36 //
37 // While calculating numbers of years in the days, the following algorithm
38 // subdivides the days into the number of 400 years, the number of 100 years and
39 // the number of 4 years. These numbers of cycle years are used in calculating
40 // leap day. This is similar to the algorithm used in  getNumOfLeapYearsBefore()
41 // and isLeapYear(). Then compute the total number of years in days from these
42 // subdivided units.
43 //
44 // Compute the number of months from the remaining days. Finally, adjust years
45 // to be 1900 and months to be from January.
update_from_seconds(int64_t total_seconds,struct tm * tm)46 int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
47   // Days in month starting from March in the year 2000.
48   static const char daysInMonth[] = {31 /* Mar */, 30, 31, 30, 31, 31,
49                                      30,           31, 30, 31, 31, 29};
50 
51   constexpr time_t time_min =
52       (sizeof(time_t) == 4)
53           ? INT_MIN
54           : INT_MIN * static_cast<int64_t>(
55                           TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR);
56   constexpr time_t time_max =
57       (sizeof(time_t) == 4)
58           ? INT_MAX
59           : INT_MAX * static_cast<int64_t>(
60                           TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR);
61 
62   time_t ts = static_cast<time_t>(total_seconds);
63   if (ts < time_min || ts > time_max)
64     return time_utils::out_of_range();
65 
66   int64_t seconds =
67       total_seconds - TimeConstants::SECONDS_UNTIL2000_MARCH_FIRST;
68   int64_t days = seconds / TimeConstants::SECONDS_PER_DAY;
69   int64_t remainingSeconds = seconds % TimeConstants::SECONDS_PER_DAY;
70   if (remainingSeconds < 0) {
71     remainingSeconds += TimeConstants::SECONDS_PER_DAY;
72     days--;
73   }
74 
75   int64_t wday = (TimeConstants::WEEK_DAY_OF2000_MARCH_FIRST + days) %
76                  TimeConstants::DAYS_PER_WEEK;
77   if (wday < 0)
78     wday += TimeConstants::DAYS_PER_WEEK;
79 
80   // Compute the number of 400 year cycles.
81   int64_t numOfFourHundredYearCycles = days / TimeConstants::DAYS_PER400_YEARS;
82   int64_t remainingDays = days % TimeConstants::DAYS_PER400_YEARS;
83   if (remainingDays < 0) {
84     remainingDays += TimeConstants::DAYS_PER400_YEARS;
85     numOfFourHundredYearCycles--;
86   }
87 
88   // The remaining number of years after computing the number of
89   // "four hundred year cycles" will be 4 hundred year cycles or less in 400
90   // years.
91   int64_t numOfHundredYearCycles = computeRemainingYears(
92       TimeConstants::DAYS_PER100_YEARS, 4, &remainingDays);
93 
94   // The remaining number of years after computing the number of
95   // "hundred year cycles" will be 25 four year cycles or less in 100 years.
96   int64_t numOfFourYearCycles =
97       computeRemainingYears(TimeConstants::DAYS_PER4_YEARS, 25, &remainingDays);
98 
99   // The remaining number of years after computing the number of
100   // "four year cycles" will be 4 one year cycles or less in 4 years.
101   int64_t remainingYears = computeRemainingYears(
102       TimeConstants::DAYS_PER_NON_LEAP_YEAR, 4, &remainingDays);
103 
104   // Calculate number of years from year 2000.
105   int64_t years = remainingYears + 4 * numOfFourYearCycles +
106                   100 * numOfHundredYearCycles +
107                   400LL * numOfFourHundredYearCycles;
108 
109   int leapDay =
110       !remainingYears && (numOfFourYearCycles || !numOfHundredYearCycles);
111 
112   // We add 31 and 28 for the number of days in January and February, since our
113   // starting point was March 1st.
114   int64_t yday = remainingDays + 31 + 28 + leapDay;
115   if (yday >= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay)
116     yday -= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay;
117 
118   int64_t months = 0;
119   while (daysInMonth[months] <= remainingDays) {
120     remainingDays -= daysInMonth[months];
121     months++;
122   }
123 
124   if (months >= TimeConstants::MONTHS_PER_YEAR - 2) {
125     months -= TimeConstants::MONTHS_PER_YEAR;
126     years++;
127   }
128 
129   if (years > INT_MAX || years < INT_MIN)
130     return time_utils::out_of_range();
131 
132   // All the data (years, month and remaining days) was calculated from
133   // March, 2000. Thus adjust the data to be from January, 1900.
134   tm->tm_year = static_cast<int>(years + 2000 - TimeConstants::TIME_YEAR_BASE);
135   tm->tm_mon = static_cast<int>(months + 2);
136   tm->tm_mday = static_cast<int>(remainingDays + 1);
137   tm->tm_wday = static_cast<int>(wday);
138   tm->tm_yday = static_cast<int>(yday);
139 
140   tm->tm_hour =
141       static_cast<int>(remainingSeconds / TimeConstants::SECONDS_PER_HOUR);
142   tm->tm_min =
143       static_cast<int>(remainingSeconds / TimeConstants::SECONDS_PER_MIN %
144                        TimeConstants::SECONDS_PER_MIN);
145   tm->tm_sec =
146       static_cast<int>(remainingSeconds % TimeConstants::SECONDS_PER_MIN);
147   // TODO(rtenneti): Need to handle timezone and update of tm_isdst.
148   tm->tm_isdst = 0;
149 
150   return 0;
151 }
152 
153 } // namespace time_utils
154 } // namespace LIBC_NAMESPACE_DECL
155