Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtksh/tcl/tclClock.c
1810 views
1
/*
2
* tclClock.c --
3
*
4
* Contains the time and date related commands. This code
5
* is derived from the time and date facilities of TclX,
6
* by Mark Diekhans and Karl Lehenbauer.
7
*
8
* Copyright 1991-1995 Karl Lehenbauer and Mark Diekhans.
9
* Copyright (c) 1995 Sun Microsystems, Inc.
10
*
11
* See the file "license.terms" for information on usage and redistribution
12
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
*
14
* SCCS: @(#) tclClock.c 1.20 96/07/23 16:14:45
15
*/
16
17
#include "tcl.h"
18
#include "tclInt.h"
19
#include "tclPort.h"
20
21
/*
22
* Function prototypes for local procedures in this file:
23
*/
24
25
static int FormatClock _ANSI_ARGS_((Tcl_Interp *interp,
26
unsigned long clockVal, int useGMT,
27
char *format));
28
static int ParseTime _ANSI_ARGS_((Tcl_Interp *interp,
29
char *string, unsigned long *timePtr));
30
31
/*
32
*-----------------------------------------------------------------------------
33
*
34
* Tcl_ClockCmd --
35
*
36
* This procedure is invoked to process the "clock" Tcl command.
37
* See the user documentation for details on what it does.
38
*
39
* Results:
40
* A standard Tcl result.
41
*
42
* Side effects:
43
* See the user documentation.
44
*
45
*-----------------------------------------------------------------------------
46
*/
47
48
int
49
Tcl_ClockCmd (dummy, interp, argc, argv)
50
ClientData dummy; /* Not used. */
51
Tcl_Interp *interp; /* Current interpreter. */
52
int argc; /* Number of arguments. */
53
char **argv; /* Argument strings. */
54
{
55
int c;
56
size_t length;
57
char **argPtr;
58
int useGMT = 0;
59
unsigned long clockVal;
60
61
if (argc < 2) {
62
Tcl_AppendResult(interp, "wrong # args: should be \"",
63
argv[0], " option ?arg ...?\"", (char *) NULL);
64
return TCL_ERROR;
65
}
66
c = argv[1][0];
67
length = strlen(argv[1]);
68
if ((c == 'c') && (strncmp(argv[1], "clicks", length) == 0)) {
69
if (argc != 2) {
70
Tcl_AppendResult(interp, "wrong # arguments: must be \"",
71
argv[0], " clicks\"", (char *) NULL);
72
return TCL_ERROR;
73
}
74
sprintf(interp->result, "%lu", TclpGetClicks());
75
return TCL_OK;
76
} else if ((c == 'f') && (strncmp(argv[1], "format", length) == 0)) {
77
char *format = "%a %b %d %X %Z %Y";
78
79
if ((argc < 3) || (argc > 7)) {
80
wrongFmtArgs:
81
Tcl_AppendResult(interp, "wrong # args: ", argv [0],
82
" format clockval ?-format string? ?-gmt boolean?",
83
(char *) NULL);
84
return TCL_ERROR;
85
}
86
87
if (ParseTime(interp, argv[2], &clockVal) != TCL_OK) {
88
return TCL_ERROR;
89
}
90
91
argPtr = argv+3;
92
argc -= 3;
93
while ((argc > 1) && (argPtr[0][0] == '-')) {
94
if (strcmp(argPtr[0], "-format") == 0) {
95
format = argPtr[1];
96
} else if (strcmp(argPtr[0], "-gmt") == 0) {
97
if (Tcl_GetBoolean(interp, argPtr[1], &useGMT) != TCL_OK) {
98
return TCL_ERROR;
99
}
100
} else {
101
Tcl_AppendResult(interp, "bad option \"", argPtr[0],
102
"\": must be -format or -gmt", (char *) NULL);
103
return TCL_ERROR;
104
}
105
argPtr += 2;
106
argc -= 2;
107
}
108
if (argc != 0) {
109
goto wrongFmtArgs;
110
}
111
112
return FormatClock(interp, clockVal, useGMT, format);
113
} else if ((c == 's') && (strncmp(argv[1], "scan", length) == 0)) {
114
unsigned long baseClock;
115
long zone;
116
char * baseStr = NULL;
117
118
if ((argc < 3) || (argc > 7)) {
119
wrongScanArgs:
120
Tcl_AppendResult (interp, "wrong # args: ", argv [0],
121
" scan dateString ?-base clockValue? ?-gmt boolean?",
122
(char *) NULL);
123
return TCL_ERROR;
124
}
125
126
argPtr = argv+3;
127
argc -= 3;
128
while ((argc > 1) && (argPtr[0][0] == '-')) {
129
if (strcmp(argPtr[0], "-base") == 0) {
130
baseStr = argPtr[1];
131
} else if (strcmp(argPtr[0], "-gmt") == 0) {
132
if (Tcl_GetBoolean(interp, argPtr[1], &useGMT) != TCL_OK) {
133
return TCL_ERROR;
134
}
135
} else {
136
Tcl_AppendResult(interp, "bad option \"", argPtr[0],
137
"\": must be -base or -gmt", (char *) NULL);
138
return TCL_ERROR;
139
}
140
argPtr += 2;
141
argc -= 2;
142
}
143
if (argc != 0) {
144
goto wrongScanArgs;
145
}
146
147
if (baseStr != NULL) {
148
if (ParseTime(interp, baseStr, &baseClock) != TCL_OK)
149
return TCL_ERROR;
150
} else {
151
baseClock = TclpGetSeconds();
152
}
153
154
if (useGMT) {
155
zone = -50000; /* Force GMT */
156
} else {
157
zone = TclpGetTimeZone(baseClock);
158
}
159
160
if (TclGetDate(argv[2], baseClock, zone, &clockVal) < 0) {
161
Tcl_AppendResult(interp, "unable to convert date-time string \"",
162
argv[2], "\"", (char *) NULL);
163
return TCL_ERROR;
164
}
165
166
sprintf(interp->result, "%lu", (long) clockVal);
167
return TCL_OK;
168
} else if ((c == 's') && (strncmp(argv[1], "seconds", length) == 0)) {
169
if (argc != 2) {
170
Tcl_AppendResult(interp, "wrong # arguments: must be \"",
171
argv[0], " seconds\"", (char *) NULL);
172
return TCL_ERROR;
173
}
174
sprintf(interp->result, "%lu", TclpGetSeconds());
175
return TCL_OK;
176
} else {
177
Tcl_AppendResult(interp, "unknown option \"", argv[1],
178
"\": must be clicks, format, scan, or seconds",
179
(char *) NULL);
180
return TCL_ERROR;
181
}
182
}
183
184
/*
185
*-----------------------------------------------------------------------------
186
*
187
* ParseTime --
188
*
189
* Given a string, produce the corresponding time_t value.
190
*
191
* Results:
192
* The return value is normally TCL_OK; in this case *timePtr
193
* will be set to the integer value equivalent to string. If
194
* string is improperly formed then TCL_ERROR is returned and
195
* an error message will be left in interp->result.
196
*
197
* Side effects:
198
* None.
199
*
200
*-----------------------------------------------------------------------------
201
*/
202
203
static int
204
ParseTime(interp, string, timePtr)
205
Tcl_Interp *interp;
206
char *string;
207
unsigned long *timePtr;
208
{
209
char *end, *p;
210
unsigned long i;
211
212
/*
213
* Since some strtoul functions don't detect negative numbers, check
214
* in advance.
215
*/
216
errno = 0;
217
for (p = (char *) string; isspace(UCHAR(*p)); p++) {
218
/* Empty loop body. */
219
}
220
if (*p == '+') {
221
p++;
222
}
223
i = strtoul(p, &end, 0);
224
if (end == p) {
225
goto badTime;
226
}
227
if (errno == ERANGE) {
228
interp->result = "integer value too large to represent";
229
Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
230
interp->result, (char *) NULL);
231
return TCL_ERROR;
232
}
233
while ((*end != '\0') && isspace(UCHAR(*end))) {
234
end++;
235
}
236
if (*end != '\0') {
237
goto badTime;
238
}
239
240
*timePtr = (time_t) i;
241
if (*timePtr != i) {
242
goto badTime;
243
}
244
return TCL_OK;
245
246
badTime:
247
Tcl_AppendResult (interp, "expected unsigned time but got \"",
248
string, "\"", (char *) NULL);
249
return TCL_ERROR;
250
}
251
252
/*
253
*-----------------------------------------------------------------------------
254
*
255
* FormatClock --
256
*
257
* Formats a time value based on seconds into a human readable
258
* string.
259
*
260
* Results:
261
* Standard Tcl result.
262
*
263
* Side effects:
264
* None.
265
*
266
*-----------------------------------------------------------------------------
267
*/
268
269
static int
270
FormatClock(interp, clockVal, useGMT, format)
271
Tcl_Interp *interp; /* Current interpreter. */
272
unsigned long clockVal; /* Time in seconds. */
273
int useGMT; /* Boolean */
274
char *format; /* Format string */
275
{
276
struct tm *timeDataPtr;
277
Tcl_DString buffer;
278
int bufSize;
279
char *p;
280
#ifdef TCL_USE_TIMEZONE_VAR
281
int savedTimeZone;
282
char *savedTZEnv;
283
#endif
284
285
#ifdef HAVE_TZSET
286
/*
287
* Some systems forgot to call tzset in localtime, make sure its done.
288
*/
289
static int calledTzset = 0;
290
291
if (!calledTzset) {
292
tzset();
293
calledTzset = 1;
294
}
295
#endif
296
297
#ifdef TCL_USE_TIMEZONE_VAR
298
/*
299
* This is a horrible kludge for systems not having the timezone in
300
* struct tm. No matter what was specified, they use the global time
301
* zone. (Thanks Solaris).
302
*/
303
if (useGMT) {
304
char *varValue;
305
306
varValue = Tcl_GetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
307
if (varValue != NULL) {
308
savedTZEnv = strcpy(ckalloc(strlen(varValue) + 1), varValue);
309
} else {
310
savedTZEnv = NULL;
311
}
312
Tcl_SetVar2(interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
313
savedTimeZone = timezone;
314
timezone = 0;
315
tzset();
316
}
317
#endif
318
319
timeDataPtr = TclpGetDate((time_t *) &clockVal, useGMT);
320
321
/*
322
* Make a guess at the upper limit on the substituted string size
323
* based on the number of percents in the string.
324
*/
325
326
for (bufSize = 0, p = format; *p != '\0'; p++) {
327
if (*p == '%') {
328
bufSize += 40;
329
} else {
330
bufSize++;
331
}
332
}
333
Tcl_DStringInit(&buffer);
334
Tcl_DStringSetLength(&buffer, bufSize);
335
336
if (TclStrftime(buffer.string, (unsigned int) bufSize, format,
337
timeDataPtr) == 0) {
338
Tcl_DStringFree(&buffer);
339
Tcl_AppendResult(interp, "bad format string", (char *)NULL);
340
return TCL_ERROR;
341
}
342
343
#ifdef TCL_USE_TIMEZONE_VAR
344
if (useGMT) {
345
if (savedTZEnv != NULL) {
346
Tcl_SetVar2(interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
347
ckfree(savedTZEnv);
348
} else {
349
Tcl_UnsetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
350
}
351
timezone = savedTimeZone;
352
tzset();
353
}
354
#endif
355
356
Tcl_DStringResult(interp, &buffer);
357
return TCL_OK;
358
}
359
360
361