From dev-return-4160-apmail-apr-dev-archive=apr.apache.org@apr.apache.org Thu Aug 30 23:22:16 2001 Return-Path: Delivered-To: apmail-apr-dev-archive@apr.apache.org Received: (qmail 9062 invoked by uid 500); 30 Aug 2001 23:22:16 -0000 Mailing-List: contact dev-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Delivered-To: mailing list dev@apr.apache.org Received: (qmail 9051 invoked from network); 30 Aug 2001 23:22:16 -0000 Date: Thu, 30 Aug 2001 16:19:20 -0700 From: Brian Pane Subject: [PATCH] performance fix for time offset computation To: dev@apr.apache.org Message-id: <3B8EC9F8.30202@pacbell.net> MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_+ZTeocGpzmGVl1gC1JdUYg)" X-Accept-Language: en-us User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.3) Gecko/20010801 References: <3B8B71E2.900@pacbell.net> <3B8BDFCD.8050305@pacbell.net> <20010828141251.C1437@waka.ebuilt.net> <3B8C184A.4050502@pacbell.net> <5.1.0.14.2.20010829093747.00a7b310@mail.charter.net> <5.1.0.14.2.20010829101518.00a6ce00@mail.charter.net> <20010829151817.B1737@waka.ebuilt.net> <3B8D730C.70200@pacbell.net> <20010830135613.H17570@ebuilt.com> <20010830154438.C2142@waka.ebuilt.net> X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N This is a multi-part message in MIME format. --Boundary_(ID_+ZTeocGpzmGVl1gC1JdUYg) Content-type: text/plain; charset=us-ascii; format=flowed Content-transfer-encoding: 7BIT Here's a revised copy of the patch--same code, but with comments added. It depends on the new include file apr/include/arch/unix/internal_time.h. --Brian --Boundary_(ID_+ZTeocGpzmGVl1gC1JdUYg) Content-type: text/plain; name=time_diff Content-transfer-encoding: 7BIT Content-disposition: inline; filename=time_diff Index: apr/misc/unix/start.c =================================================================== RCS file: /home/cvspublic/apr/misc/unix/start.c,v retrieving revision 1.52 diff -u -r1.52 start.c --- apr/misc/unix/start.c 2001/08/02 22:27:02 1.52 +++ apr/misc/unix/start.c 2001/08/30 23:15:23 @@ -59,6 +59,7 @@ #include "misc.h" /* for WSAHighByte / WSALowByte */ #include "locks.h" /* for apr_unix_setup_lock() */ +#include "internal_time.h" static int initialized = 0; @@ -83,6 +84,7 @@ #if !defined(BEOS) && !defined(OS2) && !defined(WIN32) && !defined(NETWARE) apr_unix_setup_lock(); + apr_unix_setup_time(); #elif defined WIN32 || defined(NETWARE) iVersionRequested = MAKEWORD(WSAHighByte, WSALowByte); err = WSAStartup((WORD) iVersionRequested, &wsaData); Index: apr/time/unix/time.c =================================================================== RCS file: /home/cvspublic/apr/time/unix/time.c,v retrieving revision 1.51 diff -u -r1.51 time.c --- apr/time/unix/time.c 2001/08/10 21:04:49 1.51 +++ apr/time/unix/time.c 2001/08/30 23:15:23 @@ -70,6 +70,10 @@ #endif /* End System Headers */ +#if !defined(HAVE_GMTOFF) && !defined(HAVE___OFFSET) +static apr_int32_t server_gmt_offset; +#endif /* if !defined(HAVE_GMTOFF) && !defined(HAVE___OFFSET) */ + static apr_int32_t get_offset(struct tm *tm) { #ifdef HAVE_GMTOFF @@ -77,29 +81,7 @@ #elif defined(HAVE___OFFSET) return tm->__tm_gmtoff; #else - /* We don't have an offset field to use, so calculate it. - mktime() is the inverse of localtime(); so, presumably, - passing in a struct tm made by gmtime() let's us calculate - the true GMT offset. However, there's a catch: if daylight - savings is in effect, gmtime()will set the tm_isdst field - and confuse mktime() into returning a time that's offset - by one hour. In that case, we must adjust the calculated GMT - offset. */ - { - time_t t1 = time(0), t2 = 0; - struct tm t; - int was_dst; - -#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) - gmtime_r(&t1, &t); -#else - t = *gmtime(&t1); -#endif - was_dst = (t.tm_isdst > 0); - t.tm_isdst = -1; - t2 = mktime(&t); - return (apr_int32_t) difftime(t1, t2) + (was_dst ? 3600 : 0); - } + return server_gmt_offset; #endif } @@ -308,3 +290,50 @@ return APR_SUCCESS; } #endif + +APR_DECLARE(void) apr_unix_setup_time(void) +{ +#if !defined(HAVE_GMTOFF) && !defined(HAVE___OFFSET) + /* Precompute the offset from GMT on systems where it's not + in struct tm. + + Note: This offset is normalized to be independent of daylight + savings time; if the calculation happens to be done in a + time/place where a daylight savings adjustment is in effect, + the returned offset has the same value that it would have + in the same location if dsylight savings were not in effect. + The reason for this is that the returned offset can be + applied to a past or future timestamp in explode_time(), + so the DST adjustment obtained from the current time won't + necessarily be applicable. + + mktime() is the inverse of localtime(); so, presumably, + passing in a struct tm made by gmtime() let's us calculate + the true GMT offset. However, there's a catch: if daylight + savings is in effect, gmtime()will set the tm_isdst field + and confuse mktime() into returning a time that's offset + by one hour. In that case, we must adjust the calculated GMT + offset. + + */ + + struct timeval now; + time_t t1, t2; + struct tm t; + int was_dst; + + gettimeofday(&now, NULL); + t1 = now.tv_sec; + t2 = 0; + +#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS) + gmtime_r(&t1, &t); +#else + t = *gmtime(&t1); +#endif + was_dst = (t.tm_isdst > 0); + t.tm_isdst = -1; + t2 = mktime(&t); + server_gmt_offset = (apr_int32_t) difftime(t1, t2) + (was_dst ? 3600 : 0); +#endif +} --Boundary_(ID_+ZTeocGpzmGVl1gC1JdUYg) Content-type: text/plain; name=internal_time.h Content-transfer-encoding: 7BIT Content-disposition: inline; filename=internal_time.h /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ #ifndef TIME_INTERNAL_H #define TIME_INTERNAL_H #include "apr.h" void apr_unix_setup_time(void); #endif /* TIME_INTERNAL_H */ --Boundary_(ID_+ZTeocGpzmGVl1gC1JdUYg)--