summaryrefslogtreecommitdiffstats
path: root/unix
diff options
context:
space:
mode:
authorkennykb <kennykb@noemail.net>2004-05-14 21:41:03 (GMT)
committerkennykb <kennykb@noemail.net>2004-05-14 21:41:03 (GMT)
commit0fc22eb39ee1ee43227bb8189df101c172c9e29e (patch)
treec43bfd28868eb27530e825eb959a36371420aeb7 /unix
parent5a9804afc15a0f34fc5082a513adfd573e89020c (diff)
downloadtcl-0fc22eb39ee1ee43227bb8189df101c172c9e29e.zip
tcl-0fc22eb39ee1ee43227bb8189df101c172c9e29e.tar.gz
tcl-0fc22eb39ee1ee43227bb8189df101c172c9e29e.tar.bz2
2004-05-14 Kevin B. Kenny <kennykb@acm.org>
* generic/tclInt.decls: Promoted TclpLocaltime and TclpGmtime * generic/tclIntDecls.h: from Unix-specific stubs to the generic * generic/tclIntPlatDecls.h: internal Stubs table. Reran 'genstubs' * generic/tclStubInit.c: * unix/tclUnixPort.h: * generic/tclClock.c: Changed a buggy 'GMT' timezone specification to the correct 'GMT0'. [Bug #922848] * unix/tclUnixThrd.c: Moved TclpGmtime and TclpLocaltime to unix/tclUnixTime.c where they belong. * unix/tclUnixTime.c (TclpGmtime, TclpLocaltime, TclpGetTimeZone, ThreadSafeGMTime [removed], ThreadSafeLocalTime [removed], SetTZIfNecessary, CleanupMemory): Restructured to make sure that the same mutex protects all calls to localtime, gmtime, and tzset. Added a check in front of those calls to make sure that the TZ env var hasn't changed since the last call to tzset, and repeat tzset if necessary. [Bug #942078] Removed a buggy test of the Daylight Saving Time information in 'gettimeofday' in favor of applying 'localtime' to a known value. [Bug #922848] * tests/clock.test (clock-3.14): Added test to make sure that changes to $env(TZ) take effect immediately. * win/tclWinTime.c (TclpLocaltime, TclpGmtime): Added porting layer for 'localtime' and 'gmtime' calls. FossilOrigin-Name: 27f78ca91bc1b33c909fb1eac370a3d41c65fa59
Diffstat (limited to 'unix')
-rw-r--r--unix/tclUnixPort.h12
-rw-r--r--unix/tclUnixThrd.c47
-rw-r--r--unix/tclUnixTime.c247
3 files changed, 167 insertions, 139 deletions
diff --git a/unix/tclUnixPort.h b/unix/tclUnixPort.h
index d694488..c183985 100644
--- a/unix/tclUnixPort.h
+++ b/unix/tclUnixPort.h
@@ -19,7 +19,7 @@
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclUnixPort.h,v 1.27.2.2 2003/12/09 15:32:21 dkf Exp $
+ * RCS: @(#) $Id: tclUnixPort.h,v 1.27.2.3 2004/05/14 21:41:12 kennykb Exp $
*/
#ifndef _TCLUNIXPORT
@@ -564,12 +564,14 @@ EXTERN void TclpMutexInit _ANSI_ARGS_((TclpMutex *mPtr));
EXTERN void TclpMutexLock _ANSI_ARGS_((TclpMutex *mPtr));
EXTERN void TclpMutexUnlock _ANSI_ARGS_((TclpMutex *mPtr));
EXTERN Tcl_DirEntry * TclpReaddir(DIR *);
-EXTERN struct tm * TclpLocaltime(time_t *);
-EXTERN struct tm * TclpGmtime(time_t *);
+#ifndef TclpLocaltime
+EXTERN struct tm * TclpLocaltime(CONST time_t *);
+#endif
+#ifndef TclpGmtime
+EXTERN struct tm * TclpGmtime(CONST time_t *);
+#endif
EXTERN char * TclpInetNtoa(struct in_addr);
#define readdir(x) TclpReaddir(x)
-#define localtime(x) TclpLocaltime(x)
-#define gmtime(x) TclpGmtime(x)
#define inet_ntoa(x) TclpInetNtoa(x)
#undef TclOSreaddir
#define TclOSreaddir(x) TclpReaddir(x)
diff --git a/unix/tclUnixThrd.c b/unix/tclUnixThrd.c
index aa26c51..04eacf4 100644
--- a/unix/tclUnixThrd.c
+++ b/unix/tclUnixThrd.c
@@ -21,8 +21,6 @@
typedef struct ThreadSpecificData {
char nabuf[16];
- struct tm gtbuf;
- struct tm ltbuf;
struct {
Tcl_DirEntry ent;
char name[MAXNAMLEN+1];
@@ -864,51 +862,6 @@ TclpReaddir(DIR * dir)
#endif
return ent;
}
-
-#if defined(TCL_THREADS) && (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R))
-TCL_DECLARE_MUTEX( tmMutex )
-#undef localtime
-#undef gmtime
-#endif
-
-struct tm *
-TclpLocaltime(time_t * clock)
-{
-#ifdef TCL_THREADS
- ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
-
-#ifdef HAVE_LOCALTIME_R
- return localtime_r(clock, &tsdPtr->ltbuf);
-#else
- Tcl_MutexLock( &tmMutex );
- memcpy( (VOID *) &tsdPtr->ltbuf, (VOID *) localtime( clock ), sizeof (struct tm) );
- Tcl_MutexUnlock( &tmMutex );
- return &tsdPtr->ltbuf;
-#endif
-#else
- return localtime(clock);
-#endif
-}
-
-struct tm *
-TclpGmtime(time_t * clock)
-{
-#ifdef TCL_THREADS
- ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
-
-#ifdef HAVE_GMTIME_R
- return gmtime_r(clock, &tsdPtr->gtbuf);
-#else
- Tcl_MutexLock( &tmMutex );
- memcpy( (VOID *) &tsdPtr->gtbuf, (VOID *) gmtime( clock ), sizeof (struct tm) );
- Tcl_MutexUnlock( &tmMutex );
- return &tsdPtr->gtbuf;
-#endif
-#else
- return gmtime(clock);
-#endif
-}
-
char *
TclpInetNtoa(struct in_addr addr)
{
diff --git a/unix/tclUnixTime.c b/unix/tclUnixTime.c
index fc72f35..4f99801 100644
--- a/unix/tclUnixTime.c
+++ b/unix/tclUnixTime.c
@@ -9,7 +9,7 @@
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
- * RCS: @(#) $Id: tclUnixTime.c,v 1.15 2002/07/19 12:31:10 dkf Exp $
+ * RCS: @(#) $Id: tclUnixTime.c,v 1.15.2.1 2004/05/14 21:41:12 kennykb Exp $
*/
#include "tclInt.h"
@@ -25,22 +25,27 @@
*/
static Tcl_ThreadDataKey tmKey;
+typedef struct ThreadSpecificData {
+ struct tm gmtime_buf;
+ struct tm localtime_buf;
+} ThreadSpecificData;
/*
* If we fall back on the thread-unsafe versions of gmtime and localtime,
* use this mutex to try to protect them.
*/
-#if !defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)
TCL_DECLARE_MUTEX(tmMutex)
-#endif
-/*
- * Forward declarations for procedures defined later in this file.
- */
+static char* lastTZ = NULL; /* Holds the last setting of the
+ * TZ environment variable, or an
+ * empty string if the variable was
+ * not set. */
+
+/* Static functions declared in this file */
-static struct tm *ThreadSafeGMTime _ANSI_ARGS_(( CONST time_t* ));
-static struct tm *ThreadSafeLocalTime _ANSI_ARGS_(( CONST time_t* ));
+static void SetTZIfNecessary _ANSI_ARGS_((void));
+static void CleanupMemory _ANSI_ARGS_((ClientData));
/*
*-----------------------------------------------------------------------------
@@ -129,18 +134,22 @@ TclpGetTimeZone (currentTime)
unsigned long currentTime;
{
/*
- * Determine how a timezone is obtained from "struct tm". If there is no
- * time zone in this struct (very lame) then use the timezone variable.
- * This is done in a way to make the timezone variable the method of last
- * resort, as some systems have it in addition to a field in "struct tm".
- * The gettimeofday system call can also be used to determine the time
- * zone.
+ * We prefer first to use the time zone in "struct tm" if the
+ * structure contains such a member. Following that, we try
+ * to locate the external 'timezone' variable and use its value.
+ * If both of those methods fail, we attempt to convert a known
+ * time to local time and use the difference from UTC as the local
+ * time zone. In all cases, we need to undo any Daylight Saving Time
+ * adjustment.
*/
#if defined(HAVE_TM_TZADJ)
# define TCL_GOT_TIMEZONE
+
+ /* Struct tm contains tm_tzadj - that value may be used. */
+
time_t curTime = (time_t) currentTime;
- struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime);
+ struct tm *timeDataPtr = TclpLocaltime(&curTime);
int timeZone;
timeZone = timeDataPtr->tm_tzadj / 60;
@@ -149,12 +158,16 @@ TclpGetTimeZone (currentTime)
}
return timeZone;
+
#endif
#if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
# define TCL_GOT_TIMEZONE
+
+ /* Struct tm contains tm_gmtoff - that value may be used. */
+
time_t curTime = (time_t) currentTime;
- struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime);
+ struct tm *timeDataPtr = TclpLocaltime(&curTime);
int timeZone;
timeZone = -(timeDataPtr->tm_gmtoff / 60);
@@ -163,46 +176,17 @@ TclpGetTimeZone (currentTime)
}
return timeZone;
-#endif
-#if defined(USE_DELTA_FOR_TZ)
-#define TCL_GOT_TIMEZONE 1
- /*
- * This hack replaces using global var timezone or gettimeofday
- * in situations where they are buggy such as on AIX when libbsd.a
- * is linked in.
- */
-
- int timeZone;
- time_t tt;
- struct tm *stm;
- tt = 849268800L; /* 1996-11-29 12:00:00 GMT */
- stm = ThreadSafeLocalTime(&tt); /* eg 1996-11-29 6:00:00 CST6CDT */
- /* The calculation below assumes a max of +12 or -12 hours from GMT */
- timeZone = (12 - stm->tm_hour)*60 + (0 - stm->tm_min);
- return timeZone; /* eg +360 for CST6CDT */
#endif
- /*
- * Must prefer timezone variable over gettimeofday, as gettimeofday does
- * not return timezone information on many systems that have moved this
- * information outside of the kernel.
- */
-
-#if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
+#if defined(HAVE_TIMEZONE_VAR) && !defined(TCL_GOT_TIMEZONE) && !defined(USE_DELTA_FOR_TZ)
# define TCL_GOT_TIMEZONE
- static int setTZ = 0;
-#ifdef TCL_THREADS
- static Tcl_Mutex tzMutex;
-#endif
+
int timeZone;
- Tcl_MutexLock(&tzMutex);
- if (!setTZ) {
- tzset();
- setTZ = 1;
- }
- Tcl_MutexUnlock(&tzMutex);
+ /* The 'timezone' external var is present and may be used. */
+
+ SetTZIfNecessary();
/*
* Note: this is not a typo in "timezone" below! See tzset
@@ -210,30 +194,36 @@ TclpGetTimeZone (currentTime)
*/
timeZone = timezone / 60;
-
return timeZone;
+
#endif
-#if !defined(NO_GETTOD) && !defined (TCL_GOT_TIMEZONE)
-# define TCL_GOT_TIMEZONE
- struct timeval tv;
- struct timezone tz;
- int timeZone;
+#if !defined(TCL_GOT_TIMEZONE)
+#define TCL_GOT_TIMEZONE 1
+ /*
+ * Fallback - determine time zone with a known reference time.
+ */
- gettimeofday(&tv, &tz);
- timeZone = tz.tz_minuteswest;
- if (tz.tz_dsttime) {
+ int timeZone;
+ time_t tt;
+ struct tm *stm;
+ tt = 849268800L; /* 1996-11-29 12:00:00 GMT */
+ stm = TclpLocaltime(&tt); /* eg 1996-11-29 6:00:00 CST6CDT */
+ /* The calculation below assumes a max of +12 or -12 hours from GMT */
+ timeZone = (12 - stm->tm_hour)*60 + (0 - stm->tm_min);
+ if ( stm -> tm_isdst ) {
timeZone += 60;
}
-
- return timeZone;
+ return timeZone; /* eg +360 for CST6CDT */
#endif
#ifndef TCL_GOT_TIMEZONE
/*
* Cause compile error, we don't know how to get timezone.
*/
- error: autoconf did not figure out how to determine the timezone.
+
+#error autoconf did not figure out how to determine the timezone.
+
#endif
}
@@ -293,9 +283,9 @@ TclpGetDate(time, useGMT)
CONST time_t *tp = (CONST time_t *)time;
if (useGMT) {
- return ThreadSafeGMTime(tp);
+ return TclpGmtime(tp);
} else {
- return ThreadSafeLocalTime(tp);
+ return TclpLocaltime(tp);
}
}
@@ -340,7 +330,7 @@ TclpStrftime(s, maxsize, format, t, useGMT)
/*
*----------------------------------------------------------------------
*
- * ThreadSafeGMTime --
+ * TclpGmtime --
*
* Wrapper around the 'gmtime' library function to make it thread
* safe.
@@ -354,33 +344,42 @@ TclpStrftime(s, maxsize, format, t, useGMT)
*----------------------------------------------------------------------
*/
-static struct tm *
-ThreadSafeGMTime(timePtr)
+struct tm *
+TclpGmtime( timePtr )
CONST time_t *timePtr; /* Pointer to the number of seconds
- * since the local system's epoch
- */
+ * since the local system's epoch */
{
/*
* Get a thread-local buffer to hold the returned time.
*/
- struct tm *tmPtr = (struct tm *)
- Tcl_GetThreadData(&tmKey, sizeof(struct tm));
+ ThreadSpecificData *tsdPtr = TCL_TSD_INIT( &tmKey );
#ifdef HAVE_GMTIME_R
- gmtime_r(timePtr, tmPtr);
+ gmtime_r(timePtr, &( tsdPtr->gmtime_buf ));
#else
- Tcl_MutexLock(&tmMutex);
- memcpy((VOID *) tmPtr, (VOID *) gmtime(timePtr), sizeof(struct tm));
- Tcl_MutexUnlock(&tmMutex);
+ Tcl_MutexLock( &tmMutex );
+ memcpy( (VOID *) &( tsdPtr->gmtime_buf ),
+ (VOID *) gmtime( timePtr ),
+ sizeof( struct tm ) );
+ Tcl_MutexUnlock( &tmMutex );
#endif
- return tmPtr;
+ return &( tsdPtr->gmtime_buf );
+}
+/*
+ * Forwarder for obsolete item in Stubs
+ */
+struct tm*
+TclpGmtime_unix( timePtr )
+ CONST time_t* timePtr;
+{
+ return TclpGmtime( timePtr );
}
/*
*----------------------------------------------------------------------
*
- * ThreadSafeLocalTime --
+ * TclpLocaltime --
*
* Wrapper around the 'localtime' library function to make it thread
* safe.
@@ -394,25 +393,99 @@ ThreadSafeGMTime(timePtr)
*----------------------------------------------------------------------
*/
-static struct tm *
-ThreadSafeLocalTime(timePtr)
+struct tm *
+TclpLocaltime(timePtr)
CONST time_t *timePtr; /* Pointer to the number of seconds
- * since the local system's epoch
- */
+ * since the local system's epoch */
{
/*
* Get a thread-local buffer to hold the returned time.
*/
- struct tm *tmPtr = (struct tm *)
- Tcl_GetThreadData(&tmKey, sizeof(struct tm));
+ ThreadSpecificData *tsdPtr = TCL_TSD_INIT( &tmKey );
+ SetTZIfNecessary();
#ifdef HAVE_LOCALTIME_R
- localtime_r(timePtr, tmPtr);
+ localtime_r( timePtr, &( tsdPtr->localtime_buf ) );
#else
+ Tcl_MutexLock( &tmMutex );
+ memcpy( (VOID *) &( tsdPtr -> localtime_buf ),
+ (VOID *) localtime( timePtr ),
+ sizeof( struct tm ) );
+ Tcl_MutexUnlock( &tmMutex );
+#endif
+ return &( tsdPtr->localtime_buf );
+}
+/*
+ * Forwarder for obsolete item in Stubs
+ */
+struct tm*
+TclpLocaltime_unix( timePtr )
+ CONST time_t* timePtr;
+{
+ return TclpLocaltime( timePtr );
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * SetTZIfNecessary --
+ *
+ * Determines whether a call to 'tzset' is needed prior to the
+ * next call to 'localtime' or examination of the 'timezone' variable.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * If 'tzset' has never been called in the current process, or if
+ * the value of the environment variable TZ has changed since the
+ * last call to 'tzset', then 'tzset' is called again.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+SetTZIfNecessary() {
+
+ CONST char* newTZ = getenv( "TZ" );
Tcl_MutexLock(&tmMutex);
- memcpy((VOID *) tmPtr, (VOID *) localtime(timePtr), sizeof(struct tm));
+ if ( newTZ == NULL ) {
+ newTZ = "";
+ }
+ if ( lastTZ == NULL || strcmp( lastTZ, newTZ ) ) {
+ tzset();
+ if ( lastTZ == NULL ) {
+ Tcl_CreateExitHandler( CleanupMemory, (ClientData) NULL );
+ } else {
+ Tcl_Free( lastTZ );
+ }
+ lastTZ = Tcl_Alloc( strlen( newTZ ) + 1 );
+ strcpy( lastTZ, newTZ );
+ }
Tcl_MutexUnlock(&tmMutex);
-#endif
- return tmPtr;
+
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * CleanupMemory --
+ *
+ * Releases the private copy of the TZ environment variable
+ * upon exit from Tcl.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * Frees allocated memory.
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+CleanupMemory( ClientData ignored )
+{
+ Tcl_Free( lastTZ );
}