Debian Codegraph @

pTk/mTk/tclUnix/tclUnixTime.c

raw symbols SCIP
1 /*
2 * tclUnixTime.c --
3 *
4 * Contains Unix specific versions of Tcl functions that
5 * obtain time values from the operating system.
6 *
7 * Copyright (c) 1995 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tclUnixTime.c,v 1.15 2002/07/19 12:31:10 dkf Exp $
13 */
14 #include "tkPort.h"
15 #include "Lang.h"
16 #ifdef TCL_EVENT_IMPLEMENT
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 # include <sys/time.h>
24 # else
25 # include <time.h>
26 # endif
27 #endif
28
29 #if 0
30 #include "tclInt.h"
31 #include "tclPort.h"
32 #include <locale.h>
33 #endif
34 #ifdef TIMEOFDAY_NO_TZ
35 #define Tk_timeofday(x) gettimeofday(x)
36 extern int gettimeofday _ANSI_ARGS_((struct timeval *tp));
37 #else
38 #ifdef TIMEOFDAY_TZ
39 #define Tk_timeofday(x) gettimeofday(x, (struct timezone *) 0)
40 extern int gettimeofday _ANSI_ARGS_((struct timeval *tp ,struct timezone *tzp));
41 #else
42 #define Tk_timeofday(x) gettimeofday(x, (void *) 0)
43 #ifdef TIMEOFDAY_DOTS
44 extern int gettimeofday _ANSI_ARGS_((struct timeval *tp, ...));
45 #else
46 extern int gettimeofday _ANSI_ARGS_((struct timeval *tp, void *));
47 #endif
48 #endif
49 #endif
50
51 #define TM_YEAR_BASE 1900
52 #define IsLeapYear(x) ((x % 4 == 0) && (x % 100 != 0 || x % 400 == 0))
53
54 /*
55 * TclpGetDate is coded to return a pointer to a 'struct tm'. For
56 * thread safety, this structure must be in thread-specific data.
57 * The 'tmKey' variable is the key to this buffer.
58 */
59
60 static Tcl_ThreadDataKey tmKey;
61
62 /*
63 * If we fall back on the thread-unsafe versions of gmtime and localtime,
64 * use this mutex to try to protect them.
65 */
66
67 #if !defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R)
68 TCL_DECLARE_MUTEX(tmMutex)
69 #endif
70
71 /*
72 * Forward declarations for procedures defined later in this file.
73 */
74
75 static struct tm *ThreadSafeGMTime _ANSI_ARGS_(( CONST time_t* ));
76 static struct tm *ThreadSafeLocalTime _ANSI_ARGS_(( CONST time_t* ));
77
78 /*
79 *-----------------------------------------------------------------------------
80 *
81 * TclpGetSeconds --
82 *
83 * This procedure returns the number of seconds from the epoch. On
84 * most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
85 *
86 * Results:
87 * Number of seconds from the epoch.
88 *
89 * Side effects:
90 * None.
91 *
92 *-----------------------------------------------------------------------------
93 */
94
95 unsigned long
96 TclpGetSeconds()
97 {
98 return time((time_t *) NULL);
99 }
100
101 /*
102 *-----------------------------------------------------------------------------
103 *
104 * TclpGetClicks --
105 *
106 * This procedure returns a value that represents the highest resolution
107 * clock available on the system. There are no garantees on what the
108 * resolution will be. In Tcl we will call this value a "click". The
109 * start time is also system dependant.
110 *
111 * Results:
112 * Number of clicks from some start time.
113 *
114 * Side effects:
115 * None.
116 *
117 *-----------------------------------------------------------------------------
118 */
119
120 unsigned long
121 TclpGetClicks()
122 {
123 unsigned long now;
124 #ifdef NO_GETTOD
125 struct tms dummy;
126 now = (unsigned long) times(&dummy);
127 #else
128 struct timeval date;
129 Tk_timeofday(&date);
130 now = date.tv_sec*1000000 + date.tv_usec;
131 #endif
132
133 return now;
134 }
135
136 /*
137 *----------------------------------------------------------------------
138 *
139 * TclpGetTimeZone --
140 *
141 * Determines the current timezone. The method varies wildly
142 * between different platform implementations, so its hidden in
143 * this function.
144 *
145 * Results:
146 * The return value is the local time zone, measured in
147 * minutes away from GMT (-ve for east, +ve for west).
148 *
149 * Side effects:
150 * None.
151 *
152 *----------------------------------------------------------------------
153 */
154 #if 0
155 int
156 TclpGetTimeZone (currentTime)
157 unsigned long currentTime;
158 {
159 /*
160 * Determine how a timezone is obtained from "struct tm". If there is no
161 * time zone in this struct (very lame) then use the timezone variable.
162 * This is done in a way to make the timezone variable the method of last
163 * resort, as some systems have it in addition to a field in "struct tm".
164 * The gettimeofday system call can also be used to determine the time
165 * zone.
166 */
167
168 #if defined(HAVE_TM_TZADJ)
169 # define TCL_GOT_TIMEZONE
170 time_t curTime = (time_t) currentTime;
171 struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime);
172 int timeZone;
173
174 timeZone = timeDataPtr->tm_tzadj / 60;
175 if (timeDataPtr->tm_isdst) {
176 timeZone += 60;
177 }
178
179 return timeZone;
180 #endif
181
182 #if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
183 # define TCL_GOT_TIMEZONE
184 time_t curTime = (time_t) currentTime;
185 struct tm *timeDataPtr = ThreadSafeLocalTime(&curTime);
186 int timeZone;
187
188 timeZone = -(timeDataPtr->tm_gmtoff / 60);
189 if (timeDataPtr->tm_isdst) {
190 timeZone += 60;
191 }
192
193 return timeZone;
194 #endif
195
196 #if defined(USE_DELTA_FOR_TZ)
197 #define TCL_GOT_TIMEZONE 1
198 /*
199 * This hack replaces using global var timezone or gettimeofday
200 * in situations where they are buggy such as on AIX when libbsd.a
201 * is linked in.
202 */
203
204 int timeZone;
205 time_t tt;
206 struct tm *stm;
207 tt = 849268800L; /* 1996-11-29 12:00:00 GMT */
208 stm = ThreadSafeLocalTime(&tt); /* eg 1996-11-29 6:00:00 CST6CDT */
209 /* The calculation below assumes a max of +12 or -12 hours from GMT */
210 timeZone = (12 - stm->tm_hour)*60 + (0 - stm->tm_min);
211 return timeZone; /* eg +360 for CST6CDT */
212 #endif
213
214 /*
215 * Must prefer timezone variable over gettimeofday, as gettimeofday does
216 * not return timezone information on many systems that have moved this
217 * information outside of the kernel.
218 */
219
220 #if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
221 # define TCL_GOT_TIMEZONE
222 static int setTZ = 0;
223 #ifdef TCL_THREADS
224 static Tcl_Mutex tzMutex;
225 #endif
226 int timeZone;
227
228 Tcl_MutexLock(&tzMutex);
229 if (!setTZ) {
230 tzset();
231 setTZ = 1;
232 }
233 Tcl_MutexUnlock(&tzMutex);
234
235 /*
236 * Note: this is not a typo in "timezone" below! See tzset
237 * documentation for details.
238 */
239
240 timeZone = timezone / 60;
241
242 return timeZone;
243 #endif
244
245 #if defined(HAVE_GETTIMEOFDAY) && !defined (TCL_GOT_TIMEZONE) && !defined (TIMEOFDAY_NO_TZ)
246 # define TCL_GOT_TIMEZONE
247 struct timeval tv;
248 struct timezone tz;
249 int timeZone;
250
251 gettimeofday(&tv, &tz);
252 timeZone = tz.tz_minuteswest;
253 if (tz.tz_dsttime) {
254 timeZone += 60;
255 }
256
257 return timeZone;
258 #endif
259
260 #ifndef TCL_GOT_TIMEZONE
261 /*
262 * Cause compile error, we don't know how to get timezone.
263 */
264 error: autoconf did not figure out how to determine the timezone.
265 #endif
266
267 }
268
269 #endif
270 /*
271 *----------------------------------------------------------------------
272 *
273 * Tcl_GetTime --
274 *
275 * Gets the current system time in seconds and microseconds
276 * since the beginning of the epoch: 00:00 UCT, January 1, 1970.
277 *
278 * Results:
279 * Returns the current time in timePtr.
280 *
281 * Side effects:
282 * None.
283 *
284 *----------------------------------------------------------------------
285 */
286
287 void
288 Tcl_GetTime(timePtr)
289 Tcl_Time *timePtr; /* Location to store time information. */
290 {
291 #ifdef NO_GETTOD
292 /* Do something with times() and epoch adjustment ? */
293 timePtr->sec = time((time_t *) NULL)
294 timePtr->usec = 0;
295 #else
296 struct timeval tv;
297 Tk_timeofday(&tv);
298 timePtr->sec = tv.tv_sec;
299 timePtr->usec = tv.tv_usec;
300 #endif
301 }
302
303 /*
304 *----------------------------------------------------------------------
305 *
306 * TclpGetDate --
307 *
308 * This function converts between seconds and struct tm. If
309 * useGMT is true, then the returned date will be in Greenwich
310 * Mean Time (GMT). Otherwise, it will be in the local time zone.
311 *
312 * Results:
313 * Returns a static tm structure.
314 *
315 * Side effects:
316 * None.
317 *
318 *----------------------------------------------------------------------
319 */
320
321 struct tm *
322 TclpGetDate(time, useGMT)
323 TclpTime_t time;
324 int useGMT;
325 {
326 CONST time_t *tp = (CONST time_t *)time;
327
328 if (useGMT) {
329 return ThreadSafeGMTime(tp);
330 } else {
331 return ThreadSafeLocalTime(tp);
332 }
333 }
334
335 /*
336 *----------------------------------------------------------------------
337 *
338 * TclpStrftime --
339 *
340 * On Unix, we can safely call the native strftime implementation,
341 * and also ignore the useGMT parameter.
342 *
343 * Results:
344 * The normal strftime result.
345 *
346 * Side effects:
347 * None.
348 *
349 *----------------------------------------------------------------------
350 */
351
352 #ifndef _LANG
353 size_t
354 TclpStrftime(s, maxsize, format, t, useGMT)
355 char *s;
356 size_t maxsize;
357 CONST char *format;
358 CONST struct tm *t;
359 int useGMT;
360 {
361 if (format[0] == '%' && format[1] == 'Q') {
362 /* Format as a stardate */
363 sprintf(s, "Stardate %2d%03d.%01d",
364 (((t->tm_year + TM_YEAR_BASE) + 377) - 2323),
365 (((t->tm_yday + 1) * 1000) /
366 (365 + IsLeapYear((t->tm_year + TM_YEAR_BASE)))),
367 (((t->tm_hour * 60) + t->tm_min)/144));
368 return(strlen(s));
369 }
370 setlocale(LC_TIME, "");
371 return strftime(s, maxsize, format, t);
372 }
373 #endif
374
375 /*
376 *----------------------------------------------------------------------
377 *
378 * ThreadSafeGMTime --
379 *
380 * Wrapper around the 'gmtime' library function to make it thread
381 * safe.
382 *
383 * Results:
384 * Returns a pointer to a 'struct tm' in thread-specific data.
385 *
386 * Side effects:
387 * Invokes gmtime or gmtime_r as appropriate.
388 *
389 *----------------------------------------------------------------------
390 */
391
392 static struct tm *
393 ThreadSafeGMTime(timePtr)
394 CONST time_t *timePtr; /* Pointer to the number of seconds
395 * since the local system's epoch
396 */
397
398 {
399 /*
400 * Get a thread-local buffer to hold the returned time.
401 */
402
403 struct tm *tmPtr = (struct tm *)
404 Tcl_GetThreadData(&tmKey, sizeof(struct tm));
405 #ifdef HAVE_GMTIME_R
406 gmtime_r(timePtr, tmPtr);
407 #else
408 Tcl_MutexLock(&tmMutex);
409 memcpy((VOID *) tmPtr, (VOID *) gmtime(timePtr), sizeof(struct tm));
410 Tcl_MutexUnlock(&tmMutex);
411 #endif
412 return tmPtr;
413 }
414
415 /*
416 *----------------------------------------------------------------------
417 *
418 * ThreadSafeLocalTime --
419 *
420 * Wrapper around the 'localtime' library function to make it thread
421 * safe.
422 *
423 * Results:
424 * Returns a pointer to a 'struct tm' in thread-specific data.
425 *
426 * Side effects:
427 * Invokes localtime or localtime_r as appropriate.
428 *
429 *----------------------------------------------------------------------
430 */
431
432 static struct tm *
433 ThreadSafeLocalTime(timePtr)
434 CONST time_t *timePtr; /* Pointer to the number of seconds
435 * since the local system's epoch
436 */
437
438 {
439 /*
440 * Get a thread-local buffer to hold the returned time.
441 */
442
443 struct tm *tmPtr = (struct tm *)
444 Tcl_GetThreadData(&tmKey, sizeof(struct tm));
445 #ifdef HAVE_LOCALTIME_R
446 localtime_r(timePtr, tmPtr);
447 #else
448 Tcl_MutexLock(&tmMutex);
449 memcpy((VOID *) tmPtr, (VOID *) localtime(timePtr), sizeof(struct tm));
450 Tcl_MutexUnlock(&tmMutex);
451 #endif
452 return tmPtr;
453 }
454
455 #endif
456
457
458
459