[libc][NFC] Move aligned access implementations to separate header
[llvm-project.git] / libc / src / time / time_utils.cpp
blobb25b737d1f8d0ac73ef8b2a38dff7214fca0b7ea
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 //===----------------------------------------------------------------------===//
9 #include "src/time/time_utils.h"
10 #include "src/__support/common.h"
12 #include <limits.h>
14 namespace __llvm_libc {
15 namespace time_utils {
17 using __llvm_libc::time_utils::TimeConstants;
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;
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.
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.
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.
44 // Compute the number of months from the remaining days. Finally, adjust years
45 // to be 1900 and months to be from January.
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};
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();
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--;
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;
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--;
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);
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);
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);
104 // Calculate number of years from year 2000.
105 int64_t years = remainingYears + 4 * numOfFourYearCycles +
106 100 * numOfHundredYearCycles +
107 400LL * numOfFourHundredYearCycles;
109 int leapDay =
110 !remainingYears && (numOfFourYearCycles || !numOfHundredYearCycles);
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;
118 int64_t months = 0;
119 while (daysInMonth[months] <= remainingDays) {
120 remainingDays -= daysInMonth[months];
121 months++;
124 if (months >= TimeConstants::MONTHS_PER_YEAR - 2) {
125 months -= TimeConstants::MONTHS_PER_YEAR;
126 years++;
129 if (years > INT_MAX || years < INT_MIN)
130 return time_utils::out_of_range();
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 = years + 2000 - TimeConstants::TIME_YEAR_BASE;
135 tm->tm_mon = months + 2;
136 tm->tm_mday = remainingDays + 1;
137 tm->tm_wday = wday;
138 tm->tm_yday = yday;
140 tm->tm_hour = remainingSeconds / TimeConstants::SECONDS_PER_HOUR;
141 tm->tm_min = remainingSeconds / TimeConstants::SECONDS_PER_MIN %
142 TimeConstants::SECONDS_PER_MIN;
143 tm->tm_sec = remainingSeconds % TimeConstants::SECONDS_PER_MIN;
144 // TODO(rtenneti): Need to handle timezone and update of tm_isdst.
145 tm->tm_isdst = 0;
147 return 0;
150 } // namespace time_utils
151 } // namespace __llvm_libc