home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / tcl / tclX6.5c / src / tclXclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-19  |  3.2 KB  |  109 lines

  1. /* 
  2.  * tclXclock.c --
  3.  *
  4.  *      Contains the TCL time and date related commands.
  5.  *-----------------------------------------------------------------------------
  6.  * Copyright 1992 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 2.0 1992/10/16 04:50:28 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 ((long *) 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.     long             clockVal;
  70.     char            *format;
  71.     struct tm       *timeDataPtr;
  72.     int              fmtError;
  73.  
  74.     if ((argc < 2) || (argc > 4)) {
  75.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  76.                           " clockval [format] [GMT|{}]", (char *) NULL);
  77.         return TCL_ERROR;
  78.     }
  79.  
  80.     if (Tcl_GetLong (interp, argv[1], &clockVal) != TCL_OK)
  81.         return TCL_ERROR;
  82.     if ((argc == 4) && (argv [3][0] != '\0')) {
  83.         if (!STREQU (argv [3], "GMT")) {
  84.             Tcl_AppendResult (interp, "expected \"GMT\" or {} got \"",
  85.                               argv [3], "\"", (char *) NULL);
  86.             return TCL_ERROR;
  87.         }
  88.         useGMT = TRUE;
  89.     }
  90.  
  91.     if ((argc >= 3) && (argv [2][0] != '\0'))
  92.         format = argv[2];
  93.     else
  94.         format = "%a %b %d %X %Z %Y";
  95.  
  96.     if (useGMT)
  97.         timeDataPtr = gmtime (&clockVal);
  98.     else    
  99.         timeDataPtr = localtime (&clockVal);
  100.  
  101.     fmtError = strftime (interp->result, TCL_RESULT_SIZE, format, 
  102.                          timeDataPtr) < 0;
  103.     if (fmtError) {
  104.         Tcl_AppendResult (interp, "error formating time", (char *) NULL);
  105.         return TCL_ERROR;
  106.     }
  107.     return TCL_OK;
  108. }
  109.