Provide timegm replacement and android build fix

This commit is contained in:
Tatsuhiro Tsujikawa 2013-03-07 21:17:55 +09:00
parent 61726d5e74
commit add067ed7e
5 changed files with 143 additions and 7 deletions

View File

@ -85,16 +85,26 @@ LIBS_OLD=$LIBS
# Search for dlsym function, which is used in tests. Linux needs -ldl,
# but netbsd does not need it.
AC_SEARCH_LIBS([dlsym], [dl])
TESTS_LIBS=$LIBS $TESTS_LIBS
TESTS_LIBS="$LIBS $TESTS_LIBS"
LIBS=$LIBS_OLD
LIBS_OLD=$LIBS
AC_SEARCH_LIBS([clock_gettime], [rt],
[AC_DEFINE([HAVE_CLOCK_GETTIME], [1],
[Define to 1 if you have the `clock_gettime`.])])
SRC_LIBS=$LIBS $SRC_LIBS
SRC_LIBS="$LIBS $SRC_LIBS"
LIBS=$LIBS_OLD
case "$host" in
*android*)
# android does not need -pthread, but needs followng 2 libs for C++
SRC_LIBS="$SRC_LIBS -lstdc++ -lsupc++"
;;
*)
SRC_LIBS="$SRC_LIBS -pthread"
;;
esac
# zlib
PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.3])
LIBS="$ZLIB_LIBS $LIBS"
@ -211,6 +221,7 @@ AC_CHECK_FUNCS([ \
getpwnam \
memmove \
memset \
timegm \
])
AX_HAVE_EPOLL([have_epoll=yes], [have_epoll=no])

View File

@ -26,8 +26,8 @@ if ENABLE_SRC
AM_CFLAGS = -Wall
AM_CPPFLAGS = -Wall -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes \
@OPENSSL_CFLAGS@ @XML_CPPFLAGS@ @LIBEVENT_OPENSSL_CFLAGS@ @DEFS@
AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ @SRC_LIBS@ \
-pthread
AM_LDFLAGS = @OPENSSL_LIBS@ @XML_LIBS@ @LIBEVENT_OPENSSL_LIBS@ @SRC_LIBS@
LDADD = $(top_builddir)/lib/libspdylay.la
bin_PROGRAMS = spdycat spdyd
@ -38,8 +38,8 @@ if HAVE_LIBEVENT_OPENSSL
bin_PROGRAMS += shrpx
endif # HAVE_LIBEVENT_OPENSSL
HELPER_OBJECTS = util.cc spdylay_ssl.cc
HELPER_HFILES = util.h spdylay_ssl.h spdylay_config.h
HELPER_OBJECTS = util.cc timegm.c spdylay_ssl.cc
HELPER_HFILES = util.h timegm.h spdylay_ssl.h spdylay_config.h
EVENT_OBJECTS =
EVENT_HFILES = EventPoll.h EventPollEvent.h
@ -75,7 +75,7 @@ spdyd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} \
if HAVE_LIBEVENT_OPENSSL
SHRPX_SRCS = \
util.cc util.h base64.h \
util.cc util.h timegm.c timegm.h base64.h \
shrpx_config.cc shrpx_config.h \
shrpx_error.h \
shrpx_listen_handler.cc shrpx_listen_handler.h \

75
src/timegm.c Normal file
View File

@ -0,0 +1,75 @@
/*
* Spdylay - SPDY Library
*
* 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"
#ifndef HAVE_TIMEGM
#include <stdint.h>
/* Counter the number of leap year in the range [0, y). The |y| is the
year, including century (e.g., 2012) */
static int count_leap_year(int y)
{
y -= 1;
return y/4-y/100+y/400;
}
/* Returns nonzero if the |y| is the leap year. The |y| is the year,
including century (e.g., 2012) */
static int is_leap_year(int y)
{
return y%4 == 0 && (y%100 != 0 || y%400 == 0);
}
/* The number of days before ith month begins */
static int daysum[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
};
/* Based on the algorithm of Python 2.7 calendar.timegm. */
time_t timegm(struct tm *tm)
{
int days;
int num_leap_year;
int64_t t;
if(tm->tm_mon > 11) {
return -1;
}
num_leap_year = count_leap_year(tm->tm_year + 1900) - count_leap_year(1970);
days = (tm->tm_year - 70) * 365 +
num_leap_year + daysum[tm->tm_mon] + tm->tm_mday-1;
if(tm->tm_mon >= 2 && is_leap_year(tm->tm_year + 1900)) {
++days;
}
t = ((int64_t)days * 24 + tm->tm_hour) * 3600 + tm->tm_min * 60 + tm->tm_sec;
if(sizeof(time_t) == 4) {
if(t < INT32_MIN || t > INT32_MAX) {
return -1;
}
}
return t;
}
#endif /* !HAVE_TIMEGM */

48
src/timegm.h Normal file
View File

@ -0,0 +1,48 @@
/*
* Spdylay - SPDY Library
*
* 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.
*/
#ifndef TIMEGM_H
#define TIMEGM_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif // HAVE_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <time.h>
#ifndef HAVE_TIMEGM
time_t timegm(struct tm *tm);
#endif /* HAVE_TIMEGM */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* TIMEGM_H */

View File

@ -29,6 +29,8 @@
#include <cstdio>
#include <cstring>
#include "timegm.h"
namespace spdylay {
namespace util {