2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: tclClock.c /main/2 1996/08/08 14:43:05 cde-hp $ */
27 * Contains the time and date related commands. This code
28 * is derived from the time and date facilities of TclX,
29 * by Mark Diekhans and Karl Lehenbauer.
31 * Copyright 1991-1995 Karl Lehenbauer and Mark Diekhans.
32 * Copyright (c) 1995 Sun Microsystems, Inc.
34 * See the file "license.terms" for information on usage and redistribution
35 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
37 * SCCS: @(#) tclClock.c 1.19 96/03/13 11:28:45
45 * Function prototypes for local procedures in this file:
48 static int FormatClock _ANSI_ARGS_((Tcl_Interp *interp,
49 unsigned long clockVal, int useGMT,
51 static int ParseTime _ANSI_ARGS_((Tcl_Interp *interp,
52 char *string, unsigned long *timePtr));
55 *-----------------------------------------------------------------------------
59 * This procedure is invoked to process the "clock" Tcl command.
60 * See the user documentation for details on what it does.
63 * A standard Tcl result.
66 * See the user documentation.
68 *-----------------------------------------------------------------------------
72 Tcl_ClockCmd (dummy, interp, argc, argv)
73 ClientData dummy; /* Not used. */
74 Tcl_Interp *interp; /* Current interpreter. */
75 int argc; /* Number of arguments. */
76 char **argv; /* Argument strings. */
82 unsigned long clockVal;
85 Tcl_AppendResult(interp, "wrong # args: should be \"",
86 argv[0], " option ?arg ...?\"", (char *) NULL);
90 length = strlen(argv[1]);
91 if ((c == 'c') && (strncmp(argv[1], "clicks", length) == 0)) {
93 Tcl_AppendResult(interp, "wrong # arguments: must be \"",
94 argv[0], " clicks\"", (char *) NULL);
97 sprintf(interp->result, "%lu", TclGetClicks());
99 } else if ((c == 'f') && (strncmp(argv[1], "format", length) == 0)) {
100 char *format = "%a %b %d %X %Z %Y";
102 if ((argc < 3) || (argc > 7)) {
104 Tcl_AppendResult(interp, "wrong # args: ", argv [0],
105 " format clockval ?-format string? ?-gmt boolean?",
110 if (ParseTime(interp, argv[2], &clockVal) != TCL_OK) {
116 while ((argc > 1) && (argPtr[0][0] == '-')) {
117 if (strcmp(argPtr[0], "-format") == 0) {
119 } else if (strcmp(argPtr[0], "-gmt") == 0) {
120 if (Tcl_GetBoolean(interp, argPtr[1], &useGMT) != TCL_OK) {
124 Tcl_AppendResult(interp, "bad option \"", argPtr[0],
125 "\": must be -format or -gmt", (char *) NULL);
135 return FormatClock(interp, clockVal, useGMT, format);
136 } else if ((c == 's') && (strncmp(argv[1], "scan", length) == 0)) {
137 unsigned long baseClock;
139 char * baseStr = NULL;
141 if ((argc < 3) || (argc > 7)) {
143 Tcl_AppendResult (interp, "wrong # args: ", argv [0],
144 " scan dateString ?-base clockValue? ?-gmt boolean?",
151 while ((argc > 1) && (argPtr[0][0] == '-')) {
152 if (strcmp(argPtr[0], "-base") == 0) {
154 } else if (strcmp(argPtr[0], "-gmt") == 0) {
155 if (Tcl_GetBoolean(interp, argPtr[1], &useGMT) != TCL_OK) {
159 Tcl_AppendResult(interp, "bad option \"", argPtr[0],
160 "\": must be -base or -gmt", (char *) NULL);
170 if (baseStr != NULL) {
171 if (ParseTime(interp, baseStr, &baseClock) != TCL_OK)
174 baseClock = TclGetSeconds();
178 zone = -50000; /* Force GMT */
180 zone = TclGetTimeZone(baseClock);
183 if (TclGetDate(argv[2], baseClock, zone, &clockVal) < 0) {
184 Tcl_AppendResult(interp, "unable to convert date-time string \"",
185 argv[2], "\"", (char *) NULL);
189 sprintf(interp->result, "%lu", (long) clockVal);
191 } else if ((c == 's') && (strncmp(argv[1], "seconds", length) == 0)) {
193 Tcl_AppendResult(interp, "wrong # arguments: must be \"",
194 argv[0], " seconds\"", (char *) NULL);
197 sprintf(interp->result, "%lu", TclGetSeconds());
200 Tcl_AppendResult(interp, "unknown option \"", argv[1],
201 "\": must be clicks, format, scan, or seconds",
208 *-----------------------------------------------------------------------------
212 * Given a string, produce the corresponding time_t value.
215 * The return value is normally TCL_OK; in this case *timePtr
216 * will be set to the integer value equivalent to string. If
217 * string is improperly formed then TCL_ERROR is returned and
218 * an error message will be left in interp->result.
223 *-----------------------------------------------------------------------------
227 ParseTime(interp, string, timePtr)
230 unsigned long *timePtr;
236 * Since some strtoul functions don't detect negative numbers, check
240 for (p = (char *) string; isspace(UCHAR(*p)); p++) {
241 /* Empty loop body. */
246 i = strtoul(p, &end, 0);
250 if (errno == ERANGE) {
251 interp->result = "integer value too large to represent";
252 Tcl_SetErrorCode(interp, "ARITH", "IOVERFLOW",
253 interp->result, (char *) NULL);
256 while ((*end != '\0') && isspace(UCHAR(*end))) {
263 *timePtr = (time_t) i;
270 Tcl_AppendResult (interp, "expected unsigned time but got \"",
271 string, "\"", (char *) NULL);
276 *-----------------------------------------------------------------------------
280 * Formats a time value based on seconds into a human readable
284 * Standard Tcl result.
289 *-----------------------------------------------------------------------------
293 FormatClock(interp, clockVal, useGMT, format)
294 Tcl_Interp *interp; /* Current interpreter. */
295 unsigned long clockVal; /* Time in seconds. */
296 int useGMT; /* Boolean */
297 char *format; /* Format string */
299 struct tm *timeDataPtr;
302 #ifdef TCL_USE_TIMEZONE_VAR
309 * Some systems forgot to call tzset in localtime, make sure its done.
311 static int calledTzset = 0;
319 #ifdef TCL_USE_TIMEZONE_VAR
321 * This is a horrible kludge for systems not having the timezone in
322 * struct tm. No matter what was specified, they use the global time
323 * zone. (Thanks Solaris).
328 varValue = Tcl_GetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
329 if (varValue != NULL) {
330 savedTZEnv = strcpy(ckalloc(strlen(varValue) + 1), varValue);
334 Tcl_SetVar2(interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
335 savedTimeZone = timezone;
342 timeDataPtr = gmtime((time_t *) &clockVal);
344 timeDataPtr = localtime((time_t *) &clockVal);
348 * Format the time, increasing the buffer size until strftime succeeds.
350 bufSize = TCL_DSTRING_STATIC_SIZE - 1;
351 Tcl_DStringInit(&buffer);
352 Tcl_DStringSetLength(&buffer, bufSize);
354 while (strftime(buffer.string, (unsigned int) bufSize, format,
357 Tcl_DStringSetLength(&buffer, bufSize);
360 #ifdef TCL_USE_TIMEZONE_VAR
362 if (savedTZEnv != NULL) {
363 Tcl_SetVar2(interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
366 Tcl_UnsetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
368 timezone = savedTimeZone;
373 Tcl_DStringResult(interp, &buffer);