access: bluray: workaround BDJO update checks
[vlc.git] / compat / timegm.c
blob27a3df3c3f2a0a20dba3090bbb404cdb7a7dac0b
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <stdbool.h>
27 #include <time.h>
29 static bool is_leap_year(unsigned y)
31 if (y % 4)
32 return false;
33 if (y % 100)
34 return true;
35 if (y % 400)
36 return false;
37 return true;
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 */)
52 return -1;
54 /* Count the number of days */
55 unsigned t = 365 * (tm->tm_year - 70)
56 + ydays[tm->tm_mon] + (tm->tm_mday - 1);
58 /* TODO: unroll */
59 for (int i = 70; i < tm->tm_year; i++)
60 t += is_leap_year(1900 + i);
62 if (tm->tm_mon > 1)
63 t += is_leap_year(1900 + tm->tm_year);
65 t *= 24;
66 t += tm->tm_hour;
67 t *= 60;
68 t += tm->tm_min;
69 t *= 60;
70 t += tm->tm_sec;
71 return t;