home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / DAYNUM.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  133 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  DAYNUM.C - Functions to return statistics about a given date.
  5. **
  6. **  public domain by Bob Stout - uses Ray Gardner's SCALDATE.C
  7. */
  8.  
  9. #include "scaldate.h"
  10.  
  11. static long jan1date;
  12.  
  13. /*
  14. **  Determine if a given date is valid
  15. */
  16.  
  17. Boolean_T valiDate(unsigned yr, unsigned mo, unsigned day)
  18. {
  19.       unsigned int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  20.  
  21.       if (1 > mo || 12 < mo)
  22.             return False_;
  23.       if (1 > day || day > (days[mo - 1] + (2 == mo && isleap(yr))))
  24.             return False_;
  25.       else  return True_;
  26. }
  27.  
  28. /*
  29. **  Return the day of the week
  30. */
  31.  
  32. unsigned dow(unsigned yr, unsigned mo, unsigned day)
  33. {
  34.  
  35. #if (!ISO_CAL)    /* Sunday(0) -> Saturday(6) (i.e. U.S.) calendars  */
  36.       return (unsigned)(ymd_to_scalar(yr, mo, day) % 7L);
  37. #else             /* International Monday(0) -> Sunday(6) calendars  */
  38.       return (unsigned)((ymd_to_scalar(yr, mo, day) - 1L) % 7L);
  39. #endif
  40. }
  41.  
  42. /*
  43. **  Return the day of the year (1 - 365/6)
  44. */
  45.  
  46. int daynum(int year, int month, int day)
  47. {
  48.       jan1date = ymd_to_scalar(year, 1, 1);
  49.       return (int)(ymd_to_scalar(year, month, day) - jan1date + 1L);
  50. }
  51.  
  52. /*
  53. **  Return the week of the year (1 - 52, 0 - 52 if ISO)
  54. */
  55.  
  56. int weeknum(int year, int month, int day)
  57. {
  58.       int wn, j1n, dn = daynum(year, month, day);
  59.  
  60.       dn += (j1n = (int)((jan1date - (long)ISO_CAL) % 7L)) - 1;
  61.       wn = dn / 7;
  62.       if (ISO_CAL)
  63.             wn += (j1n < 4);
  64.       else  ++wn;
  65.       return wn;
  66. }
  67.  
  68. /*
  69. **  Return the phase of the moon (0 -> 7, 0 = new, 4 = full)
  70. */
  71.  
  72. char *MoonPhaseText[8] = {"new", "waxing crescent", "first quarter",
  73.       "waxing gibbous", "full", "waning gibbous", "third quarter",
  74.       "waning crescent"
  75.       };
  76.  
  77. unsigned moonphase(unsigned yr, unsigned mo, unsigned dy)
  78. {
  79.       unsigned long date = (unsigned long)ymd_to_scalar(yr, mo, dy);
  80.  
  81.       date *= 9693L;
  82.       date /= 35780L;
  83.       date -= 4L;
  84.       date %= 8L;
  85.       return (unsigned)date;
  86. }
  87.  
  88. #ifdef TEST
  89.  
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92.  
  93. void do_err(void);
  94.  
  95. main(int argc, char *argv[])
  96. {
  97.       int day, month, year;
  98.       char *days[] =
  99. #if (!ISO_CAL)
  100.             {"Sunday", "Monday", "Tuesday", "Wednesday",
  101.              "Thursday", "Friday", "Saturday"};
  102. #else
  103.             {"Monday", "Tuesday", "Wednesday", "Thursday",
  104.              "Friday", "Saturday", "Sunday"};
  105. #endif
  106.  
  107.       if (4 > argc)
  108.       {
  109.             puts("Usage: DAYNUM month day year");
  110.             return EXIT_FAILURE;
  111.       }
  112.  
  113.       month = atoi(argv[1]);
  114.       day   = atoi(argv[2]);
  115.       year  = atoi(argv[3]);
  116.       if (100 > year)
  117.             year += 1900;
  118.  
  119.       if (!valiDate(year, month, day))
  120.             printf("%d/%d/%d is invalid!\n", month, day, year);
  121.       else
  122.       {
  123.             printf("%d/%d/%d is a %s, day #%d in week %d\n", month, day, year,
  124.                   days[dow(year, month, day)], daynum(year, month, day),
  125.                   weeknum(year, month, day));
  126.             printf("The phase of the moon is %s\n",
  127.                   MoonPhaseText[moonphase(year, month, day)]);
  128.       }
  129.       return EXIT_SUCCESS;
  130. }
  131.  
  132. #endif /* TEST */
  133.