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

  1. /* 
  2.  * tclXcnvclock.c --
  3.  *
  4.  *      Contains the TCL convertclock command.  This is in a module seperate
  5.  * from clock so that it can be excluded, along with the yacc generated code,
  6.  * since its rather large.
  7.  *-----------------------------------------------------------------------------
  8.  * Copyright 1992 Karl Lehenbauer and Mark Diekhans.
  9.  *
  10.  * Permission to use, copy, modify, and distribute this software and its
  11.  * documentation for any purpose and without fee is hereby granted, provided
  12.  * that the above copyright notice appear in all copies.  Karl Lehenbauer and
  13.  * Mark Diekhans make no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without express or
  15.  * implied warranty.
  16.  *-----------------------------------------------------------------------------
  17.  * $Id: tclXcnvclock.c,v 2.1 1992/11/07 22:23:03 markd Exp $
  18.  *-----------------------------------------------------------------------------
  19.  */
  20.  
  21. #include "tclExtdInt.h"
  22.  
  23.  
  24. /*
  25.  *-----------------------------------------------------------------------------
  26.  *
  27.  * Tcl_ConvertclockCmd --
  28.  *     Implements the TCL convertclock command:
  29.  *         convertclock dateString [GMT|{}]
  30.  *
  31.  * Results:
  32.  *     Standard TCL results.
  33.  *
  34.  *-----------------------------------------------------------------------------
  35.  */
  36. int
  37. Tcl_ConvertclockCmd (clientData, interp, argc, argv)
  38.     ClientData  clientData;
  39.     Tcl_Interp *interp;
  40.     int         argc;
  41.     char      **argv;
  42. {
  43.     long        clockVal;
  44.     time_t      baseClock;
  45.     struct tm  *timeDataPtr;
  46.     long        zone;
  47.  
  48.     if ((argc < 2) || (argc > 4)) {
  49.         Tcl_AppendResult (interp, tclXWrongArgs, argv [0], 
  50.                           " dateString [GMT|{}] [baseclock]", (char *) NULL);
  51.     return TCL_ERROR;
  52.     }
  53.     if (argc == 4) {
  54.         if (Tcl_GetLong (interp, argv [3], &baseClock) != TCL_OK)
  55.             return TCL_ERROR;
  56.     } else
  57.         time (&baseClock);
  58.  
  59.     if ((argc > 2) && (argv [2][0] != '\0')) {
  60.         if (!STREQU (argv [2], "GMT")) {
  61.             Tcl_AppendResult (interp, "invalid argument: expected `GMT', ",
  62.                               "got : `", argv [2], "'", (char *) NULL);
  63.             return TCL_ERROR;
  64.         }
  65.         zone = 0; /* Zero minutes from GMT */
  66.     } else {
  67.         timeDataPtr = localtime (&baseClock);
  68.         /*
  69.          * Get the minutes east of GMT.
  70.          */
  71. #ifdef TCL_TM_GMTOFF
  72.         zone = -(timeDataPtr->tm_gmtoff / 60);
  73. #endif
  74. #ifdef TCL_TIMEZONE_VAR 
  75.         zone = timezone / 60;
  76. #endif
  77. #if  !defined(TCL_TM_GMTOFF) && !defined(TCL_TIMEZONE_VAR)
  78.         zone = timeDataPtr->tm_tzadj  / 60;
  79. #endif
  80.     }
  81.  
  82.     clockVal = Tcl_GetDate (argv [1], baseClock, zone);
  83.     if (clockVal == -1) {
  84.         Tcl_AppendResult (interp, "Unable to convert date-time string \"",
  85.                           argv [1], "\"", (char *) NULL);
  86.     return TCL_ERROR;
  87.     }
  88.     sprintf (interp->result, "%ld", clockVal);
  89.     return TCL_OK;
  90. }
  91.  
  92.