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/common.h"
11 
12 #include <limits.h>
13 
14 namespace __llvm_libc {
15 namespace time_utils {
16 
17 using __llvm_libc::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   if (sizeof(time_t) == 4) {
52     if (total_seconds < 0x80000000)
53       return time_utils::out_of_range();
54     if (total_seconds > 0x7FFFFFFF)
55       return time_utils::out_of_range();
56   } else {
57     if (total_seconds <
58             INT_MIN * static_cast<int64_t>(
59                           TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR) ||
60         total_seconds >
61             INT_MAX * static_cast<int64_t>(
62                           TimeConstants::NUMBER_OF_SECONDS_IN_LEAP_YEAR))
63       return time_utils::out_of_range();
64   }
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 reminder number of years after computing 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 reminder number of years after computing 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 reminder number of years after computing number of "four year cycles"
100   // 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   int64_t yday = remainingDays + 31 + 28 + leapDay;
113   if (yday >= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay)
114     yday -= TimeConstants::DAYS_PER_NON_LEAP_YEAR + leapDay;
115 
116   int64_t months = 0;
117   while (daysInMonth[months] <= remainingDays) {
118     remainingDays -= daysInMonth[months];
119     months++;
120   }
121 
122   if (months >= TimeConstants::MONTHS_PER_YEAR - 2) {
123     months -= TimeConstants::MONTHS_PER_YEAR;
124     years++;
125   }
126 
127   if (years > INT_MAX || years < INT_MIN)
128     return time_utils::out_of_range();
129 
130   // All the data (years, month and remaining days) was calculated from
131   // March, 2000. Thus adjust the data to be from January, 1900.
132   tm->tm_year = years + 2000 - TimeConstants::TIME_YEAR_BASE;
133   tm->tm_mon = months + 2;
134   tm->tm_mday = remainingDays + 1;
135   tm->tm_wday = wday;
136   tm->tm_yday = yday;
137 
138   tm->tm_hour = remainingSeconds / TimeConstants::SECONDS_PER_HOUR;
139   tm->tm_min = remainingSeconds / TimeConstants::SECONDS_PER_MIN %
140                TimeConstants::SECONDS_PER_MIN;
141   tm->tm_sec = remainingSeconds % TimeConstants::SECONDS_PER_MIN;
142   // TODO(rtenneti): Need to handle timezone and update of tm_isdst.
143   tm->tm_isdst = 0;
144 
145   return 0;
146 }
147 
148 } // namespace time_utils
149 } // namespace __llvm_libc
150