summaryrefslogtreecommitdiffstats
path: root/contrib/tcl/generic/tclClock.c
blob: 3fb4abdd450450325eb0213fc448f77735140295 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* 
 * 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.19 96/03/13 11:28:45
 */

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

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

static int		FormatClock _ANSI_ARGS_((Tcl_Interp *interp,
			    unsigned long clockVal, int useGMT,
			    char *format));
static int		ParseTime _ANSI_ARGS_((Tcl_Interp *interp,
			    char *string, unsigned long *timePtr));

/*
 *-----------------------------------------------------------------------------
 *
 * Tcl_ClockCmd --
 *
 *	This procedure is invoked to process the "clock" Tcl command.
 *	See the user documentation for details on what it does.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	See the user documentation.
 *
 *-----------------------------------------------------------------------------
 */

int
Tcl_ClockCmd (dummy, interp, argc, argv)
    ClientData dummy;			/* Not used. */
    Tcl_Interp *interp;			/* Current interpreter. */
    int argc;				/* Number of arguments. */
    char **argv;			/* Argument strings. */
{
    int c;
    size_t length;
    char **argPtr;
    int useGMT = 0;
    unsigned long clockVal;
    
    if (argc < 2) {
	Tcl_AppendResult(interp, "wrong # args: should be \"",
		argv[0], " option ?arg ...?\"", (char *) NULL);
	return TCL_ERROR;
    }
    c = argv[1][0];
    length = strlen(argv[1]);
    if ((c == 'c') && (strncmp(argv[1], "clicks", length) == 0)) {
	if (argc != 2) {
	    Tcl_AppendResult(interp, "wrong # arguments: must be \"",
		    argv[0], " clicks\"", (char *) NULL);
	    return TCL_ERROR;
	}
	sprintf(interp->result, "%lu", TclGetClicks());
	return TCL_OK;
    } else if ((c == 'f') && (strncmp(argv[1], "format", length) == 0)) {
	char *format = "%a %b %d %X %Z %Y";
	
	if ((argc < 3) || (argc > 7)) {
	    wrongFmtArgs:
	    Tcl_AppendResult(interp, "wrong # args: ", argv [0], 
		    " format clockval ?-format string? ?-gmt boolean?",
		    (char *) NULL);
	    return TCL_ERROR;
	}

	if (ParseTime(interp, argv[2], &clockVal) != TCL_OK) {
	    return TCL_ERROR;
	}

	argPtr = argv+3;
	argc -= 3;
	while ((argc > 1) && (argPtr[0][0] == '-')) {
	    if (strcmp(argPtr[0], "-format") == 0) {
	        format = argPtr[1];
	    } else if (strcmp(argPtr[0], "-gmt") == 0) {
		if (Tcl_GetBoolean(interp, argPtr[1], &useGMT) != TCL_OK) {
		    return TCL_ERROR;
		}
	    } else {
		Tcl_AppendResult(interp, "bad option \"", argPtr[0],
			"\": must be -format or -gmt", (char *) NULL);
		return TCL_ERROR;
	    }
	    argPtr += 2;
	    argc -= 2;
	}
	if (argc != 0) {
	    goto wrongFmtArgs;
	}

	return FormatClock(interp, clockVal, useGMT, format);
    } else if ((c == 's') && (strncmp(argv[1], "scan", length) == 0)) {
	unsigned long baseClock;
	long zone;
	char * baseStr = NULL;

	if ((argc < 3) || (argc > 7)) {
	    wrongScanArgs:
	    Tcl_AppendResult (interp, "wrong # args: ", argv [0], 
		    " scan dateString ?-base clockValue? ?-gmt boolean?",
		    (char *) NULL);
	    return TCL_ERROR;
	}

	argPtr = argv+3;
	argc -= 3;
	while ((argc > 1) && (argPtr[0][0] == '-')) {
	    if (strcmp(argPtr[0], "-base") == 0) {
	        baseStr = argPtr[1];
	    } else if (strcmp(argPtr[0], "-gmt") == 0) {
		if (Tcl_GetBoolean(interp, argPtr[1], &useGMT) != TCL_OK) {
		    return TCL_ERROR;
		}
	    } else {
		Tcl_AppendResult(interp, "bad option \"", argPtr[0],
			"\": must be -base or -gmt", (char *) NULL);
		return TCL_ERROR;
	    }
	    argPtr += 2;
	    argc -= 2;
	}
	if (argc != 0) {
	    goto wrongScanArgs;
	}
	
	if (baseStr != NULL) {
	    if (ParseTime(interp, baseStr, &baseClock) != TCL_OK)
		return TCL_ERROR;
	} else {
	    baseClock = TclGetSeconds();
	}

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

	if (TclGetDate(argv[2], baseClock, zone, &clockVal) < 0) {
	    Tcl_AppendResult(interp, "unable to convert date-time string \"",
		    argv[2], "\"", (char *) NULL);
	    return TCL_ERROR;
	}

	sprintf(interp->result, "%lu", (long) clockVal);
	return TCL_OK;
    } else if ((c == 's') && (strncmp(argv[1], "seconds", length) == 0)) {
	if (argc != 2) {
	    Tcl_AppendResult(interp, "wrong # arguments: must be \"",
		    argv[0], " seconds\"", (char *) NULL);
	    return TCL_ERROR;
	}
	sprintf(interp->result, "%lu", TclGetSeconds());
	return TCL_OK;
    } else {
	Tcl_AppendResult(interp, "unknown option \"", argv[1],
		"\": must be clicks, format, scan, or seconds",
		(char *) NULL);
	return TCL_ERROR;
    }
}

/*
 *-----------------------------------------------------------------------------
 *
 * ParseTime --
 *
 *      Given a string, produce the corresponding time_t value.
 *
 * Results:
 *      The return value is normally TCL_OK;  in this case *timePtr
 *      will be set to the integer value equivalent to string.  If
 *      string is improperly formed then TCL_ERROR is returned and
 *      an error message will be left in interp->result.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static int
ParseTime(interp, string, timePtr)
    Tcl_Interp *interp;
    char *string;
    unsigned long *timePtr;
{
    char *end, *p;
    unsigned long  i;

    /*
     * Since some strtoul functions don't detect negative numbers, check
     * in advance.
     */
    errno = 0;
    for (p = (char *) string; isspace(UCHAR(*p)); p++) {
        /* Empty loop body. */
    }
    if (*p == '+') {
        p++;
    }
    i = strtoul(p, &end, 0);
    if (end == p) {
        goto badTime;
    }
    if (errno == ERANGE) {
	interp->result = "integer value too large to represent";
	Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
		interp->result, (char *) NULL);
	return TCL_ERROR;
    }
    while ((*end != '\0') && isspace(UCHAR(*end))) {
        end++;
    }
    if (*end != '\0') {
        goto badTime;
    }

    *timePtr = (time_t) i;
    if (*timePtr != i) {
        goto badTime;
    }
    return TCL_OK;

  badTime:
    Tcl_AppendResult (interp, "expected unsigned time but got \"", 
                      string, "\"", (char *) NULL);
    return TCL_ERROR;
}

/*
 *-----------------------------------------------------------------------------
 *
 * FormatClock --
 *
 *      Formats a time value based on seconds into a human readable
 *	string.
 *
 * Results:
 *      Standard Tcl result.
 *
 * Side effects:
 *      None.
 *
 *-----------------------------------------------------------------------------
 */

static int
FormatClock(interp, clockVal, useGMT, format)
    Tcl_Interp *interp;			/* Current interpreter. */
    unsigned long clockVal;	       	/* Time in seconds. */
    int useGMT;				/* Boolean */
    char *format;			/* Format string */
{
    struct tm *timeDataPtr;
    Tcl_DString buffer;
    int bufSize;
#ifdef TCL_USE_TIMEZONE_VAR
    int savedTimeZone;
    char *savedTZEnv;
#endif

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

    if (!calledTzset) {
        tzset();
        calledTzset = 1;
    }
#endif

#ifdef TCL_USE_TIMEZONE_VAR
    /*
     * 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).
     */
    if (useGMT) {
        char *varValue;

        varValue = Tcl_GetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
        if (varValue != NULL) {
	    savedTZEnv = strcpy(ckalloc(strlen(varValue) + 1), varValue);
        } else {
            savedTZEnv = NULL;
	}
        Tcl_SetVar2(interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
        savedTimeZone = timezone;
        timezone = 0;
        tzset();
    }
#endif

    if (useGMT) {
        timeDataPtr = gmtime((time_t *) &clockVal);
    } else {
        timeDataPtr = localtime((time_t *) &clockVal);
    }
    
    /*
     * Format the time, increasing the buffer size until strftime succeeds.
     */
    bufSize = TCL_DSTRING_STATIC_SIZE - 1;
    Tcl_DStringInit(&buffer);
    Tcl_DStringSetLength(&buffer, bufSize);

    while (strftime(buffer.string, (unsigned int) bufSize, format,
	    timeDataPtr) == 0) {
        bufSize *= 2;
        Tcl_DStringSetLength(&buffer, bufSize);
    }

#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

    Tcl_DStringResult(interp, &buffer);
    return TCL_OK;
}

OpenPOWER on IntegriCloud