home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / STSDATE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  2.8 KB  |  94 lines

  1. /**
  2. *
  3. * Name        stsdate -- Return the system date in USA format
  4. *
  5. * Synopsis    ercode = stsdate(pdate,pyear,pmonth,pday);
  6. *
  7. *        int ercode      Error code is -1 if current system date
  8. *                  is not a legal date, 0 if o.k.
  9. *        char *pdate      Pointer to string in which the date is
  10. *                  returned.
  11. *        int *pyear,*pmonth,*pday  Pointers to variables in which
  12. *                  the year, month and day are returned.
  13. *
  14. * Description    This function returns the system date as integers and as
  15. *        a string containing the month and day of the week.  For
  16. *        example, 8/17/83 is returned as
  17. *
  18. *             Wednesday August 17, 1983
  19. *
  20. *        The day of week algorithm is valid only for the 20th and
  21. *        21st centuries; any date outside this range returns a
  22. *        nonzero error code and the null string.
  23. *
  24. *        The date string, pdate, must be large enough to
  25. *        accommodate the largest date string since sprintf() is
  26. *        used.  If it is not, memory will be overwritten.  pdate
  27. *        should be large enough to store a string of length 30.
  28. *
  29. * Returns    int ercode      Returned error code
  30. *        char *pdate      Date string
  31. *        int *pyear,*pmonth,*pday  The date as numeric values.
  32. *
  33. * Version    3.0 (C)Copyright Blaise Computing Inc. 1983, 1984, 1985, 1986
  34. *
  35. **/
  36.  
  37. #include <stdio.h>
  38.  
  39. #include <bstring.h>
  40.  
  41. int stsdate(pdate,pyear,pmonth,pday)
  42. char          *pdate;
  43. register int  *pyear,*pmonth;
  44. int          *pday;
  45. {
  46.  
  47.     /* The static array day_offset measures the offset into the week  */
  48.     /* for each month of the 20th century.  Notice that day_offset[0] */
  49.     /* is not a legal month.                          */
  50.  
  51.     static int day_offset[] = {0,6,2,2,5,0,3,5,1,4,6,2,4};
  52.     static char *day_name[] =
  53.        {
  54.         "Sunday","Monday","Tuesday",
  55.         "Wednesday","Thursday",
  56.         "Friday","Saturday"
  57.        };
  58.     static char *month_name[] =
  59.        {
  60.         "",                        /* Illegal Month               */
  61.         "January","February","March",
  62.         "April","May","June",
  63.         "July","August","September",
  64.         "October","November","December"
  65.        };
  66.     register int  leap_yrs;          /* Number of leap years          */
  67.     register int  day_index;
  68.  
  69.     qyretdat(pyear,pmonth,pday);      /* Get system date          */
  70.  
  71.     if (   utrange(*pyear,1900,2099)
  72.     || utrange(*pmonth,1,12)
  73.     || utrange(*pday,1,31))
  74.     {
  75.        *pdate = '\0';
  76.        return(-1);              /* Date is out of range          */
  77.     }
  78.  
  79.     /* Now count the leap years and compute the number of days (mod 7)*/
  80.     /* to get the day of the week.                      */
  81.  
  82.     leap_yrs = ((*pyear - 1900)/4) + 1;
  83.     if ((*pyear % 4 == 0) && (*pmonth <= 2))   /* Not February 29 yet */
  84.        leap_yrs--;
  85.     day_index = ((day_offset[*pmonth] + *pday + (*pyear - 1900)
  86.                                + leap_yrs) % 7);
  87.  
  88.     sprintf(pdate,"%s %s %d, %d",day_name[day_index],
  89.                  month_name[*pmonth],
  90.                  *pday,
  91.                  *pyear);
  92.     return(0);
  93. }
  94.