2 * ----------------------------------------------------------------------
3 * Copyright © 2005-2014 Rich Felker, et al.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * ----------------------------------------------------------------------
30 /* 2000-03-01 (mod 400 year, immediately after feb29 */
31 #define LEAPOCH (946684800LL + 86400*(31+29))
33 #define DAYS_PER_400Y (365*400 + 97)
34 #define DAYS_PER_100Y (365*100 + 24)
35 #define DAYS_PER_4Y (365*4 + 1)
37 static int __month_to_secs(int month, int is_leap)
39 static const int secs_through_month[] = {
40 0, 31*86400, 59*86400, 90*86400,
41 120*86400, 151*86400, 181*86400, 212*86400,
42 243*86400, 273*86400, 304*86400, 334*86400 };
43 int t = secs_through_month[month];
44 if (is_leap && month >= 2) t+=86400;
48 static long long __year_to_secs(long long year, int *is_leap)
50 if (year-2ULL <= 136) {
52 int leaps = (y-68)>>2;
55 if (is_leap) *is_leap = 1;
56 } else if (is_leap) *is_leap = 0;
57 return 31536000*(y-70) + 86400*leaps;
60 int cycles, centuries, leaps, rem;
62 if (!is_leap) is_leap = &(int){0};
63 cycles = (year-100) / 400;
64 rem = (year-100) % 400;
75 if (rem >= 300) centuries = 3, rem -= 300;
76 else centuries = 2, rem -= 200;
78 if (rem >= 100) centuries = 1, rem -= 100;
91 leaps += 97*cycles + 24*centuries - *is_leap;
93 return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
96 static long long __tm_to_secs(const struct tm *tm)
99 long long year = tm->tm_year;
100 int month = tm->tm_mon;
101 if (month >= 12 || month < 0) {
102 int adj = month / 12;
110 long long t = __year_to_secs(year, &is_leap);
111 t += __month_to_secs(month, is_leap);
112 t += 86400LL * (tm->tm_mday-1);
113 t += 3600LL * tm->tm_hour;
114 t += 60LL * tm->tm_min;
119 static int __secs_to_tm(long long t, struct tm *tm)
121 long long days, secs;
122 int remdays, remsecs, remyears;
123 int qc_cycles, c_cycles, q_cycles;
125 int wday, yday, leap;
126 static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
128 /* Reject time_t values whose year would overflow int */
129 if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
134 remsecs = secs % 86400;
141 if (wday < 0) wday += 7;
143 qc_cycles = days / DAYS_PER_400Y;
144 remdays = days % DAYS_PER_400Y;
146 remdays += DAYS_PER_400Y;
150 c_cycles = remdays / DAYS_PER_100Y;
151 if (c_cycles == 4) c_cycles--;
152 remdays -= c_cycles * DAYS_PER_100Y;
154 q_cycles = remdays / DAYS_PER_4Y;
155 if (q_cycles == 25) q_cycles--;
156 remdays -= q_cycles * DAYS_PER_4Y;
158 remyears = remdays / 365;
159 if (remyears == 4) remyears--;
160 remdays -= remyears * 365;
162 leap = !remyears && (q_cycles || !c_cycles);
163 yday = remdays + 31 + 28 + leap;
164 if (yday >= 365+leap) yday -= 365+leap;
166 years = remyears + 4*q_cycles + 100*c_cycles + 400*qc_cycles;
168 for (months=0; days_in_month[months] <= remdays; months++)
169 remdays -= days_in_month[months];
171 if (years+100 > INT_MAX || years+100 < INT_MIN)
174 tm->tm_year = years + 100;
175 tm->tm_mon = months + 2;
176 if (tm->tm_mon >= 12) {
180 tm->tm_mday = remdays + 1;
184 tm->tm_hour = remsecs / 3600;
185 tm->tm_min = remsecs / 60 % 60;
186 tm->tm_sec = remsecs % 60;
192 struct tm *__gmtime_r(const time_t *t, struct tm *tm)
194 if (__secs_to_tm(*t, tm) < 0) {
203 time_t timegm(struct tm *tm)
206 long long t = __tm_to_secs(tm);
207 if (__secs_to_tm(t, &new) < 0) {
211 #if SIZEOF_TIME_T != 8
212 if (t > (long long)INT_MAX || t < (long long)INT_MIN) {