4 * Contains Unix specific versions of Tcl functions that
5 * obtain time values from the operating system.
7 * Copyright (c) 1995 Sun Microsystems, Inc.
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
18 #define TM_YEAR_BASE 1900
19 #define IsLeapYear(x) ((x % 4 == 0) && (x % 100 != 0 || x % 400 == 0))
22 * TclpGetDate is coded to return a pointer to a 'struct tm'. For
23 * thread safety, this structure must be in thread-specific data.
24 * The 'tmKey' variable is the key to this buffer.
27 static Tcl_ThreadDataKey tmKey;
30 * If we fall back on the thread-unsafe versions of gmtime and localtime,
31 * use this mutex to try to protect them.
34 #if !defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)
35 TCL_DECLARE_MUTEX(tmMutex)
39 * Forward declarations for procedures defined later in this file.
42 static struct tm *ThreadSafeGMTime _ANSI_ARGS_(( CONST time_t* ));
43 static struct tm *ThreadSafeLocalTime _ANSI_ARGS_(( CONST time_t* ));
46 *-----------------------------------------------------------------------------
50 * This procedure returns the number of seconds from the epoch. On
51 * most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
54 * Number of seconds from the epoch.
59 *-----------------------------------------------------------------------------
65 return time((time_t *) NULL);
69 *-----------------------------------------------------------------------------
73 * This procedure returns a value that represents the highest resolution
74 * clock available on the system. There are no garantees on what the
75 * resolution will be. In Tcl we will call this value a "click". The
76 * start time is also system dependant.
79 * Number of clicks from some start time.
84 *-----------------------------------------------------------------------------
99 now = (unsigned long) times(&dummy);
101 gettimeofday(&date, &tz);
102 now = date.tv_sec*1000000 + date.tv_usec;
109 *----------------------------------------------------------------------
113 * Determines the current timezone. The method varies wildly
114 * between different platform implementations, so its hidden in
118 * The return value is the local time zone, measured in
119 * minutes away from GMT (-ve for east, +ve for west).
124 *----------------------------------------------------------------------
128 TclpGetTimeZone (currentTime)
129 unsigned long currentTime;
132 * Determine how a timezone is obtained from "struct tm". If there is no
133 * time zone in this struct (very lame) then use the timezone variable.
134 * This is done in a way to make the timezone variable the method of last
135 * resort, as some systems have it in addition to a field in "struct tm".
136 * The gettimeofday system call can also be used to determine the time
140 #if defined(HAVE_TM_TZADJ)
141 # define TCL_GOT_TIMEZONE
142 time_t curTime = (time_t) currentTime;
143 struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime);
146 timeZone = timeDataPtr->tm_tzadj / 60;
147 if (timeDataPtr->tm_isdst) {
154 #if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
155 # define TCL_GOT_TIMEZONE
156 time_t curTime = (time_t) currentTime;
157 struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime);
160 timeZone = -(timeDataPtr->tm_gmtoff / 60);
161 if (timeDataPtr->tm_isdst) {
168 #if defined(USE_DELTA_FOR_TZ)
169 #define TCL_GOT_TIMEZONE 1
171 * This hack replaces using global var timezone or gettimeofday
172 * in situations where they are buggy such as on AIX when libbsd.a
179 tt = 849268800L; /* 1996-11-29 12:00:00 GMT */
180 stm = ThreadSafeLocalTime(&tt); /* eg 1996-11-29 6:00:00 CST6CDT */
181 /* The calculation below assumes a max of +12 or -12 hours from GMT */
182 timeZone = (12 - stm->tm_hour)*60 + (0 - stm->tm_min);
183 return timeZone; /* eg +360 for CST6CDT */
187 * Must prefer timezone variable over gettimeofday, as gettimeofday does
188 * not return timezone information on many systems that have moved this
189 * information outside of the kernel.
192 #if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
193 # define TCL_GOT_TIMEZONE
194 static int setTZ = 0;
196 static Tcl_Mutex tzMutex;
200 Tcl_MutexLock(&tzMutex);
205 Tcl_MutexUnlock(&tzMutex);
208 * Note: this is not a typo in "timezone" below! See tzset
209 * documentation for details.
212 timeZone = timezone / 60;
217 #if !defined(NO_GETTOD) && !defined (TCL_GOT_TIMEZONE)
218 # define TCL_GOT_TIMEZONE
223 gettimeofday(&tv, &tz);
224 timeZone = tz.tz_minuteswest;
232 #ifndef TCL_GOT_TIMEZONE
234 * Cause compile error, we don't know how to get timezone.
236 error: autoconf did not figure out how to determine the timezone.
242 *----------------------------------------------------------------------
246 * Gets the current system time in seconds and microseconds
247 * since the beginning of the epoch: 00:00 UCT, January 1, 1970.
250 * Returns the current time in timePtr.
255 *----------------------------------------------------------------------
260 Tcl_Time *timePtr; /* Location to store time information. */
265 (void) gettimeofday(&tv, &tz);
266 timePtr->sec = tv.tv_sec;
267 timePtr->usec = tv.tv_usec;
271 *----------------------------------------------------------------------
275 * This function converts between seconds and struct tm. If
276 * useGMT is true, then the returned date will be in Greenwich
277 * Mean Time (GMT). Otherwise, it will be in the local time zone.
280 * Returns a static tm structure.
285 *----------------------------------------------------------------------
289 TclpGetDate(time, useGMT)
293 CONST time_t *tp = (CONST time_t *)time;
296 return ThreadSafeGMTime(tp);
298 return ThreadSafeLocalTime(tp);
303 *----------------------------------------------------------------------
307 * On Unix, we can safely call the native strftime implementation,
308 * and also ignore the useGMT parameter.
311 * The normal strftime result.
316 *----------------------------------------------------------------------
320 TclpStrftime(s, maxsize, format, t, useGMT)
327 if (format[0] == '%' && format[1] == 'Q') {
328 /* Format as a stardate */
329 sprintf(s, "Stardate %2d%03d.%01d",
330 (((t->tm_year + TM_YEAR_BASE) + 377) - 2323),
331 (((t->tm_yday + 1) * 1000) /
332 (365 + IsLeapYear((t->tm_year + TM_YEAR_BASE)))),
333 (((t->tm_hour * 60) + t->tm_min)/144));
336 setlocale(LC_TIME, "");
337 return strftime(s, maxsize, format, t);
341 *----------------------------------------------------------------------
343 * ThreadSafeGMTime --
345 * Wrapper around the 'gmtime' library function to make it thread
349 * Returns a pointer to a 'struct tm' in thread-specific data.
352 * Invokes gmtime or gmtime_r as appropriate.
354 *----------------------------------------------------------------------
358 ThreadSafeGMTime(timePtr)
359 CONST time_t *timePtr; /* Pointer to the number of seconds
360 * since the local system's epoch
365 * Get a thread-local buffer to hold the returned time.
368 struct tm *tmPtr = (struct tm *)
369 Tcl_GetThreadData(&tmKey, sizeof(struct tm));
371 gmtime_r(timePtr, tmPtr);
373 Tcl_MutexLock(&tmMutex);
374 memcpy((VOID *) tmPtr, (VOID *) gmtime(timePtr), sizeof(struct tm));
375 Tcl_MutexUnlock(&tmMutex);
381 *----------------------------------------------------------------------
383 * ThreadSafeLocalTime --
385 * Wrapper around the 'localtime' library function to make it thread
389 * Returns a pointer to a 'struct tm' in thread-specific data.
392 * Invokes localtime or localtime_r as appropriate.
394 *----------------------------------------------------------------------
398 ThreadSafeLocalTime(timePtr)
399 CONST time_t *timePtr; /* Pointer to the number of seconds
400 * since the local system's epoch
405 * Get a thread-local buffer to hold the returned time.
408 struct tm *tmPtr = (struct tm *)
409 Tcl_GetThreadData(&tmKey, sizeof(struct tm));
410 #ifdef HAVE_LOCALTIME_R
411 localtime_r(timePtr, tmPtr);
413 Tcl_MutexLock(&tmMutex);
414 memcpy((VOID *) tmPtr, (VOID *) localtime(timePtr), sizeof(struct tm));
415 Tcl_MutexUnlock(&tmMutex);