Merge branch 'vendor/LIBRESSL'
[dragonfly.git] / crypto / libressl / crypto / compat / timegm.c
1 /*
2  * ----------------------------------------------------------------------
3  * Copyright © 2005-2014 Rich Felker, et al.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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  * ----------------------------------------------------------------------
24  */
25
26 #include <errno.h>
27 #include <limits.h>
28 #include <time.h>
29
30 /* 2000-03-01 (mod 400 year, immediately after feb29 */
31 #define LEAPOCH (946684800LL + 86400*(31+29))
32
33 #define DAYS_PER_400Y (365*400 + 97)
34 #define DAYS_PER_100Y (365*100 + 24)
35 #define DAYS_PER_4Y   (365*4   + 1)
36
37 static int __month_to_secs(int month, int is_leap)
38 {
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;
45         return t;
46 }
47
48 static long long __year_to_secs(long long year, int *is_leap)
49 {
50         if (year-2ULL <= 136) {
51                 int y = year;
52                 int leaps = (y-68)>>2;
53                 if (!((y-68)&3)) {
54                         leaps--;
55                         if (is_leap) *is_leap = 1;
56                 } else if (is_leap) *is_leap = 0;
57                 return 31536000*(y-70) + 86400*leaps;
58         }
59
60         int cycles, centuries, leaps, rem;
61
62         if (!is_leap) is_leap = &(int){0};
63         cycles = (year-100) / 400;
64         rem = (year-100) % 400;
65         if (rem < 0) {
66                 cycles--;
67                 rem += 400;
68         }
69         if (!rem) {
70                 *is_leap = 1;
71                 centuries = 0;
72                 leaps = 0;
73         } else {
74                 if (rem >= 200) {
75                         if (rem >= 300) centuries = 3, rem -= 300;
76                         else centuries = 2, rem -= 200;
77                 } else {
78                         if (rem >= 100) centuries = 1, rem -= 100;
79                         else centuries = 0;
80                 }
81                 if (!rem) {
82                         *is_leap = 0;
83                         leaps = 0;
84                 } else {
85                         leaps = rem / 4U;
86                         rem %= 4U;
87                         *is_leap = !rem;
88                 }
89         }
90
91         leaps += 97*cycles + 24*centuries - *is_leap;
92
93         return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
94 }
95
96 static long long __tm_to_secs(const struct tm *tm)
97 {
98         int is_leap;
99         long long year = tm->tm_year;
100         int month = tm->tm_mon;
101         if (month >= 12 || month < 0) {
102                 int adj = month / 12;
103                 month %= 12;
104                 if (month < 0) {
105                         adj--;
106                         month += 12;
107                 }
108                 year += adj;
109         }
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;
115         t += tm->tm_sec;
116         return t;
117 }
118
119 static int __secs_to_tm(long long t, struct tm *tm)
120 {
121         long long days, secs;
122         int remdays, remsecs, remyears;
123         int qc_cycles, c_cycles, q_cycles;
124         int years, months;
125         int wday, yday, leap;
126         static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
127
128         /* Reject time_t values whose year would overflow int */
129         if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
130                 return -1;
131
132         secs = t - LEAPOCH;
133         days = secs / 86400;
134         remsecs = secs % 86400;
135         if (remsecs < 0) {
136                 remsecs += 86400;
137                 days--;
138         }
139
140         wday = (3+days)%7;
141         if (wday < 0) wday += 7;
142
143         qc_cycles = days / DAYS_PER_400Y;
144         remdays = days % DAYS_PER_400Y;
145         if (remdays < 0) {
146                 remdays += DAYS_PER_400Y;
147                 qc_cycles--;
148         }
149
150         c_cycles = remdays / DAYS_PER_100Y;
151         if (c_cycles == 4) c_cycles--;
152         remdays -= c_cycles * DAYS_PER_100Y;
153
154         q_cycles = remdays / DAYS_PER_4Y;
155         if (q_cycles == 25) q_cycles--;
156         remdays -= q_cycles * DAYS_PER_4Y;
157
158         remyears = remdays / 365;
159         if (remyears == 4) remyears--;
160         remdays -= remyears * 365;
161
162         leap = !remyears && (q_cycles || !c_cycles);
163         yday = remdays + 31 + 28 + leap;
164         if (yday >= 365+leap) yday -= 365+leap;
165
166         years = remyears + 4*q_cycles + 100*c_cycles + 400*qc_cycles;
167
168         for (months=0; days_in_month[months] <= remdays; months++)
169                 remdays -= days_in_month[months];
170
171         if (years+100 > INT_MAX || years+100 < INT_MIN)
172                 return -1;
173
174         tm->tm_year = years + 100;
175         tm->tm_mon = months + 2;
176         if (tm->tm_mon >= 12) {
177                 tm->tm_mon -=12;
178                 tm->tm_year++;
179         }
180         tm->tm_mday = remdays + 1;
181         tm->tm_wday = wday;
182         tm->tm_yday = yday;
183
184         tm->tm_hour = remsecs / 3600;
185         tm->tm_min = remsecs / 60 % 60;
186         tm->tm_sec = remsecs % 60;
187
188         return 0;
189 }
190
191 #ifdef _WIN32
192 struct tm *__gmtime_r(const time_t *t, struct tm *tm)
193 {
194         if (__secs_to_tm(*t, tm) < 0) {
195                 errno = EOVERFLOW;
196                 return 0;
197         }
198         tm->tm_isdst = 0;
199         return tm;
200 }
201 #endif
202
203 time_t timegm(struct tm *tm)
204 {
205         struct tm new;
206         long long t = __tm_to_secs(tm);
207         if (__secs_to_tm(t, &new) < 0) {
208                 errno = EOVERFLOW;
209                 return -1;
210         }
211 #if SIZEOF_TIME_T != 8
212         if (t > (long long)INT_MAX || t < (long long)INT_MIN) {
213                 errno = EOVERFLOW;
214                 return -1;
215         }
216 #endif
217         *tm = new;
218         tm->tm_isdst = 0;
219         return t;
220 }