home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / XBBS7200.ZIP / XBBS7200.TAR / today / datetx.c next >
Encoding:
C/C++ Source or Header  |  1985-11-14  |  2.7 KB  |  99 lines

  1. /*
  2.  *              Convert Date to Readable Format.
  3.  *
  4.  * Synopsis:
  5.  *
  6.  *      char    *datetxt(buffer, year, month, day);
  7.  *      char    *buffer;        -- Output string goes here
  8.  *      int     year;           -- Year,        1979 = 1979
  9.  *      int     month;          -- Month,       January = 1
  10.  *      int     day;            -- Day,         1 = 1
  11.  *
  12.  * The readable date will be written into the outpub buffer, terminated by
  13.  * a null byte.  datetxt returns a pointer to the null byte.
  14.  *
  15.  * External routines called:
  16.  *
  17.  *      nbrtxt          (Number to ascii conversion)
  18.  *      copyst          (String copy routine)
  19.  */
  20.  
  21. extern  char    *nbrtxt();
  22. extern  char    *copyst();
  23. extern  char    *datetxt();
  24.  
  25. static char *daynames[] = {
  26.     "Sunday",                       /* Sunday is day zero           */
  27.     "Monday",
  28.     "Tuesday",
  29.     "Wednesday",
  30.     "Thursday",
  31.     "Friday",
  32.     "Saturday",
  33. };
  34.  
  35. static char *monthnames[] = {
  36.     "?Nomember?",                   /* Illegal month                */
  37.     "January",
  38.     "February",
  39.     "March",
  40.     "April",
  41.     "May",
  42.     "June",
  43.     "July",
  44.     "August",
  45.     "September",
  46.     "October",
  47.     "November",
  48.     "December",
  49. };
  50.  
  51. char *datetxt(buffer, year, month, day)
  52. char    *buffer;                        /* Output goes here             */
  53. int     year;                           /* Year, 1979 = 1979            */
  54. int     month;                          /* Month of year, Jan = 1       */
  55. int     day;                            /* Day in the month 1 = 1       */
  56. /*
  57.  * Output the date in readable format:
  58.  *      Tuesday, the third of October
  59.  */
  60. {
  61.     register char   *op;                    /* Output pointer       */
  62.  
  63.     op = buffer;                            /* Setup output pointer */
  64.     op = copyst(op, daynames[dayofweek(year, month, day)]);
  65.     op = copyst(op, ", the ");
  66.     op = nbrtxt(op, day, 1);
  67.     op = copyst(op, " day of ");
  68.     op = copyst(op, monthnames[(month < 0 || month > 12) ? 0 : month]);
  69.     op = copyst(op, ", ");
  70.     if (year < 1000 || year >= 2000)
  71.         return(nbrtxt(op, year, 0));
  72.     else {
  73.         op = nbrtxt(op, year/100, 0);
  74.         op = copyst(op, " ");
  75.         if ((year = year % 100) == 0)
  76.             return(copyst(op, "hundred"));
  77.         else
  78.             return(nbrtxt(op, year, 0));
  79.     }
  80. }
  81.  
  82. dayofweek(year, month, day)
  83. int     year;                                   /* Year, 1978 = 1978    */
  84. int     month;                                  /* Month, January = 1   */
  85. int     day;                                    /* Day of month, 1 = 1  */
  86. /*
  87.  * Return the day of the week on which this date falls: Sunday = 0.
  88.  * Note, this routine is valid only for the Gregorian calender.
  89.  */
  90. {
  91.     register int yearfactor;
  92.  
  93.     yearfactor = year + (month - 14)/12;
  94.     return (( (13 * (month + 10 - (month + 10)/13*12) - 1)/5
  95.         + day + 77 + 5 * (yearfactor % 100)/4
  96.         + yearfactor / 400
  97.         - yearfactor / 100 * 2) % 7);
  98. }
  99.