2013-03-07 13:17:55 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-03-07 13:17:55 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "timegm.h"
|
|
|
|
|
2015-05-13 17:17:45 +02:00
|
|
|
#include <inttypes.h>
|
2013-03-07 13:17:55 +01:00
|
|
|
|
|
|
|
/* Counter the number of leap year in the range [0, y). The |y| is the
|
|
|
|
year, including century (e.g., 2012) */
|
2014-11-27 15:39:04 +01:00
|
|
|
static int count_leap_year(int y) {
|
2013-03-07 13:17:55 +01:00
|
|
|
y -= 1;
|
2014-11-27 15:39:04 +01:00
|
|
|
return y / 4 - y / 100 + y / 400;
|
2013-03-07 13:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Based on the algorithm of Python 2.7 calendar.timegm. */
|
2014-11-27 15:39:04 +01:00
|
|
|
time_t timegm(struct tm *tm) {
|
2013-03-07 13:17:55 +01:00
|
|
|
int days;
|
|
|
|
int num_leap_year;
|
|
|
|
int64_t t;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (tm->tm_mon > 11) {
|
2013-03-07 13:17:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970);
|
2015-06-29 16:14:54 +02:00
|
|
|
days = (tm->tm_year - 70) * 365 + num_leap_year + tm->tm_yday;
|
2013-03-07 13:17:55 +01:00
|
|
|
t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec;
|
2015-06-29 17:01:26 +02:00
|
|
|
|
|
|
|
#if SIZEOF_TIME_T == 4
|
|
|
|
if (t < INT32_MIN || t > INT32_MAX) {
|
|
|
|
return -1;
|
2013-03-07 13:17:55 +01:00
|
|
|
}
|
2015-06-29 17:01:26 +02:00
|
|
|
#endif /* SIZEOF_TIME_T == 4 */
|
|
|
|
|
2014-07-06 16:32:08 +02:00
|
|
|
return (time_t)t;
|
2013-03-07 13:17:55 +01:00
|
|
|
}
|