Diff
Not logged in

Differences From Artifact [01a29fcce4]:

To Artifact [e2422c180e]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20





21
22
23
24
25
26
27
1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32













-
+






+
+
+
+
+







/* 
 * tclClock.c --
 *
 *	Contains the time and date related commands.  This code
 *	is derived from the time and date facilities of TclX,
 *	by Mark Diekhans and Karl Lehenbauer.
 *
 * Copyright 1991-1995 Karl Lehenbauer and Mark Diekhans.
 * Copyright (c) 1995 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * SCCS: @(#) tclClock.c 1.37 97/07/29 10:29:58
 * SCCS: @(#) tclClock.c 1.41 98/02/17 17:18:15
 */

#include "tcl.h"
#include "tclInt.h"
#include "tclPort.h"

/*
 * The date parsing stuff uses lexx and has tons o statics.
 */
static Tcl_Mutex clockMutex;

/*
 * Function prototypes for local procedures in this file:
 */

static int		FormatClock _ANSI_ARGS_((Tcl_Interp *interp,
			    unsigned long clockVal, int useGMT,
			    char *format));
168
169
170
171
172
173
174

175
176

177
178
179
180
181

182
183
184
185
186
187
188
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196







+


+





+







	    if (useGMT) {
		zone = -50000; /* Force GMT */
	    } else {
		zone = TclpGetTimeZone((unsigned long) baseClock);
	    }

	    scanStr = Tcl_GetStringFromObj(objv[2], &dummy);
	    Tcl_MutexLock(&clockMutex);
	    if (TclGetDate(scanStr, (unsigned long) baseClock, zone,
		    (unsigned long *) &clockVal) < 0) {
		Tcl_MutexUnlock(&clockMutex);
		Tcl_AppendStringsToObj(resultPtr,
			"unable to convert date-time string \"",
			scanStr, "\"", (char *) NULL);
		return TCL_ERROR;
	    }
	    Tcl_MutexUnlock(&clockMutex);

	    Tcl_SetLongObj(resultPtr, (long) clockVal);
	    return TCL_OK;
	case 3:			/* seconds */
	    if (objc != 2) {
		Tcl_WrongNumArgs(interp, 2, objv, NULL);
		return TCL_ERROR;
218
219
220
221
222
223
224


225
226
227



228
229
230
231
232
233
234
235
236
237

238
239
240
241

242
243
244

245
246
247
248



249

250
251
252
253
254
255
256
226
227
228
229
230
231
232
233
234



235
236
237
238

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

255
256



257
258
259
260
261
262
263
264
265
266
267
268







+
+
-
-
-
+
+
+

-








+




+


-
+

-
-
-
+
+
+

+







    int useGMT;				/* Boolean */
    char *format;			/* Format string */
{
    struct tm *timeDataPtr;
    Tcl_DString buffer;
    int bufSize;
    char *p;
    Tcl_Obj *resultPtr;
    int result;
#ifdef TCL_USE_TIMEZONE_VAR
    int savedTimeZone;
    char *savedTZEnv;
#ifndef HAVE_TM_ZONE
    int savedTimeZone = 0;	/* lint. */
    char *savedTZEnv = NULL;	/* lint. */
#endif
    Tcl_Obj *resultPtr;

    resultPtr = Tcl_GetObjResult(interp);
#ifdef HAVE_TZSET
    /*
     * Some systems forgot to call tzset in localtime, make sure its done.
     */
    static int  calledTzset = 0;

    Tcl_MutexLock(&clockMutex);
    if (!calledTzset) {
        tzset();
        calledTzset = 1;
    }
    Tcl_MutexUnlock(&clockMutex);
#endif

#ifdef TCL_USE_TIMEZONE_VAR
#ifndef HAVE_TM_ZONE
    /*
     * This is a horrible kludge for systems not having the timezone in
     * struct tm.  No matter what was specified, they use the global time
     * zone.  (Thanks Solaris).
     * This is a kludge for systems not having the timezone string in
     * struct tm.  No matter what was specified, they use the local
     * timezone string.
     */

    if (useGMT) {
        char *varValue;

        varValue = Tcl_GetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
        if (varValue != NULL) {
	    savedTZEnv = strcpy(ckalloc(strlen(varValue) + 1), varValue);
        } else {
276
277
278
279
280
281
282

283
284


285
286

287
288
289


290
291
292
293
294
295
296
297
298
299
300
301





302
303
304
305
306
307
288
289
290
291
292
293
294
295


296
297


298



299
300

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322







+
-
-
+
+
-
-
+
-
-
-
+
+
-











+
+
+
+
+






	} else {
	    bufSize++;
	}
    }
    Tcl_DStringInit(&buffer);
    Tcl_DStringSetLength(&buffer, bufSize);

    Tcl_MutexLock(&clockMutex);
    if ((TclStrftime(buffer.string, (unsigned int) bufSize, format,
	    timeDataPtr) == 0) && (*format != '\0')) {
    result = TclStrftime(buffer.string, (unsigned int) bufSize, format,
	    timeDataPtr);
	Tcl_AppendStringsToObj(resultPtr, "bad format string \"",
		format, "\"", (char *) NULL);
    Tcl_MutexUnlock(&clockMutex);
	return TCL_ERROR;
    }


#ifndef HAVE_TM_ZONE
#ifdef TCL_USE_TIMEZONE_VAR
    if (useGMT) {
        if (savedTZEnv != NULL) {
            Tcl_SetVar2(interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
            ckfree(savedTZEnv);
        } else {
            Tcl_UnsetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
        }
        timezone = savedTimeZone;
        tzset();
    }
#endif
    if ((result == 0) && (*format != '\0')) {
	Tcl_AppendStringsToObj(resultPtr, "bad format string \"", format, 
		"\"", (char *) NULL);
	return TCL_ERROR;
    }

    Tcl_SetStringObj(resultPtr, buffer.string, -1);
    Tcl_DStringFree(&buffer);
    return TCL_OK;
}