"Fossies" - the Fresh Open Source Software Archive

Member "ical-tcl/time_tcl.C" (15 Apr 2019, 10057 Bytes) of package /linux/privat/ical-3.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "time_tcl.C": 3.0.2_vs_3.0.3.

    1 /* Copyright (c) 1993 by Sanjay Ghemawat */
    2 /*
    3  * Time/Date interface for Tcl.
    4  */
    5 
    6 #include <stdio.h>
    7 #include <string.h>
    8 
    9 extern "C" {
   10 #include <tcl.h>
   11 }
   12 
   13 #include "Date.h"
   14 #include "Time_.h"
   15 
   16 #include "Month.h"
   17 #include "WeekDay.h"
   18 #include "Year.h"
   19 #include "parse.h"
   20 
   21 #include "ical.h"
   22 
   23 /*
   24  * Tcl representations for time/date entities
   25  *
   26  *      Month    is an integer in range 1..12
   27  *      Monthday is an integer in range 1..31
   28  *      Weekday  is an integer in range 1..7 (1 == Sunday)
   29  *      Year     is an integer in some range (possibly 1900..2400)
   30  *      Date     is an integer since some unspecified date)
   31  *      Time     is a double in seconds since some unspecified date
   32  */
   33 
   34 /* Date command */
   35 int Cmd_Date(ClientData, Tcl_Interp* tcl, int argc, const char* argv[]) {
   36     const char* cmd = argv[1];
   37     char buffer[100];
   38     int days;
   39     Date d;
   40 
   41     switch (argc) {
   42       case 2:
   43         if (strcmp(cmd, "today") == 0) {
   44             sprintf(buffer, "%d", Date::Today().EpochDays());
   45             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   46             return TCL_OK;
   47         }
   48         if (strcmp(cmd, "first") == 0) {
   49             sprintf(buffer, "%d", Date::First().EpochDays());
   50             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   51             return TCL_OK;
   52         }
   53         if (strcmp(cmd, "last") == 0) {
   54             sprintf(buffer, "%d", Date::Last().EpochDays());
   55             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   56             return TCL_OK;
   57         }
   58         break;
   59       case 3:
   60         /* Argv[2] is date. */
   61         if (Tcl_GetInt(tcl, argv[2], &days) != TCL_OK) {
   62             return TCL_ERROR;
   63         }
   64         d = Date(days);
   65 
   66         if (strcmp(cmd, "monthsize") == 0) {
   67             sprintf(buffer, "%d", d.GetMonth().Size(d.GetYear()));
   68             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   69             return TCL_OK;
   70         }
   71         if (strcmp(cmd, "monthday") == 0) {
   72             sprintf(buffer, "%d", d.GetMDay());
   73             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   74             return TCL_OK;
   75         }
   76         if (strcmp(cmd, "weekday") == 0) {
   77             sprintf(buffer, "%d", d.GetWDay().Index());
   78             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   79             return TCL_OK;
   80         }
   81         if (strcmp(cmd, "month") == 0) {
   82             sprintf(buffer, "%d", d.GetMonth().Index());
   83             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   84             return TCL_OK;
   85         }
   86         if (strcmp(cmd, "year") == 0) {
   87             sprintf(buffer, "%d", d.GetYear());
   88             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
   89             return TCL_OK;
   90         }
   91         if (strcmp(cmd, "split") == 0) {
   92             int mday, year;
   93             Month month;
   94             WeekDay wday;
   95 
   96             d.BreakDown(mday, wday, month, year);
   97             sprintf(buffer, "%d %d %d %d",
   98                     mday,
   99                     wday.Index(),
  100                     month.Index(),
  101                     year);
  102             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  103             return TCL_OK;
  104         }
  105         break;
  106       case 5:
  107         if (strcmp(cmd, "make") == 0) {
  108             int month, year;
  109 
  110             if ((Tcl_GetInt(tcl, argv[2], &days) != TCL_OK) ||
  111                 (days < 1) ||
  112                 (days > 31) ||
  113                 (Tcl_GetInt(tcl, argv[3], &month) != TCL_OK) ||
  114                 (month < 1) ||
  115                 (month > 12) ||
  116                 (Tcl_GetInt(tcl, argv[4], &year) != TCL_OK) ||
  117                 (year < Year::First()) ||
  118                 (year > Year::Last())) {
  119                 TCL_Error(tcl, "illegal date specification");
  120             }
  121 
  122             d = Date(days, Month::First()+(month-1), year);
  123             sprintf(buffer, "%d", d.EpochDays());
  124             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  125             return TCL_OK;
  126         }
  127         break;
  128       case 6:
  129         if (strcmp(cmd, "extract") == 0) {
  130             // "date extract <string> <datevar> <prevar> <postvar>"
  131             Date result;
  132             int start, length;
  133 
  134             if (!find_date(argv[2], result, start, length))
  135                 TCL_Return(tcl, "0");
  136 
  137             // Set variables
  138             sprintf(buffer, "%d", result.EpochDays());
  139             if (Tcl_SetVar(tcl, argv[3], buffer, 0) == NULL)
  140                 TCL_Error(tcl, "could not set <date> result");
  141 
  142             char* pre = new char[start+1];
  143             strncpy(pre, argv[2], start);
  144             pre[start] = '\0';
  145             if (Tcl_SetVar(tcl, argv[4], pre, 0) == NULL) {
  146                 delete [] pre;
  147                 TCL_Error(tcl, "could not set <pre> result");
  148             }
  149             delete [] pre;
  150 
  151             if (Tcl_SetVar(tcl, argv[5], argv[2]+start+length, 0) == NULL)
  152                 TCL_Error(tcl, "could not set <post> result");
  153 
  154             TCL_Return(tcl, "1");
  155         }
  156         break;
  157       default:
  158         break;
  159     }
  160 
  161     TCL_Error(tcl, "invalid arguments to date");
  162 }
  163 
  164 /* Time command */
  165 int Cmd_Time(ClientData, Tcl_Interp* tcl, int argc, const char** argv) {
  166     const char* cmd = argv[1];
  167     char buffer[100];
  168     double secs;
  169     Time t;
  170     int hour, minute, second, millisecond;
  171     int day, year;
  172     Month month;
  173     WeekDay wday;
  174 
  175     if (strcmp(cmd, "split") == 0) {
  176         bool all=false;
  177         const char *tz=0;
  178         argc-=2;
  179         argv+=2;
  180 
  181         if (argc > 1 && strcmp(argv[0], "-all") == 0) {
  182             all=true;
  183             argc--;
  184             argv++;
  185         }
  186         if (argc > 2 && strcmp(argv[0], "-timezone") == 0) {
  187             tz=argv[1];
  188             argc-=2;
  189             argv+=2;
  190         }
  191         if (argc > 1)
  192                 TCL_Error(tcl, "invalid arguments to time");
  193         if (Tcl_GetDouble(tcl, argv[0], &secs) != TCL_OK)
  194             return TCL_ERROR;
  195         Time(secs).BreakDown(day, wday, month, year,
  196                 hour, minute, second, millisecond, tz);
  197         if (all)
  198             sprintf(buffer, "%d %d %d %d %d %d %d %d",
  199                     day, wday.Index(), month.Index(), year,
  200                     hour, minute, second, millisecond);
  201         else
  202             sprintf(buffer, "%d %d %d %d",
  203                     hour, minute, second, millisecond);
  204         Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  205         return TCL_OK;
  206     }
  207     switch (argc) {
  208       case 2:
  209         if (strcmp(cmd, "now") == 0) {
  210             sprintf(buffer, "%lf", Time::Now().EpochSeconds());
  211             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  212             return TCL_OK;
  213         }
  214         break;
  215       case 3:
  216         /* Argv[2] is time */
  217         if (Tcl_GetDouble(tcl, argv[2], &secs) != TCL_OK) {
  218             return TCL_ERROR;
  219         }
  220         t = Time(secs);
  221 
  222         if (strcmp(cmd, "date") == 0) {
  223             t.BreakDownDate(day, wday, month, year);
  224             Date date(day, month, year);
  225 
  226             sprintf(buffer, "%d", date.EpochDays());
  227             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  228             return TCL_OK;
  229         }
  230 
  231         t.BreakDownClock(hour, minute, second, millisecond);
  232         if (strcmp(cmd, "hour") == 0) {
  233             sprintf(buffer, "%d", hour);
  234             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  235             return TCL_OK;
  236         }
  237         if (strcmp(cmd, "minute") == 0) {
  238             sprintf(buffer, "%d", minute);
  239             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  240             return TCL_OK;
  241         }
  242         if (strcmp(cmd, "second") == 0) {
  243             sprintf(buffer, "%d", second);
  244             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  245             return TCL_OK;
  246         }
  247         if (strcmp(cmd, "millisecond") == 0) {
  248             sprintf(buffer, "%d", millisecond);
  249             Tcl_SetResult(tcl, buffer, TCL_VOLATILE);
  250             return TCL_OK;
  251         }
  252         break;
  253       case 6:
  254         if (strcmp(cmd, "extract_time") == 0) {
  255             // "date extract_time <string> <todvar> <prevar> <postvar>"
  256             int result;
  257             int start, length;
  258 
  259             if (!find_timeofday(argv[2], result, start, length))
  260                 TCL_Return(tcl, "0");
  261 
  262             // Set variables
  263             sprintf(buffer, "%d", result);
  264             if (Tcl_SetVar(tcl, argv[3], buffer, 0) == NULL)
  265                 TCL_Error(tcl, "could not set <time of day> result");
  266 
  267             char* pre = new char[start+1];
  268             strncpy(pre, argv[2], start);
  269             pre[start] = '\0';
  270             if (Tcl_SetVar(tcl, argv[4], pre, 0) == NULL) {
  271                 delete [] pre;
  272                 TCL_Error(tcl, "could not set <pre> result");
  273             }
  274             delete [] pre;
  275 
  276             if (Tcl_SetVar(tcl, argv[5], argv[2]+start+length, 0) == NULL)
  277                 TCL_Error(tcl, "could not set <post> result");
  278 
  279             TCL_Return(tcl, "1");
  280         }
  281         break;
  282       case 7:
  283         if (strcmp(cmd, "extract_range") == 0) {
  284             // "date extract_range <string> <startvar> <finishvar>\
  285             //                     <prevar> <postvar>"
  286             int r1, r2;
  287             int start, length;
  288 
  289             if (!find_timerange(argv[2], r1, r2, start, length))
  290                 TCL_Return(tcl, "0");
  291 
  292             // Set variables
  293             sprintf(buffer, "%d", r1);
  294             if (Tcl_SetVar(tcl, argv[3], buffer, 0) == NULL)
  295                 TCL_Error(tcl, "could not set <start time> result");
  296 
  297             // Set variables
  298             sprintf(buffer, "%d", r2);
  299             if (Tcl_SetVar(tcl, argv[4], buffer, 0) == NULL)
  300                 TCL_Error(tcl, "could not set <finish time> result");
  301 
  302             char* pre = new char[start+1];
  303             strncpy(pre, argv[2], start);
  304             pre[start] = '\0';
  305             if (Tcl_SetVar(tcl, argv[5], pre, 0) == NULL) {
  306                 delete [] pre;
  307                 TCL_Error(tcl, "could not set <pre> result");
  308             }
  309             delete [] pre;
  310 
  311             if (Tcl_SetVar(tcl, argv[6], argv[2]+start+length, 0) == NULL)
  312                 TCL_Error(tcl, "could not set <post> result");
  313 
  314             TCL_Return(tcl, "1");
  315         }
  316         break;
  317       default:
  318         break;
  319     }
  320 
  321     TCL_Error(tcl, "invalid arguments to time");
  322 }