OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tcl/] [generic/] [tclClock.c] - Diff between revs 578 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 578 Rev 1765
/*
/*
 * tclClock.c --
 * tclClock.c --
 *
 *
 *      Contains the time and date related commands.  This code
 *      Contains the time and date related commands.  This code
 *      is derived from the time and date facilities of TclX,
 *      is derived from the time and date facilities of TclX,
 *      by Mark Diekhans and Karl Lehenbauer.
 *      by Mark Diekhans and Karl Lehenbauer.
 *
 *
 * Copyright 1991-1995 Karl Lehenbauer and Mark Diekhans.
 * Copyright 1991-1995 Karl Lehenbauer and Mark Diekhans.
 * Copyright (c) 1995 Sun Microsystems, Inc.
 * Copyright (c) 1995 Sun Microsystems, Inc.
 *
 *
 * See the file "license.terms" for information on usage and redistribution
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 *
 * RCS: @(#) $Id: tclClock.c,v 1.1.1.1 2002-01-16 10:25:25 markom Exp $
 * RCS: @(#) $Id: tclClock.c,v 1.1.1.1 2002-01-16 10:25:25 markom Exp $
 */
 */
 
 
#include "tcl.h"
#include "tcl.h"
#include "tclInt.h"
#include "tclInt.h"
#include "tclPort.h"
#include "tclPort.h"
 
 
/*
/*
 * Function prototypes for local procedures in this file:
 * Function prototypes for local procedures in this file:
 */
 */
 
 
static int              FormatClock _ANSI_ARGS_((Tcl_Interp *interp,
static int              FormatClock _ANSI_ARGS_((Tcl_Interp *interp,
                            unsigned long clockVal, int useGMT,
                            unsigned long clockVal, int useGMT,
                            char *format));
                            char *format));


/*
/*
 *-------------------------------------------------------------------------
 *-------------------------------------------------------------------------
 *
 *
 * Tcl_ClockObjCmd --
 * Tcl_ClockObjCmd --
 *
 *
 *      This procedure is invoked to process the "clock" Tcl command.
 *      This procedure is invoked to process the "clock" Tcl command.
 *      See the user documentation for details on what it does.
 *      See the user documentation for details on what it does.
 *
 *
 * Results:
 * Results:
 *      A standard Tcl result.
 *      A standard Tcl result.
 *
 *
 * Side effects:
 * Side effects:
 *      See the user documentation.
 *      See the user documentation.
 *
 *
 *-------------------------------------------------------------------------
 *-------------------------------------------------------------------------
 */
 */
 
 
int
int
Tcl_ClockObjCmd (client, interp, objc, objv)
Tcl_ClockObjCmd (client, interp, objc, objv)
    ClientData client;                  /* Not used. */
    ClientData client;                  /* Not used. */
    Tcl_Interp *interp;                 /* Current interpreter. */
    Tcl_Interp *interp;                 /* Current interpreter. */
    int objc;                           /* Number of arguments. */
    int objc;                           /* Number of arguments. */
    Tcl_Obj *CONST objv[];              /* Argument values. */
    Tcl_Obj *CONST objv[];              /* Argument values. */
{
{
    Tcl_Obj *resultPtr;
    Tcl_Obj *resultPtr;
    int index;
    int index;
    Tcl_Obj *CONST *objPtr;
    Tcl_Obj *CONST *objPtr;
    int useGMT = 0;
    int useGMT = 0;
    char *format = "%a %b %d %X %Z %Y";
    char *format = "%a %b %d %X %Z %Y";
    int dummy;
    int dummy;
    unsigned long baseClock, clockVal;
    unsigned long baseClock, clockVal;
    long zone;
    long zone;
    Tcl_Obj *baseObjPtr = NULL;
    Tcl_Obj *baseObjPtr = NULL;
    char *scanStr;
    char *scanStr;
 
 
    static char *switches[] =
    static char *switches[] =
            {"clicks", "format", "scan", "seconds", (char *) NULL};
            {"clicks", "format", "scan", "seconds", (char *) NULL};
    static char *formatSwitches[] = {"-format", "-gmt", (char *) NULL};
    static char *formatSwitches[] = {"-format", "-gmt", (char *) NULL};
    static char *scanSwitches[] = {"-base", "-gmt", (char *) NULL};
    static char *scanSwitches[] = {"-base", "-gmt", (char *) NULL};
 
 
    resultPtr = Tcl_GetObjResult(interp);
    resultPtr = Tcl_GetObjResult(interp);
    if (objc < 2) {
    if (objc < 2) {
        Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?");
        Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?");
        return TCL_ERROR;
        return TCL_ERROR;
    }
    }
 
 
    if (Tcl_GetIndexFromObj(interp, objv[1], switches, "option", 0, &index)
    if (Tcl_GetIndexFromObj(interp, objv[1], switches, "option", 0, &index)
            != TCL_OK) {
            != TCL_OK) {
        return TCL_ERROR;
        return TCL_ERROR;
    }
    }
    switch (index) {
    switch (index) {
        case 0:                  /* clicks */
        case 0:                  /* clicks */
            if (objc != 2) {
            if (objc != 2) {
                Tcl_WrongNumArgs(interp, 2, objv, NULL);
                Tcl_WrongNumArgs(interp, 2, objv, NULL);
                return TCL_ERROR;
                return TCL_ERROR;
            }
            }
            Tcl_SetLongObj(resultPtr, (long) TclpGetClicks());
            Tcl_SetLongObj(resultPtr, (long) TclpGetClicks());
            return TCL_OK;
            return TCL_OK;
        case 1:                 /* format */
        case 1:                 /* format */
            if ((objc < 3) || (objc > 7)) {
            if ((objc < 3) || (objc > 7)) {
                wrongFmtArgs:
                wrongFmtArgs:
                Tcl_WrongNumArgs(interp, 2, objv,
                Tcl_WrongNumArgs(interp, 2, objv,
                        "clockval ?-format string? ?-gmt boolean?");
                        "clockval ?-format string? ?-gmt boolean?");
                return TCL_ERROR;
                return TCL_ERROR;
            }
            }
 
 
            if (Tcl_GetLongFromObj(interp, objv[2], (long*) &clockVal)
            if (Tcl_GetLongFromObj(interp, objv[2], (long*) &clockVal)
                    != TCL_OK) {
                    != TCL_OK) {
                return TCL_ERROR;
                return TCL_ERROR;
            }
            }
 
 
            objPtr = objv+3;
            objPtr = objv+3;
            objc -= 3;
            objc -= 3;
            while (objc > 1) {
            while (objc > 1) {
                if (Tcl_GetIndexFromObj(interp, objPtr[0], formatSwitches,
                if (Tcl_GetIndexFromObj(interp, objPtr[0], formatSwitches,
                        "switch", 0, &index) != TCL_OK) {
                        "switch", 0, &index) != TCL_OK) {
                    return TCL_ERROR;
                    return TCL_ERROR;
                }
                }
                switch (index) {
                switch (index) {
                    case 0:              /* -format */
                    case 0:              /* -format */
                        format = Tcl_GetStringFromObj(objPtr[1], &dummy);
                        format = Tcl_GetStringFromObj(objPtr[1], &dummy);
                        break;
                        break;
                    case 1:             /* -gmt */
                    case 1:             /* -gmt */
                        if (Tcl_GetBooleanFromObj(interp, objPtr[1],
                        if (Tcl_GetBooleanFromObj(interp, objPtr[1],
                                &useGMT) != TCL_OK) {
                                &useGMT) != TCL_OK) {
                            return TCL_ERROR;
                            return TCL_ERROR;
                        }
                        }
                        break;
                        break;
                }
                }
                objPtr += 2;
                objPtr += 2;
                objc -= 2;
                objc -= 2;
            }
            }
            if (objc != 0) {
            if (objc != 0) {
                goto wrongFmtArgs;
                goto wrongFmtArgs;
            }
            }
            return FormatClock(interp, (unsigned long) clockVal, useGMT,
            return FormatClock(interp, (unsigned long) clockVal, useGMT,
                    format);
                    format);
        case 2:                 /* scan */
        case 2:                 /* scan */
            if ((objc < 3) || (objc > 7)) {
            if ((objc < 3) || (objc > 7)) {
                wrongScanArgs:
                wrongScanArgs:
                Tcl_WrongNumArgs(interp, 2, objv,
                Tcl_WrongNumArgs(interp, 2, objv,
                        "dateString ?-base clockValue? ?-gmt boolean?");
                        "dateString ?-base clockValue? ?-gmt boolean?");
                return TCL_ERROR;
                return TCL_ERROR;
            }
            }
 
 
            objPtr = objv+3;
            objPtr = objv+3;
            objc -= 3;
            objc -= 3;
            while (objc > 1) {
            while (objc > 1) {
                if (Tcl_GetIndexFromObj(interp, objPtr[0], scanSwitches,
                if (Tcl_GetIndexFromObj(interp, objPtr[0], scanSwitches,
                        "switch", 0, &index) != TCL_OK) {
                        "switch", 0, &index) != TCL_OK) {
                    return TCL_ERROR;
                    return TCL_ERROR;
                }
                }
                switch (index) {
                switch (index) {
                    case 0:              /* -base */
                    case 0:              /* -base */
                        baseObjPtr = objPtr[1];
                        baseObjPtr = objPtr[1];
                        break;
                        break;
                    case 1:             /* -gmt */
                    case 1:             /* -gmt */
                        if (Tcl_GetBooleanFromObj(interp, objPtr[1],
                        if (Tcl_GetBooleanFromObj(interp, objPtr[1],
                                &useGMT) != TCL_OK) {
                                &useGMT) != TCL_OK) {
                            return TCL_ERROR;
                            return TCL_ERROR;
                        }
                        }
                        break;
                        break;
                }
                }
                objPtr += 2;
                objPtr += 2;
                objc -= 2;
                objc -= 2;
            }
            }
            if (objc != 0) {
            if (objc != 0) {
                goto wrongScanArgs;
                goto wrongScanArgs;
            }
            }
 
 
            if (baseObjPtr != NULL) {
            if (baseObjPtr != NULL) {
                if (Tcl_GetLongFromObj(interp, baseObjPtr,
                if (Tcl_GetLongFromObj(interp, baseObjPtr,
                        (long*) &baseClock) != TCL_OK) {
                        (long*) &baseClock) != TCL_OK) {
                    return TCL_ERROR;
                    return TCL_ERROR;
                }
                }
            } else {
            } else {
                baseClock = TclpGetSeconds();
                baseClock = TclpGetSeconds();
            }
            }
 
 
            if (useGMT) {
            if (useGMT) {
                zone = -50000; /* Force GMT */
                zone = -50000; /* Force GMT */
            } else {
            } else {
                zone = TclpGetTimeZone((unsigned long) baseClock);
                zone = TclpGetTimeZone((unsigned long) baseClock);
            }
            }
 
 
            scanStr = Tcl_GetStringFromObj(objv[2], &dummy);
            scanStr = Tcl_GetStringFromObj(objv[2], &dummy);
            if (TclGetDate(scanStr, (unsigned long) baseClock, zone,
            if (TclGetDate(scanStr, (unsigned long) baseClock, zone,
                    (unsigned long *) &clockVal) < 0) {
                    (unsigned long *) &clockVal) < 0) {
                Tcl_AppendStringsToObj(resultPtr,
                Tcl_AppendStringsToObj(resultPtr,
                        "unable to convert date-time string \"",
                        "unable to convert date-time string \"",
                        scanStr, "\"", (char *) NULL);
                        scanStr, "\"", (char *) NULL);
                return TCL_ERROR;
                return TCL_ERROR;
            }
            }
 
 
            Tcl_SetLongObj(resultPtr, (long) clockVal);
            Tcl_SetLongObj(resultPtr, (long) clockVal);
            return TCL_OK;
            return TCL_OK;
        case 3:                 /* seconds */
        case 3:                 /* seconds */
            if (objc != 2) {
            if (objc != 2) {
                Tcl_WrongNumArgs(interp, 2, objv, NULL);
                Tcl_WrongNumArgs(interp, 2, objv, NULL);
                return TCL_ERROR;
                return TCL_ERROR;
            }
            }
            Tcl_SetLongObj(resultPtr, (long) TclpGetSeconds());
            Tcl_SetLongObj(resultPtr, (long) TclpGetSeconds());
            return TCL_OK;
            return TCL_OK;
        default:
        default:
            return TCL_ERROR;   /* Should never be reached. */
            return TCL_ERROR;   /* Should never be reached. */
    }
    }
}
}


/*
/*
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 *
 *
 * FormatClock --
 * FormatClock --
 *
 *
 *      Formats a time value based on seconds into a human readable
 *      Formats a time value based on seconds into a human readable
 *      string.
 *      string.
 *
 *
 * Results:
 * Results:
 *      Standard Tcl result.
 *      Standard Tcl result.
 *
 *
 * Side effects:
 * Side effects:
 *      None.
 *      None.
 *
 *
 *-----------------------------------------------------------------------------
 *-----------------------------------------------------------------------------
 */
 */
 
 
static int
static int
FormatClock(interp, clockVal, useGMT, format)
FormatClock(interp, clockVal, useGMT, format)
    Tcl_Interp *interp;                 /* Current interpreter. */
    Tcl_Interp *interp;                 /* Current interpreter. */
    unsigned long clockVal;             /* Time in seconds. */
    unsigned long clockVal;             /* Time in seconds. */
    int useGMT;                         /* Boolean */
    int useGMT;                         /* Boolean */
    char *format;                       /* Format string */
    char *format;                       /* Format string */
{
{
    struct tm *timeDataPtr;
    struct tm *timeDataPtr;
    Tcl_DString buffer;
    Tcl_DString buffer;
    int bufSize;
    int bufSize;
    char *p;
    char *p;
#ifdef TCL_USE_TIMEZONE_VAR
#ifdef TCL_USE_TIMEZONE_VAR
    int savedTimeZone;
    int savedTimeZone;
    char *savedTZEnv;
    char *savedTZEnv;
#endif
#endif
    Tcl_Obj *resultPtr;
    Tcl_Obj *resultPtr;
 
 
    resultPtr = Tcl_GetObjResult(interp);
    resultPtr = Tcl_GetObjResult(interp);
#ifdef HAVE_TZSET
#ifdef HAVE_TZSET
    /*
    /*
     * Some systems forgot to call tzset in localtime, make sure its done.
     * Some systems forgot to call tzset in localtime, make sure its done.
     */
     */
    static int  calledTzset = 0;
    static int  calledTzset = 0;
 
 
    if (!calledTzset) {
    if (!calledTzset) {
        tzset();
        tzset();
        calledTzset = 1;
        calledTzset = 1;
    }
    }
#endif
#endif
 
 
#ifdef TCL_USE_TIMEZONE_VAR
#ifdef TCL_USE_TIMEZONE_VAR
    /*
    /*
     * This is a horrible kludge for systems not having the timezone in
     * This is a horrible kludge for systems not having the timezone in
     * struct tm.  No matter what was specified, they use the global time
     * struct tm.  No matter what was specified, they use the global time
     * zone.  (Thanks Solaris).
     * zone.  (Thanks Solaris).
     */
     */
    if (useGMT) {
    if (useGMT) {
        char *varValue;
        char *varValue;
 
 
        varValue = Tcl_GetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
        varValue = Tcl_GetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
        if (varValue != NULL) {
        if (varValue != NULL) {
            savedTZEnv = strcpy(ckalloc(strlen(varValue) + 1), varValue);
            savedTZEnv = strcpy(ckalloc(strlen(varValue) + 1), varValue);
        } else {
        } else {
            savedTZEnv = NULL;
            savedTZEnv = NULL;
        }
        }
        Tcl_SetVar2(interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
        Tcl_SetVar2(interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
        savedTimeZone = timezone;
        savedTimeZone = timezone;
        timezone = 0;
        timezone = 0;
        tzset();
        tzset();
    }
    }
#endif
#endif
 
 
    timeDataPtr = TclpGetDate((time_t *) &clockVal, useGMT);
    timeDataPtr = TclpGetDate((time_t *) &clockVal, useGMT);
 
 
    /*
    /*
     * Make a guess at the upper limit on the substituted string size
     * Make a guess at the upper limit on the substituted string size
     * based on the number of percents in the string.
     * based on the number of percents in the string.
     */
     */
 
 
    for (bufSize = 1, p = format; *p != '\0'; p++) {
    for (bufSize = 1, p = format; *p != '\0'; p++) {
        if (*p == '%') {
        if (*p == '%') {
            bufSize += 40;
            bufSize += 40;
        } else {
        } else {
            bufSize++;
            bufSize++;
        }
        }
    }
    }
    Tcl_DStringInit(&buffer);
    Tcl_DStringInit(&buffer);
    Tcl_DStringSetLength(&buffer, bufSize);
    Tcl_DStringSetLength(&buffer, bufSize);
 
 
    if ((TclStrftime(buffer.string, (unsigned int) bufSize, format,
    if ((TclStrftime(buffer.string, (unsigned int) bufSize, format,
            timeDataPtr) == 0) && (*format != '\0')) {
            timeDataPtr) == 0) && (*format != '\0')) {
        Tcl_AppendStringsToObj(resultPtr, "bad format string \"",
        Tcl_AppendStringsToObj(resultPtr, "bad format string \"",
                format, "\"", (char *) NULL);
                format, "\"", (char *) NULL);
        return TCL_ERROR;
        return TCL_ERROR;
    }
    }
 
 
#ifdef TCL_USE_TIMEZONE_VAR
#ifdef TCL_USE_TIMEZONE_VAR
    if (useGMT) {
    if (useGMT) {
        if (savedTZEnv != NULL) {
        if (savedTZEnv != NULL) {
            Tcl_SetVar2(interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
            Tcl_SetVar2(interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
            ckfree(savedTZEnv);
            ckfree(savedTZEnv);
        } else {
        } else {
            Tcl_UnsetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
            Tcl_UnsetVar2(interp, "env", "TZ", TCL_GLOBAL_ONLY);
        }
        }
        timezone = savedTimeZone;
        timezone = savedTimeZone;
        tzset();
        tzset();
    }
    }
#endif
#endif
 
 
    Tcl_SetStringObj(resultPtr, buffer.string, -1);
    Tcl_SetStringObj(resultPtr, buffer.string, -1);
    Tcl_DStringFree(&buffer);
    Tcl_DStringFree(&buffer);
    return TCL_OK;
    return TCL_OK;
}
}
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.