Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site telesoft.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!ittatc!dcdwest!sdcsvax!telesoft!dar From: dar@telesoft.UUCP (David Reisner @favorite) Newsgroups: net.astro Subject: Re: Phase of Moon from Unix time Message-ID: <318@telesoft.UUCP> Date: Wed, 5-Mar-86 17:58:19 EST Article-I.D.: telesoft.318 Posted: Wed Mar 5 17:58:19 1986 Date-Received: Sat, 8-Mar-86 05:03:10 EST References: <75@escher.UUCP> <82500002@convex> Organization: TeleSoft, SanDiego CA Lines: 226 > > I am looking for a routine that will return the phase of > > the moon (in floating, a 0-28 integer, or whatever) ... This was in net.sources sometime in the past. It had some bugs in it, which I have fixed (someone's C compiler does not perform the correct implicit conversions in expression evaluation). It does a little more than just return the phase... -David sdcsvax!telesoft!dar /* The phase of the moon, for your safety and convenience. ** (compile cc pom.c -lm hjs) ** ** Stolen from ArchMach & converted from PL/I by Brian Hess. ** Extensively cleaned up by Rich $alz. ** ** If you can figure out how this program works, then YOU deserve ** to be working on it, sucker! Here's a hint: The epoch is 13:23, ** 10/1/78. */ /* Change Log 25jun85 dar fixed day/hour comma printing; calculate Fraction w/dbls */ #include #include #include /* Globals. */ long Day; long Hour; long Minute; double Fraction; /*-dar 0.0=full, 0.25=last qrtr, 0.5=new, */ /*-dar 0.75=first qrtr, 1.0=full */ char *Moon[] = { "new", "first quarter of the", "full", "last quarter of the" }; /* Linked in later. */ char *rindex(); time_t time(); struct tm *localtime(); #define LINES 24 #define WIDTH 80 #define CENTER ((WIDTH - 2 * LINES) / 2) #define BRIGHT '@' #define LEDGE '(' #define REDGE ')' #define FULL 0.5 #define TwoPi (2 * 3.14159) #define ZERO 0.03 #define Plural(X) if (X != 1) printf("s"); /*!*/ long Calculate() { register struct tm *tm; register long Length; register long Phase; register long DeltaH; register long Delta; register long offset; time_t tick; long julian; long year; long hour; long minute; /*-dar Assume that the following code computes the current year, hour, and minute. */ tick = time((time_t *)0); tm = localtime(&tick); julian = tm->tm_yday + 1; year = tm->tm_year - 78; hour = tm->tm_hour; minute = tm->tm_min; /*-dar Assume that Length is the length of a lunar cycle (units?), 0 <= Phase <= Length is some portion of the cycle Length and is computed by position in epoch (Delta) mod cycle Length. */ Length = (double)2551 / 60 * 1000 + (double)443 / 60; offset = ((year * 365L + julian) * 24L + hour) * 60L + minute; Delta = offset - (273L * 24L + 13L) * 60L + 23L; Phase = Delta - (Delta / Length) * Length; /*-dar Phase / Length should be between 0 and 1, and should be the desired Fraction of the moon to display. C defines = / as an integer division, a conversion (of the result of the division) to float, and an assignment. We need float division to get a non-zero result, as desired. */ Fraction = (double)Phase / (double)Length; Length = Length / 4; Phase = Phase / Length; Delta = Delta - (Delta / Length) * Length; DeltaH = Delta / 60; Minute = Delta - DeltaH * 60; Day = DeltaH / 24; Hour = DeltaH - (Day * 24); /*printf("recomputed Fraction would be %lf\n", Phase / Length ); <<<< -dar*/ return(Phase); } /*!*/ int CharPos(x) double x; { register int i; i = x * LINES + 0.5; if ((i += LINES + CENTER) < 1) i = 1; return(i); } void Draw() { register char *p; register int i; register int end; register double y; register double cht; register double squisher; register double horizon; register double terminator; char Buffer[256]; /* Clear screen? */ if (Fraction < FULL) squisher = cos(TwoPi * Fraction); else squisher = cos(TwoPi * (Fraction - FULL)); cht = (double)2.0 / (LINES - 6.0); for (y = 0.93; y > -1.0; y -= cht) { for (i = sizeof Buffer, p = Buffer; --i >= 0; ) *p++ = ' '; horizon = sqrt(1.0 - y * y); Buffer[ CharPos(-horizon)] = LEDGE; Buffer[i = CharPos( horizon)] = REDGE; Buffer[++i] = '\0'; terminator = horizon * squisher; if (Fraction > ZERO && Fraction < (1.0 - ZERO)) { if (Fraction < FULL) { i = CharPos( terminator); end = CharPos( horizon); } else { i = CharPos(-horizon); end = CharPos( terminator); } while (i <= end) Buffer[i++] = BRIGHT; } printf(" %s\n", Buffer); } } /*!*/ main(ac) int ac; { register long Phase; Phase = Calculate(); if (ac == 1) Draw(); printf("\nIt is "); if (Day == 0 && Hour == 0 && Minute == 0) printf("exactly"); else { if (Day) { printf("%ld day", Day); Plural(Day); if (Hour | Minute) /*-dar was Hour | Day */ printf(", "); } if (Hour) { printf("%ld hour", Hour); Plural(Hour); if (Minute) printf(", "); } if (Minute) { printf("%ld minute", Minute); Plural(Minute); } printf(" since"); } printf(" the %s moon.\n", Moon[Phase]); exit(0); }