home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / CALSUPP.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  1KB  |  66 lines

  1. /* calsupp.c -- public domain by Ray McVay */
  2.  
  3. /*  This module provides three handy date related functions:
  4. **       dow() - Returns the day of the week for a given date
  5. **       IsLeap() - Returns 1 if a year is a leap year
  6. **       GetToday() - Returns today's date from the operating system
  7. */
  8.  
  9. #include <time.h>
  10.  
  11. /*
  12. ** Returns an integer that represents the day of the week for
  13. **  the date passed as parameters.
  14. **
  15. **   day:    day of month
  16. **   mon:    month (1-12)
  17. **   yr:     year
  18. **
  19. **  returns 0-6 where 0 == sunday
  20. */
  21.  
  22. int dow(int day, int mon, int yr)
  23. {
  24.       int dow;
  25.  
  26.       if (mon <= 2)
  27.       {
  28.             mon += 12;
  29.             yr -= 1;
  30.       }
  31.       dow = (day + mon * 2 + ((mon + 1) * 6) / 10 +
  32.             yr + yr / 4 - yr / 100 + yr / 400 + 2);
  33.       dow = dow % 7;
  34.       return ((dow ? dow : 7) - 1);
  35. }
  36.  
  37.  
  38. /*
  39. **  Returns 1 if yr is a leap year, 0 if it is not
  40. */
  41.  
  42. int IsLeap(int yr)
  43. {
  44.       if (yr % 400 == 0)  return 1;
  45.       if (yr % 100 == 0)  return 0;
  46.       if (yr % 4 == 0)    return 1;
  47.       else                return 0;
  48. }
  49.  
  50.  
  51. /*
  52. **  Returns the current day, month and year in the referenced variables
  53. */
  54.  
  55. void GetToday(int *day, int *mon, int *yr)
  56. {
  57.       struct tm   today;
  58.       time_t  ctime;
  59.  
  60.       time(&ctime);
  61.       today = *localtime(&ctime);
  62.       *day = today.tm_mday;
  63.       *mon = today.tm_mon + 1;
  64.       *yr = today.tm_year + 1900;
  65. }
  66.