Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

//**************************************

//     

//INCLUDE files for :Determine the phase

//     of the moon for any given date

//**************************************

//     

/* +++Date last modified: 05-Jul-1997 */

/*

** Header for SNIPPETS date & time function, excluding scalar dates

*/

#ifndef DATETIME__H

#define DATETIME__H

#include "sniptype.h"

/*

** Julian day number functions from JDN_L.C

*/

long ymd_to_jdnl(int y, int m, int d, int julian);

void jdnl_to_ymd(long jdn, int *yy, int *mm, int *dd, int julian);

/*

** Add times from ADDTIME.C

*/

int add_time(unsigned basehrs, unsigned basemins, unsigned basesecs,

int spanhrs, int spanmins, int spansecs,

unsigned *hrs, unsigned *mins, unsigned *secs, int *days);

/*

** Determine the date of Easter for any given year

*/

void easter(int year, int *easter_month, int *easter_day);

/*

** Moon phase from MOON_AGE.C

*/

int moon_age(int month, int day, int year);

/*

** Date parser from PARSDATE.C

*/

typedef enum {USA, ISO, EUROPE} Syntax_T;

Boolean_T parse_date(const char *str, unsigned *year, unsigned *month,

unsigned *day, Syntax_T syntax);

/*

** Time parser from PARSTIME.C

*/

Boolean_T parse_time(const char *str, unsigned *hours, unsigned *mins,

unsigned *secs);

#endif /* DATETIME__H */

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

/* +++Date last modified: 05-Jul-1997 */

/* PD by Michelangelo Jones, 1:1/124. */

/*

** Returns 0 for new moon, 15 for full moon,

** 29 for the day before new, and so forth.

*/

/*

** this routine sometimes gets "off" by a few days,

** but is self-correcting.

*/

#include "datetime.h"

int moon_age(int month, int day, int year)

    {

    static short int ages[] =

        {18, 0, 11, 22, 3, 14, 25, 6, 17,

        28, 9, 20, 1, 12, 23, 4, 15, 26, 7};

        static short int offsets[] =

            {-1, 1, 0, 1, 2, 3, 4, 5, 7, 7, 9, 9};

            if (day == 31)

            day = 1;

            return ((ages[(year + 1) % 19] + ((day + offsets[month-1]) % 30) +

            (year < 1900)) % 30);

        }

        #ifdef TEST

        #include 

        #include 

            static char *description[] = {

            "new", /* totally dark */

            "waxing crescent", /* increasing to full & quarter light*/

            "in its first quarter", /* increasing to full & half light */

            "waxing gibbous",/* increasing to full & > than half */

            "full", /* fully lighted*/

            "waning gibbous",/* decreasing from full & > than half*/

            "in its last quarter", /* decreasing from full & half light*/

            "waning crescent"/* decreasing from full & quarter light */

        };

        static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",

        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

        int main(int argc, char *argv[])

            {

            int month, day, year, phase;

            if (4 > argc)

                {

                puts("Usage: MOON_AGE month day year");

                return EXIT_FAILURE;

            }

            month = atoi(argv[1]);

            day= atoi(argv[2]);

            year = atoi(argv[3]);

            if (100 > year)

            year += 1900;

            printf("moon_age(%d, %d, %d) returned %d\n", month, day, year,

            phase = moon_age(month, day, year));

            printf("Moon phase on %d %s %d is %s\n", day, months[month - 1], year,

            description[(int)((phase + 2) * 16L / 59L)]);

            return EXIT_SUCCESS;

        }

        #endif /* TEST */