home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / common / time.c < prev    next >
C/C++ Source or Header  |  1996-03-22  |  9KB  |  455 lines

  1. #include "::h:gsupport.h"
  2.  
  3. /*
  4.  * The following code is operating-system dependent [@time.01].  Include files
  5.  *  that are system-dependent.
  6.  */
  7.  
  8. #if PORT
  9.    /* probably needs something */
  10. Deliberate Syntax Error
  11. #endif                    /* PORT */
  12.  
  13. #if AMIGA
  14. #include "time.h"
  15. #endif                    /* AMIGA */
  16.  
  17. #if ATARI_ST
  18.    /* nothing is needed */
  19. #endif                    /* ATARI_ST */
  20.  
  21. #if ARM || MVS || VM
  22. #include <time.h>
  23. #endif                    /* ARM || MVS || ... */
  24.  
  25. #if MACINTOSH
  26. #if LSC
  27. #include <time.h>
  28. #endif                    /* LSC */
  29. #if MPW
  30. #include <types.h>
  31. #include "time.h"
  32. #include <OSUtils.h>
  33. #include <Events.h>
  34. #endif                    /* MPW */
  35. #if THINK_C
  36. #include <time.h>
  37. #endif                    /* THINK_C */
  38. #endif                    /* MACINTOSH */
  39.  
  40. #if MSDOS
  41. #include <time.h>
  42. #if MICROSOFT
  43. #include <sys/types.h>
  44. #endif                    /* MICROSOFT */
  45. #if ZTC_386 || SCCX_CX
  46. #include <dos.h>
  47. #endif                    /* ZTC_386 || SCCX_MX */
  48. #endif                    /* MSDOS */
  49.  
  50. #if OS2
  51. #include <time.h>
  52. #include <sys/types.h>
  53. #endif                    /* OS2 */
  54.  
  55. #if UNIX
  56. #ifdef CRAY
  57. #define word    fubar_word
  58. #include <sys/types.h>
  59. #include <sys/times.h>
  60. #undef word
  61. #else                    /* CRAY */
  62. #include <sys/types.h>
  63. #include <sys/times.h>
  64. #endif                    /* CRAY */
  65. #include SysTime
  66. #endif                    /* UNIX */
  67.  
  68. #if VMS
  69. #include <types.h>
  70. #include <time.h>
  71. struct tms {
  72.     time_t    tms_utime;    /* user time */
  73.     time_t    tms_stime;    /* system time */
  74.     time_t    tms_cutime;    /* user time, children */
  75.     time_t    tms_cstime;    /* system time, children */
  76. };
  77. #endif                    /* VMS */
  78.  
  79. /*
  80.  * End of operating-system specific code.
  81.  */
  82.  
  83. int first_time = 1;
  84.  
  85. static char *day[] = {
  86.    "Sunday", "Monday", "Tuesday", "Wednesday",
  87.    "Thursday", "Friday", "Saturday"
  88.    };
  89.  
  90. static char *month[] = {
  91.    "January", "February", "March", "April", "May", "June",
  92.    "July", "August", "September", "October", "November", "December"
  93.    };
  94.  
  95.  
  96. /*
  97.  * getitime - fill in a "struct cal_time" with information about the current
  98.  *  time and date.
  99.  */
  100. novalue getitime(ct)
  101. struct cal_time *ct;
  102.    {
  103.  
  104. /*
  105.  * The following code is operating-system dependent [@time.02]. Declarations
  106.  *  for getting time.
  107.  */
  108.  
  109. #if PORT
  110.    long time();
  111.    long xclock;
  112. Deliberate Syntax Error
  113. #endif                    /* PORT */
  114.  
  115. #if AMIGA || OS2 || UNIX || SCCX_MX
  116. #ifdef StandardLib
  117.    time_t time();
  118.    time_t xclock;
  119. #else                    /* StandardLib */
  120.    long time();
  121.    long xclock;
  122. #endif                    /* StandardLib */
  123. #endif                    /* AMIGA || OS2 || UNIX || SCCX_MX */
  124.  
  125. #if ARM || VMS
  126.    time_t xclock;
  127. #endif                    /* ARM || VMS */
  128.  
  129. #if ATARI_ST
  130.    struct tm {
  131.        short tm_year;
  132.        short tm_mon;
  133.        short tm_wday;
  134.        short tm_mday;
  135.        short tm_hour;
  136.        short tm_min;
  137.        short tm_sec;
  138.    };
  139.    long xclock;
  140. #endif                    /* ATARI_ST */
  141.  
  142. #if MACINTOSH
  143. #if LSC
  144.    unsigned long xclock;
  145.    unsigned long time();
  146. #else                    /* LSC */
  147.    time_t xclock;
  148. #endif                    /* LSC */
  149. #endif                    /* MACINTOSH */
  150.  
  151. #if MSDOS
  152. #if MICROSOFT || NT
  153.    time_t time();
  154.    long xclock;
  155. #endif                    /* MICROSOFT || NT */
  156. #if WATCOM
  157.    time_t time();
  158.    time_t xclock;
  159. #endif                    /* WATCOM */
  160. #if TURBO || INTEL_386 || BORLAND_286 || BORLAND_386
  161.    long time();
  162.    long xclock;
  163. #endif                    /* TURBO || ... */
  164. #if HIGHC_386
  165.    long time();
  166.    time_t xclock;
  167. #endif                    /* HIGHC_386 */
  168. #endif                    /* MSDOS */
  169.  
  170. #if MVS || VM
  171.    time_t xclock;
  172. #endif                    /* MVS || VM */
  173.  
  174. /*
  175.  * End of operating-system specific code.
  176.  */
  177.  
  178.    struct tm *tbuf, *localtime();
  179. /*
  180.  * The following code is operating-system dependent [@time.03]. Code for
  181.  *  getting time.
  182.  */
  183.  
  184. #if PORT
  185.    time(&xclock);
  186.    tbuf = localtime(&xclock);
  187. Deliberate Syntax Error
  188. #endif                    /* PORT */
  189.  
  190. #if AMIGA || ARM || MACINTOSH || OS2 || UNIX || VMS || MVS || VM
  191.    time(&xclock);
  192.    tbuf = localtime(&xclock);
  193. #endif                    /* AMIGA || ARM || ... */
  194.  
  195. #if MSDOS
  196. #if ZTC_386
  197.    /*
  198.     * Symantec has made a balls-up of localtime() in Zortech C++ 3.1
  199.     * so use MS-DOS calls
  200.     */
  201.    struct tm tbufa;
  202.    struct dos_date_t d;
  203.    struct dos_time_t t;
  204.  
  205.    tbuf = &tbufa;
  206.    dos_getdate(&d);
  207.    dos_gettime(&t);
  208.  
  209.    tbufa.tm_year = d.year - 1900;
  210.    tbufa.tm_mon = d.month-1;
  211.    tbufa.tm_mday = d.day;
  212.    tbufa.tm_wday = d.dayofweek;
  213.    tbufa.tm_hour = t.hour;
  214.    tbufa.tm_min = t.minute;
  215.    tbufa.tm_sec = t.second;
  216. #else                       /* ZTC_386 */
  217.    time(&xclock);
  218.    tbuf = localtime(&xclock);
  219. #endif                       /* ZTC_386 */
  220. #endif                    /* MSDOS */
  221.  
  222. #if ATARI_ST
  223.     tbuf = localtime(&xclock);
  224. #endif                    /* ATARI_ST */
  225.  
  226. /*
  227.  * End of operating-system specific code.
  228.  */
  229.  
  230.    ct->year = 1900 + tbuf->tm_year;
  231.    ct->month_no = tbuf->tm_mon+1;
  232.    ct->month_nm = month[tbuf->tm_mon];
  233.    ct->mday = tbuf->tm_mday;
  234.    ct->wday = day[tbuf->tm_wday];
  235.    ct->hour = tbuf->tm_hour;
  236.    ct->minute = tbuf->tm_min;
  237.    ct->second = tbuf->tm_sec;
  238.    return;
  239.    }
  240.  
  241. /*
  242.  * getctime - fill a buffer with the "ctime" representation of the current
  243.  *  time and date. The buffer must be at least 26 characters.
  244.  */
  245. novalue getctime(sbuf)
  246. char *sbuf;
  247.    {
  248.    struct cal_time ct;
  249.  
  250.    getitime(&ct);
  251.    sprintf(sbuf, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", ct.wday, ct.month_nm,
  252.       ct.mday, ct.hour, ct.minute, ct.second, ct.year);
  253.    return;
  254.    }
  255.  
  256. /*
  257.  * millisec - returns execution time in milliseconds. Time is measured
  258.  *  from the function's first call. The granularity of the time is
  259.  *  generally more than one millisecond and on some systems it my only
  260.  *  be accurate to the second.
  261.  */
  262. long millisec()
  263.    {
  264.  
  265. /*
  266.  * The following code is operating-system dependent [@time.04]. Declarations
  267.  *   that are system-dependent.
  268.  */
  269.  
  270. #if PORT
  271.    static long starttime;
  272.    long time();
  273. Deliberate Syntax Error
  274. #endif                    /* PORT */
  275.  
  276. #if AMIGA || ATARI_ST
  277.    static long starttime;
  278.    long time();
  279. #endif                    /* AMIGA || ATARI_ST */
  280.  
  281. #if ARM
  282.    static clock_t strtime;
  283. #endif                    /* ARM */
  284.  
  285.  
  286. #if MACINTOSH
  287.    static long starttime;
  288. #endif                    /* MACINTOSH */
  289.  
  290. #if MSDOS
  291.  
  292. #if MICROSOFT
  293.    static long starttime;
  294.    time_t time();
  295. #endif                    /* MICROSOFT */
  296.  
  297. #if WATCOM
  298.    time_t time();
  299.    static time_t starttime;
  300. #endif                    /* WATCOM */
  301.  
  302. #if ZTC_386 || NT
  303.    static long starttime;
  304. #endif                    /* ZTC_386 || NT */
  305.  
  306. #if SCCX_MX
  307.     static time_t starttime;
  308. #endif                    /* SCCX_MX */
  309.  
  310. #if TURBO || INTEL_386 || BORLAND_286 || BORLAND_386
  311.    static long starttime;
  312.    long time();
  313. #endif                    /* TURBO || INTEL_386 ... */
  314.  
  315. #if HIGHC_386
  316.    static clock_t hc_strtime;
  317. #endif                    /* HIGHC_386 */
  318.  
  319. #endif                    /* MSDOS */
  320.  
  321. #if MVS || VM || OS2
  322.    static clock_t starttime;
  323. #endif                                  /* MVS || VM */
  324.  
  325. #if UNIX || VMS
  326.    struct tms tp;
  327.    static long starttime;
  328. #endif                    /* UNIX || VMS */
  329.  
  330. /*
  331.  * End of operating-system specific code.
  332.  */
  333.  
  334.    if (first_time) {
  335.       first_time = 0;
  336.  
  337. /*
  338.  * The following code is operating-system dependent [@time.05].  Get start
  339.  *  time.
  340.  */
  341.  
  342. #if PORT
  343.       /* needs something */
  344. Deliberate Syntax Error
  345. #endif                    /* PORT */
  346.  
  347. #if AMIGA || ATARI_ST
  348.       time(&starttime);    /* note: this obtains time in various units */
  349. #endif                    /* AMIGA || ATARI_ST */
  350.  
  351. #if MSDOS
  352. #if MICROSOFT || TURBO || WATCOM || SCCX_MX
  353.       time(&starttime);    /* note: this obtains time in various units */
  354. #endif                    /* MICROSOFT || TURBO ... */
  355. #if ZTC_386
  356.       starttime = clock();
  357. #endif                    /* ZTC_386 */
  358. #if INTEL_386 || NT || BORLAND_286 || BORLAND_386
  359.       starttime = (long)clock();
  360. #endif                    /* INTEL_386 || NT ... */
  361. #if HIGHC_386
  362.       hc_strtime = clock();
  363. #endif                    /* HIGHC_386 */
  364. #endif                    /* MSDOS */
  365.  
  366. #if ARM
  367.       strtime = clock();
  368. #endif                    /* ARM */
  369.  
  370.  
  371. #if MACINTOSH
  372.       starttime = TickCount();    /* 60 ticks / second */
  373. #endif                    /* MACINTOSH */
  374.  
  375. #if MVS || VM || OS2
  376.       starttime = clock();        /* clock ticks */
  377. #endif                    /* MVS || VM || OS2 */
  378.  
  379. #if UNIX || VMS
  380.       times(&tp);
  381.       starttime = tp.tms_utime;
  382. #endif                    /* UNIX || VMS */
  383.  
  384. /*
  385.  * End of operating-system specific code.
  386.  */
  387.  
  388.       return 0L;
  389.       }
  390.    else {    /* not first time */
  391. /*
  392.  * The following code is operating-system dependent [@time.06].  Get time.
  393.  */
  394.  
  395. #if PORT
  396.    /* needs something */
  397. Deliberate Syntax Error
  398. #endif                    /* PORT */
  399.  
  400. #if AMIGA
  401.       return 1000 * (time(NULL) - starttime);
  402. #endif                    /* AMIGA */
  403.  
  404. #if MSDOS
  405.  
  406. #if HIGHC_386
  407.       return (clock() - hc_strtime) * 10L;    /* thousandths */
  408. #endif                    /* HIGHC_386 */
  409.  
  410. #if INTEL_386 || NT || BORLAND_286 || BORLAND_386
  411.       return ((long)clock() - starttime) * (1000L / CLOCKS_PER_SEC);
  412. #endif                    /* INTEL_386 || NT ... */
  413.  
  414. #if MICROSOFT || TURBO
  415.       return 1000 * (time(NULL) - starttime);
  416. #endif                    /* MICROSOFT || TURBO */
  417.  
  418. #if ZTC_386
  419.       return (1000/CLOCKS_PER_SEC) * (clock() - starttime);
  420. #endif                    /* ZTC_386 */
  421.  
  422. #if SCCX_MX
  423.       return 10 * clock();
  424. #endif                    /* SCCX_MX */
  425.  
  426. #endif                    /* MSDOS */
  427.  
  428. #if ARM
  429.       /* thousandths; assumes CLOCKS_PER-SEC is 100 */
  430.       return (clock() - strtime) * 10L;
  431. #endif                    /* ARM */
  432.  
  433. #if ATARI_ST
  434.       return (time(NULL) - starttime) / 10;
  435. #endif                    /* ATARI_ST */
  436.  
  437. #if MACINTOSH
  438.       return 1000 * ((long)(TickCount() - starttime) / (long)Hz);
  439. #endif                    /* MACINTOSH */
  440.  
  441. #if MVS || VM || OS2
  442.       return (1000.0/CLOCKS_PER_SEC) * (clock() - starttime);
  443. #endif                    /* MVS || VM */
  444.  
  445. #if UNIX || VMS
  446.       times(&tp);
  447.       return 1000 * ((tp.tms_utime - starttime) / (double)Hz);
  448. #endif                    /* UNIX || VMS */
  449.  
  450. /*
  451.  * End of operating-system specific code.
  452.  */
  453.       }
  454.    }
  455.