os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclClock.c
Update contrib.
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.
8 * Copyright 1991-1995 Karl Lehenbauer and Mark Diekhans.
9 * Copyright (c) 1995 Sun Microsystems, Inc.
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * RCS: @(#) $Id: tclClock.c,v 1.20.2.3 2007/04/21 22:42:49 kennykb Exp $
22 * The date parsing stuff uses lexx and has tons o statics.
25 TCL_DECLARE_MUTEX(clockMutex)
28 * Function prototypes for local procedures in this file:
31 static int FormatClock _ANSI_ARGS_((Tcl_Interp *interp,
32 Tcl_WideInt clockVal, int useGMT,
36 *-------------------------------------------------------------------------
40 * This procedure is invoked to process the "clock" Tcl command.
41 * See the user documentation for details on what it does.
44 * A standard Tcl result.
47 * See the user documentation.
49 *-------------------------------------------------------------------------
53 Tcl_ClockObjCmd (client, interp, objc, objv)
54 ClientData client; /* Not used. */
55 Tcl_Interp *interp; /* Current interpreter. */
56 int objc; /* Number of arguments. */
57 Tcl_Obj *CONST objv[]; /* Argument values. */
61 Tcl_Obj *CONST *objPtr;
63 char *format = "%a %b %d %X %Z %Y";
65 Tcl_WideInt baseClock, clockVal;
67 Tcl_Obj *baseObjPtr = NULL;
71 static CONST char *switches[] =
72 {"clicks", "format", "scan", "seconds", (char *) NULL};
73 enum command { COMMAND_CLICKS, COMMAND_FORMAT, COMMAND_SCAN,
76 static CONST char *formatSwitches[] = {"-format", "-gmt", (char *) NULL};
77 static CONST char *scanSwitches[] = {"-base", "-gmt", (char *) NULL};
79 resultPtr = Tcl_GetObjResult(interp);
81 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?");
85 if (Tcl_GetIndexFromObj(interp, objv[1], switches, "option", 0, &index)
89 switch ((enum command) index) {
90 case COMMAND_CLICKS: { /* clicks */
94 format = Tcl_GetStringFromObj(objv[2], &n);
96 && ( strncmp( format, "-milliseconds",
97 (unsigned int) n) == 0 ) ) {
100 Tcl_AppendStringsToObj(resultPtr,
101 "bad switch \"", format,
102 "\": must be -milliseconds", (char *) NULL);
105 } else if (objc != 2) {
106 Tcl_WrongNumArgs(interp, 2, objv, "?-milliseconds?");
111 * We can enforce at least millisecond granularity
115 Tcl_SetLongObj(resultPtr,
116 (long) (time.sec*1000 + time.usec/1000));
118 Tcl_SetLongObj(resultPtr, (long) TclpGetClicks());
123 case COMMAND_FORMAT: /* format */
124 if ((objc < 3) || (objc > 7)) {
126 Tcl_WrongNumArgs(interp, 2, objv,
127 "clockval ?-format string? ?-gmt boolean?");
131 if (Tcl_GetWideIntFromObj(interp, objv[2], &clockVal)
139 if (Tcl_GetIndexFromObj(interp, objPtr[0], formatSwitches,
140 "switch", 0, &index) != TCL_OK) {
144 case 0: /* -format */
145 format = Tcl_GetStringFromObj(objPtr[1], &dummy);
148 if (Tcl_GetBooleanFromObj(interp, objPtr[1],
149 &useGMT) != TCL_OK) {
160 return FormatClock(interp, clockVal, useGMT,
163 case COMMAND_SCAN: /* scan */
164 if ((objc < 3) || (objc > 7)) {
166 Tcl_WrongNumArgs(interp, 2, objv,
167 "dateString ?-base clockValue? ?-gmt boolean?");
174 if (Tcl_GetIndexFromObj(interp, objPtr[0], scanSwitches,
175 "switch", 0, &index) != TCL_OK) {
180 baseObjPtr = objPtr[1];
183 if (Tcl_GetBooleanFromObj(interp, objPtr[1],
184 &useGMT) != TCL_OK) {
196 if (baseObjPtr != NULL) {
197 if (Tcl_GetWideIntFromObj(interp, baseObjPtr,
198 &baseClock) != TCL_OK) {
202 baseClock = TclpGetSeconds();
206 zone = -50000; /* Force GMT */
208 zone = TclpGetTimeZone(baseClock);
211 scanStr = Tcl_GetStringFromObj(objv[2], &dummy);
212 Tcl_MutexLock(&clockMutex);
213 if (TclGetDate(scanStr, baseClock, zone,
215 Tcl_MutexUnlock(&clockMutex);
216 Tcl_AppendStringsToObj(resultPtr,
217 "unable to convert date-time string \"",
218 scanStr, "\"", (char *) NULL);
221 Tcl_MutexUnlock(&clockMutex);
223 Tcl_SetWideIntObj(resultPtr, clockVal);
226 case COMMAND_SECONDS: /* seconds */
228 Tcl_WrongNumArgs(interp, 2, objv, NULL);
231 Tcl_SetLongObj(resultPtr, (long) TclpGetSeconds());
234 return TCL_ERROR; /* Should never be reached. */
239 *-----------------------------------------------------------------------------
243 * Formats a time value based on seconds into a human readable
247 * Standard Tcl result.
252 *-----------------------------------------------------------------------------
256 FormatClock(interp, clockVal, useGMT, format)
257 Tcl_Interp *interp; /* Current interpreter. */
258 Tcl_WideInt clockVal; /* Time in seconds. */
259 int useGMT; /* Boolean */
260 char *format; /* Format string */
262 struct tm *timeDataPtr;
263 Tcl_DString buffer, uniBuffer;
268 #if !defined(HAVE_TM_ZONE) && !defined(WIN32)
269 TIMEZONE_t savedTimeZone = 0; /* lint. */
270 char *savedTZEnv = NULL; /* lint. */
275 * Some systems forgot to call tzset in localtime, make sure its done.
277 static int calledTzset = 0;
279 Tcl_MutexLock(&clockMutex);
284 Tcl_MutexUnlock(&clockMutex);
288 * If the user gave us -format "", just return now
290 if (*format == '\0') {
294 #if !defined(HAVE_TM_ZONE) && !defined(WIN32)
296 * This is a kludge for systems not having the timezone string in
297 * struct tm. No matter what was specified, they use the local
298 * timezone string. Since this kludge requires fiddling with the
299 * TZ environment variable, it will mess up if done on multiple
300 * threads at once. Protect it with a the clock mutex.
303 Tcl_MutexLock( &clockMutex );
305 CONST char *varValue;
307 varValue = Tcl_GetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
308 if (varValue != NULL) {
309 savedTZEnv = strcpy(ckalloc(strlen(varValue) + 1), varValue);
313 Tcl_SetVar2(interp, "env", "TZ", "GMT0", TCL_GLOBAL_ONLY);
314 savedTimeZone = timezone;
320 tclockVal = (time_t) clockVal;
321 timeDataPtr = TclpGetDate((TclpTime_t) &tclockVal, useGMT);
324 * Make a guess at the upper limit on the substituted string size
325 * based on the number of percents in the string.
328 for (bufSize = 1, p = format; *p != '\0'; p++) {
335 Tcl_DStringInit(&uniBuffer);
336 Tcl_UtfToExternalDString(NULL, format, -1, &uniBuffer);
337 Tcl_DStringInit(&buffer);
338 Tcl_DStringSetLength(&buffer, bufSize);
340 /* If we haven't locked the clock mutex up above, lock it now. */
342 #if defined(HAVE_TM_ZONE) || defined(WIN32)
343 Tcl_MutexLock(&clockMutex);
345 result = TclpStrftime(buffer.string, (unsigned int) bufSize,
346 Tcl_DStringValue(&uniBuffer), timeDataPtr, useGMT);
347 #if defined(HAVE_TM_ZONE) || defined(WIN32)
348 Tcl_MutexUnlock(&clockMutex);
350 Tcl_DStringFree(&uniBuffer);
352 #if !defined(HAVE_TM_ZONE) && !defined(WIN32)
354 if (savedTZEnv != NULL) {
355 Tcl_SetVar2(interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
358 Tcl_UnsetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
360 timezone = savedTimeZone;
363 Tcl_MutexUnlock( &clockMutex );
368 * A zero return is the error case (can also mean the strftime
369 * didn't get enough space to write into). We know it doesn't
370 * mean that we wrote zero chars because the check for an empty
371 * format string is above.
373 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
374 "bad format string \"", format, "\"", (char *) NULL);
379 * Convert the time to UTF from external encoding [Bug: 3345]
381 Tcl_DStringInit(&uniBuffer);
382 Tcl_ExternalToUtfDString(NULL, buffer.string, -1, &uniBuffer);
384 Tcl_SetStringObj(Tcl_GetObjResult(interp), uniBuffer.string, -1);
386 Tcl_DStringFree(&uniBuffer);
387 Tcl_DStringFree(&buffer);