home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / tclx7_31.z / tclx7_31 / tcldev / tclX7.3a-p1 / src / tclXclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-19  |  4.4 KB  |  152 lines

  1. /* 
  2.  * tclXclock.c --
  3.  *
  4.  *      Contains the TCL time and date related commands.
  5.  *-----------------------------------------------------------------------------
  6.  * Copyright 1991-1993 Karl Lehenbauer and Mark Diekhans.
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11.  * Mark Diekhans make no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without express or
  13.  * implied warranty.
  14.  *-----------------------------------------------------------------------------
  15.  * $Id: tclXclock.c,v 3.0 1993/11/19 06:58:24 markd Rel $
  16.  *-----------------------------------------------------------------------------
  17.  */
  18.  
  19. #include "tclExtdInt.h"
  20.  
  21.  
  22. /*
  23.  *-----------------------------------------------------------------------------
  24.  *
  25.  * Tcl_GetclockCmd --
  26.  *     Implements the TCL getclock command:
  27.  *         getclock
  28.  *
  29.  * Results:
  30.  *     Standard TCL results.
  31.  *
  32.  *-----------------------------------------------------------------------------
  33.  */
  34. int
  35. Tcl_GetclockCmd (clientData, interp, argc, argv)
  36.     ClientData  clientData;
  37.     Tcl_Interp *interp;
  38.     int         argc;
  39.     char      **argv;
  40. {
  41.     if (argc != 1) {
  42.         Tcl_AppendResult (interp, tclXWrongArgs, argv[0], (char *) NULL);
  43.         return TCL_ERROR;
  44.     }
  45.     sprintf (interp->result, "%ld", time ((time_t *) NULL));
  46.     return TCL_OK;
  47. }
  48.  
  49. /*
  50.  *-----------------------------------------------------------------------------
  51.  *
  52.  * Tcl_FmtclockCmd --
  53.  *     Implements the TCL fmtclock command:
  54.  *         fmtclock clockval ?format? ?GMT|{}?
  55.  *
  56.  * Results:
  57.  *     Standard TCL results.
  58.  *
  59.  *-----------------------------------------------------------------------------
  60.  */
  61. int
  62. Tcl_FmtclockCmd (clientData, interp, argc, argv)
  63.     ClientData  clientData;
  64.     Tcl_Interp *interp;
  65.     int         argc;
  66.     char      **argv;
  67. {
  68.     int              useGMT = FALSE;
  69.     time_t           clockVal;
  70.     char            *format;
  71.     struct tm       *timeDataPtr;
  72.     int              stat;
  73. #ifdef TCL_USE_TIMEZONE_VAR
  74.     int              savedTimeZone;
  75.     char            *savedTZEnv;
  76. #endif
  77.  
  78.     if ((argc < 2) || (argc > 4)) {
  79.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  80.                           " clockval ?format? ?GMT|{}?", (char *) NULL);
  81.         return TCL_ERROR;
  82.     }
  83.  
  84.     if (Tcl_GetTime (interp, argv[1], &clockVal) != TCL_OK)
  85.         return TCL_ERROR;
  86.     if ((argc == 4) && (argv [3][0] != '\0')) {
  87.         if (!STREQU (argv [3], "GMT")) {
  88.             Tcl_AppendResult (interp, "expected \"GMT\" or {} got \"",
  89.                               argv [3], "\"", (char *) NULL);
  90.             return TCL_ERROR;
  91.         }
  92.         useGMT = TRUE;
  93.     }
  94.  
  95.     if ((argc >= 3) && (argv [2][0] != '\0'))
  96.         format = argv[2];
  97.     else
  98.         format = "%a %b %d %X %Z %Y";
  99.  
  100. #ifdef TCL_USE_TIMEZONE_VAR
  101.     /*
  102.      * This is a horrible kludge for systems not having the timezone in
  103.      * struct tm.  No matter what was specified, they use the global time
  104.      * zone.
  105.      */
  106.     if (useGMT) {
  107.         char *varValue;
  108.  
  109.         varValue = Tcl_GetVar2 (interp, "env", "TZ", TCL_GLOBAL_ONLY);
  110.         if (varValue != NULL)
  111.             savedTZEnv = ckstrdup (varValue);
  112.         else
  113.             savedTZEnv = NULL;
  114.         Tcl_SetVar2 (interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
  115.         savedTimeZone = timezone;
  116.         timezone = 0;
  117.         tzset ();
  118.     }
  119. #endif
  120.  
  121.     if (useGMT)
  122.         timeDataPtr = gmtime (&clockVal);
  123.     else    
  124.         timeDataPtr = localtime (&clockVal);
  125.  
  126.     stat = strftime (interp->result, TCL_RESULT_SIZE, format, timeDataPtr);
  127.  
  128. #ifdef TCL_USE_TIMEZONE_VAR
  129.     if (useGMT) {
  130.         if (savedTZEnv != NULL) {
  131.             Tcl_SetVar2 (interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
  132.             ckfree (savedTZEnv);
  133.         } else {
  134.             Tcl_UnsetVar2 (interp, "env", "TZ", TCL_GLOBAL_ONLY);
  135.         }
  136.         timezone = savedTimeZone;
  137.         tzset ();
  138.     }
  139. #endif
  140.  
  141.     if (stat < 0) {
  142.         char numStr [32];
  143.  
  144.         sprintf (numStr, "%d", TCL_RESULT_SIZE - 1);
  145.         Tcl_AppendResult (interp, "invalid format string or formatted time ",
  146.                           "buffer overflow (max = ", numStr, " characters)",
  147.                           (char *) NULL);
  148.         return TCL_ERROR;
  149.     }
  150.     return TCL_OK;
  151. }
  152.