1 /*****************************************************************************
2 * timegm.c: BSD/GNU timegm() replacement
3 *****************************************************************************
4 * Copyright © 2007 Laurent Aimar
5 * Copyright © 2015 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
29 static bool is_leap_year(unsigned y
)
40 time_t timegm(struct tm
*tm
)
42 static const unsigned ydays
[12 + 1] = {
43 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
46 if (tm
->tm_year
< 70 /* FIXME: (negative) dates before Epoch */
47 || tm
->tm_mon
< 0 || tm
->tm_mon
> 11
48 || tm
->tm_mday
< 1 || tm
->tm_mday
> 31
49 || tm
->tm_hour
< 0 || tm
->tm_hour
> 23
50 || tm
->tm_min
< 0 || tm
->tm_min
> 59
51 || tm
->tm_sec
< 0 || tm
->tm_sec
> 60 /* mind the leap second */)
54 /* Count the number of days */
55 unsigned t
= 365 * (tm
->tm_year
- 70)
56 + ydays
[tm
->tm_mon
] + (tm
->tm_mday
- 1);
59 for (int i
= 70; i
< tm
->tm_year
; i
++)
60 t
+= is_leap_year(1900 + i
);
63 t
+= is_leap_year(1900 + tm
->tm_year
);