home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / pac / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.3 KB  |  178 lines

  1. /* time.c */
  2. /**********************************************************************
  3. *    File Name     : time.c
  4. *    Author        : Istvan Mohos, 1987
  5. ***********************************************************************/
  6.  
  7.  
  8. #ifdef pyr
  9. #include <sys/time.h>
  10. #else
  11. #include <time.h>
  12. #endif
  13.  
  14. #ifndef LOCALTEST
  15. #include "defs.h"
  16. #else
  17. unsigned alarm();
  18. int cdate();        /* signal has to know */
  19. #include <stdio.h>
  20. #include <signal.h>
  21. char *Thisyear;
  22. main()
  23. {
  24.     year();
  25.     printf("%s<<\n", Thisyear);
  26.     printf("%d", whichmonth("MAY"));
  27.     cdate();
  28.     for (;;);
  29. }
  30. #endif
  31.  
  32. #ifndef BADSIG
  33. #define BADSIG        (int (*)())-1
  34. #endif
  35.  
  36. cdate()
  37. {
  38.     long clock;
  39.     int hour;
  40.     register char *d;
  41.     char ptr[30];
  42.     int savy, savx, se;
  43.     static char *fid = "cdate";
  44.  
  45.     _TR
  46.     clock = time((long *) 0);    /* seconds since 1970 */
  47.     d = ctime(&clock);
  48.     strcpy(ptr, d);
  49.  
  50.     d = ptr + 8;
  51.     if (*d == ' ') {
  52.         *d = *(d+1);
  53.         *++d = ' ';
  54.     }
  55.     d = ptr + 11;
  56.     if (*d == '0')
  57.         *d = ' ';
  58.     if ((hour = atoi(d)) > 11) {    /* PM */
  59.         if (hour > 12)
  60.             sprintf(d, "%2d", hour -12);
  61.         d+=2;
  62.         *d = ':';
  63.         d+=3;
  64.         *d++ = '.';
  65.     }
  66.     else {
  67.         d = ptr + 16;
  68.         *d++ = ' ';
  69.     }
  70.     *d = '\0';  /* delete :seconds, year, newline */
  71.  
  72. #ifdef LOCALTEST
  73.     printf("    01234567890123456789012\n>>>>%s<<<<\n", ptr);
  74.     alarm(10);
  75.     if (signal(SIGALRM, cdate) == BADSIG)
  76.          exit(1);
  77. #else
  78.     getyx(stdscr, savy, savx);
  79.  
  80. #ifdef sun
  81.     se = !((stdscr->_y[stdscr->_cury][stdscr->_curx] & 128));
  82. #else
  83.     se = (stdscr->_y[stdscr->_cury][stdscr->_curx] < 128);
  84. #endif
  85.     if (se)
  86.         standout();
  87.     mvaddstr(TOP,  CONVLEFT, ptr);
  88.     if (se)
  89.         standend();
  90.     move(savy, savx);
  91.     refresh();
  92.     alarm(10);
  93.     if (signal(SIGALRM, cdate) == BADSIG)
  94.          fatal("clock signal");
  95.     TR_
  96. }
  97.  
  98. clockoff()
  99. {
  100.     static char *fid = "clockoff";
  101.  
  102.     _TR
  103.     alarm(0);
  104.     if (signal(SIGALRM, SIG_DFL) == BADSIG)
  105.          fatal("clock signal at off");
  106.     TR_
  107. }
  108.  
  109. pfresh()
  110. {
  111.     static char *fid = "pfresh";
  112.     _TR
  113.     if (Clockstat == DISA)
  114.         refresh();
  115.     else {
  116.         clockoff();
  117.         refresh();
  118.         cdate();
  119.     }
  120. #endif
  121.  
  122.     TR_
  123. }
  124.  
  125. year()
  126. {
  127.     long clock;
  128.     static char ptr[30];
  129.     static char *fid = "year";
  130.  
  131.     _TR
  132.     clock = time((long *) 0);
  133.     strcpy(ptr, ctime(&clock));
  134.  
  135.     Thisyear = ptr + 20;
  136.     *(ptr + 24) = '\0'; /* kill newline */
  137.     TR_
  138. }
  139.  
  140. whichmonth(ptr)
  141. char *ptr;
  142. {
  143.     register int hv = 0;
  144.     register char *hp;
  145.  
  146.     static char hashbuf[] = {
  147.       12, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 8, 0, 0,
  148.        3, 0, 0, 4, 0, 0,10, 5, 9, 0, 0, 7, 0, 6, 0, 0, 0, 0, 0,11, };
  149.     static char *fid = "whichmonth";
  150.  
  151.     _TR
  152.     hp = ptr;
  153.     if (hp == ZERO) {
  154.         TR_
  155.         return(0);
  156.     }
  157.     if (isalpha(*hp)) {
  158.         hv += (*hp++ & 95);
  159.         if (*hp) {
  160.             hv += (*hp++ & 95);
  161.             if (*hp)
  162.                 hv += (*hp & 95);
  163.         }
  164.         hv -= 204;
  165.         if (hv < 0 || hv > 39) {
  166.             TR_
  167.             return(0);
  168.     }
  169.         return(hashbuf[hv]);
  170.     }
  171.     if ((hv = atoi(hp)) > 12 || hv < 1) {
  172.         TR_
  173.         return(0);
  174.     }
  175.     TR_
  176.     return(hv);
  177. }
  178.