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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* PD by Michelangelo Jones, 1:1/124. */
  4.  
  5. /*
  6. **  Returns 0 for new moon, 15 for full moon,
  7. **  29 for the day before new, and so forth.
  8. */
  9.  
  10. /*
  11. **  This routine sometimes gets "off" by a few days,
  12. **  but is self-correcting.
  13. */
  14.  
  15. #include "datetime.h"
  16.  
  17. int moon_age(int month, int day, int year)
  18. {
  19.       static short int ages[] =
  20.             {18, 0, 11, 22, 3, 14, 25, 6, 17,
  21.              28, 9, 20, 1, 12, 23, 4, 15, 26, 7};
  22.       static short int offsets[] =
  23.             {-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9};
  24.  
  25.       if (day == 31)
  26.             day = 1;
  27.       return ((ages[(year + 1) % 19] + ((day + offsets[month-1]) % 30) +
  28.             (year < 1900)) % 30);
  29. }
  30.  
  31. #ifdef TEST
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35.  
  36. static char *description[] = {
  37.       "new",                  /* totally dark                         */
  38.       "waxing crescent",      /* increasing to full & quarter light   */
  39.       "in its first quarter", /* increasing to full & half light      */
  40.       "waxing gibbous",       /* increasing to full & > than half     */
  41.       "full",                 /* fully lighted                        */
  42.       "waning gibbous",       /* decreasing from full & > than half   */
  43.       "in its last quarter",  /* decreasing from full & half light    */
  44.       "waning crescent"       /* decreasing from full & quarter light */
  45.       };
  46.  
  47. static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  48.                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  49.  
  50. int main(int argc, char *argv[])
  51. {
  52.       int month, day, year, phase;
  53.  
  54.       if (4 > argc)
  55.       {
  56.             puts("Usage: MOON_AGE month day year");
  57.             return EXIT_FAILURE;
  58.       }
  59.       month = atoi(argv[1]);
  60.       day   = atoi(argv[2]);
  61.       year  = atoi(argv[3]);
  62.       if (100 > year)
  63.             year += 1900;
  64.       printf("moon_age(%d, %d, %d) returned %d\n", month, day, year,
  65.             phase = moon_age(month, day, year));
  66.       printf("Moon phase on %d %s %d is %s\n", day, months[month - 1], year,
  67.             description[(int)((phase + 2) * 16L / 59L)]);
  68.       return EXIT_SUCCESS;
  69. }
  70.  
  71. #endif /* TEST */
  72.