apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Pane <bp...@pacbell.net>
Subject [PATCH] performance fix for time offset computation
Date Tue, 28 Aug 2001 18:15:41 GMT
Here's a new version of the get_offset patch that initializes
the TZ offset from apr_initialize.  I've attached the new include
file that it uses, apr/include/arch/unix/internal_time.h

--Brian

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/28 18:11:54
@@ -60,7 +60,9 @@
 #include "misc.h"       /* for WSAHighByte / WSALowByte */
 #include "locks.h"      /* for apr_unix_setup_lock() */
 
+#include "internal_time.h"
 
+
 static int initialized = 0;
 static apr_pool_t *global_apr_pool;
 
@@ -83,6 +85,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/28 18:11:54
@@ -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,30 +81,8 @@
 #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);
+    return server_gmt_offset;
 #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);
-    }
-#endif
 }
 
 APR_DECLARE(apr_status_t) apr_ansi_time_to_apr_time(apr_time_t *result,
@@ -308,3 +290,37 @@
   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.
+       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
+}




Mime
View raw message