License Public Domain
Lines 57
Keywords
fuzzy descriptions (1) moon (1) moon phase (1)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

Moon Phases by Date Atom Feed 0

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

Determine the phase of the moon, either numerically or descriptively.

Define TEST when compiling to compile in a test main() function.