home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / XBBS7200.ZIP / XBBS7200.TAR / today / timetx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-02  |  5.1 KB  |  154 lines

  1. /*
  2.  *              Convert Time to a Readable Format.
  3.  *
  4.  * Synopsis:
  5.  *
  6.  *      char    *timetxt(buffer, hour, minute, second, daylight);
  7.  *      char    *buffer;        -- Where output goes
  8.  *      int     hour;           -- Hour,        range is 0 to 24
  9.  *      int     minute;         -- Minute,      range is -1 to 59
  10.  *      int     second;         -- Seconds,     range is -1 to 59
  11.  *      int     daylight;       -- Daylight savings time if non-zero.
  12.  *
  13.  * Note: if minute or second is less than zero, the value is not calculated.
  14.  * This distinguishes between "unknown seconds" and "exactly no seconds."
  15.  * If hour is less than zero, a null string is returned.
  16.  * Timetxt converts the time to a null-trailed string.  It returns a pointer
  17.  * to the first free byte (i.e. the null);
  18.  *
  19.  * The output follows the syntax of Robert J. Lurtsema, and includes:
  20.  *
  21.  *      In twenty-five seconds, the time will be ten minutes before noon.
  22.  *
  23.  *
  24.  * External routines called:
  25.  *
  26.  *      nbrtxt          (Number to ascii conversion)
  27.  *      copyst          (String copy routine)
  28.  */
  29.  
  30. extern char     *nbrtxt();
  31. extern char     *copyst();
  32.  
  33. char *timetxt(buffer, hour, minute, second, daylight)
  34. char    *buffer;                        /* Output buffer                */
  35. int     hour;                           /* Hours 00 - 23                */
  36. int     minute;                         /* Minutes                      */
  37. int     second;                         /* Seconds                      */
  38. int     daylight;                       /* Non-zero if savings time     */
  39. /*
  40.  * Output time of day.
  41.  */
  42. {
  43.     char            *op;            /* Output pointer               */
  44.     register int    late;           /* after hour or afternoon      */
  45.     register int    sec;        /* Seconds temp            */
  46.     char        *stuff();    /* Buffer stuffer        */
  47.  
  48.     op = buffer;                    /* Setup buffer pointer         */
  49.     if (hour < 0) {            /* If it's a dummy call,    */
  50.         *op = 0;        /* Return a null string        */
  51.         return(op);
  52.     }
  53.     if (daylight == 0101010) {      /* Secret flag                  */
  54.         op = copyst(op, "The big hand is on the ");
  55.         op = nbrtxt(op, (((minute + 2 + second/30)/5 + 11)%12)+1, 0);
  56.         op = copyst(op," and the little hand is on the ");
  57.         op = nbrtxt(op, ((hour + 11) % 12) + 1, 0);
  58.         return(copyst(op, ".  "));
  59.     }
  60.     /*
  61.      * Check if the time is more than 30 minutes past the hour.
  62.      * If so, output the time before the next hour.
  63.      */
  64.     if (minute < 0) second = (-2);  /* No minutes means no seconds  */
  65.     else if ((late = (minute > 30 || (minute == 30 && second > 0)))) {
  66.         if (second > 0) {       /* Before next hour             */
  67.             second = 60 - second;
  68.             minute += 1;    /* Diddle the minute, too       */
  69.         }
  70.         minute = 60 - minute;   /* Minutes before next hour     */
  71.         hour += 1;              /* Frobozz next hour getter     */
  72.     }
  73.     /*
  74.      * Decisions, decisions:
  75.      *    Minutes    Seconds =>
  76.      *      00      00    Exactly Noon
  77.      *      00      01    One second after noon
  78.      *      01      00    Exactly one minute after noon
  79.      *      30      00    Exactly half past noon
  80.      *      59      00    Exactly one minute before noon
  81.      *      59      59    In one second, the time will be noon
  82.      */
  83.     if (late > 0 && second > 0) {
  84.         op = stuff(op, second, 1, "In ", " second");
  85.         op = copyst(op, ", the time will be ");
  86.         sec = -2;        /* We've done seconds already    */
  87.     }
  88.     else if (daylight != -1) {
  89.         op = copyst(op, "The time is ");
  90.         sec = second;        /* Seconds still to be done    */
  91.     }
  92.     if (sec == 0) {
  93.         op = copyst(op, "exactly ");
  94.         if (minute == 30)
  95.             op = copyst(op, "half past ");
  96.         else    op = stuff(op, minute, 1, " ", " minute");
  97.     }
  98.     else {                /* Non exact or missing seconds    */
  99.         op = stuff(op, minute, 0, " ",     " minute");
  100.         if(second > 0)
  101.             op = stuff(op, sec, (sec > 0),  " and ", " second");
  102.     }
  103.     op = copyst(op, (minute < 0 || (minute == 0 && late)
  104.             || (second == 0
  105.                 && ((minute == 0 && late == 0)
  106.                     || minute == 30))) ? " "
  107.         : (late) ? " before " : " after ");
  108.     /*
  109.      * Hours are not quite so bad
  110.      */
  111.     if (hour == 0 || hour == 24)
  112.         op = copyst(op, "midnight");
  113.     else if (hour == 12)
  114.         op = copyst(op, "noon");
  115.     else {
  116.         if (late = (hour > 12))
  117.             hour = hour - 12;
  118.         op = nbrtxt(op, hour, 0);
  119.         op = copyst(op, (late) ? " PM" : " AM");
  120.     }
  121.     if (daylight != -1) 
  122.         op = copyst(op, (daylight)
  123.         ? ", Pacific Daylight Time.  "
  124.         : ", Pacific Standard Time.  ");
  125.     return(op);
  126. }
  127.  
  128. static char *
  129. stuff(buffer, value, flag, leading, trailing)
  130. char    *buffer;                        /* Output goes here             */
  131. int     value;                          /* The value to print if > 0    */
  132. int     flag;                           /* flag is set to print leading */
  133. char    *leading;                       /* preceeded by ...             */
  134. char    *trailing;                      /* and followed by ...          */
  135. /*
  136.  * If value <= zero, output nothing. Else, output "leading" value "trailing".
  137.  * Note: leading is output only if flag is set.
  138.  * If value is not one, output an "s", too.
  139.  */
  140. {
  141.     register char   *op;            /* Output pointer               */
  142.  
  143.     op = buffer;                    /* Setup buffer pointer         */
  144.     if (value > 0) {
  145.         if (flag)
  146.             op = copyst(op, leading);
  147.         op = nbrtxt(op, value, 0);
  148.         op = copyst(op, trailing);
  149.         if (value != 1)
  150.             op = copyst(op, "s");
  151.     }
  152.     return(op);
  153. }
  154.