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