2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software posted to USENET.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
44 static const char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
51 * Phase of the Moon. Calculates the current phase of the moon.
52 * Based on routines from `Practical Astronomy with Your Calculator',
53 * by Duffett-Smith. Comments give the section from the book that
54 * particular piece of code was adapted from.
56 * -- Keith E. Brandt VIII 1984
69 #define PI 3.14159265358979323846
72 #define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */
73 #define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */
74 #define ECCEN 0.01671542 /* solar orbit eccentricity */
75 #define lzero 18.251907 /* lunar mean long at EPOCH */
76 #define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */
77 #define Nzero 55.204723 /* lunar mean long of node at EPOCH */
78 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
80 static void adj360(double *);
81 static double dtor(double);
82 static double potm(double);
83 static void usage(char *progname);
86 main(int argc, char **argv)
90 double days, today, tomorrow;
91 int ch, cnt, pflag = 0;
92 char *odate = NULL, *otime = NULL;
94 while ((ch = getopt(argc, argv, "d:pt:")) != -1)
115 /* Adjust based on users preferences */
117 if (otime != NULL || odate != NULL) {
118 /* Save today in case -d isn't specified */
119 localtime_r(&tt, &tmd);
122 tmd.tm_year = strtol(odate, NULL, 10) - 1900;
123 tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1;
124 tmd.tm_mday = strtol(odate + 8, NULL, 10);
125 /* Use midnight as the middle of the night */
131 tmd.tm_hour = strtol(otime, NULL, 10);
132 tmd.tm_min = strtol(otime + 3, NULL, 10);
133 tmd.tm_sec = strtol(otime + 6, NULL, 10);
139 days = (GMT.tm_yday + 1) + ((GMT.tm_hour +
140 (GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0);
141 for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
142 days += isleap(1900 + cnt) ? 366 : 365;
143 today = potm(days) + .5;
145 (void)printf("%1.0f\n", today);
148 (void)printf("The Moon is ");
149 if ((int)today == 100)
150 (void)printf("Full\n");
151 else if (!(int)today)
152 (void)printf("New\n");
154 tomorrow = potm(days + 1);
155 if ((int)today == 50)
156 (void)printf("%s\n", tomorrow > today ?
157 "at the First Quarter" : "at the Last Quarter");
159 (void)printf("%s ", tomorrow > today ?
160 "Waxing" : "Waning");
162 (void)printf("Gibbous (%1.0f%% of Full)\n",
165 (void)printf("Crescent (%1.0f%% of Full)\n",
175 * return phase of the moon
180 double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
181 double A4, lprime, V, ldprime, D, Nm;
183 N = 360 * days / 365.2422; /* sec 42 #3 */
185 Msol = N + EPSILONg - RHOg; /* sec 42 #4 */
187 Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */
188 LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */
190 l = 13.1763966 * days + lzero; /* sec 61 #4 */
192 Mm = l - (0.1114041 * days) - Pzero; /* sec 61 #5 */
194 Nm = Nzero - (0.0529539 * days); /* sec 61 #6 */
196 Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */
197 Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */
198 A3 = 0.37 * sin(dtor(Msol));
199 Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */
200 Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */
201 A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */
202 lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */
203 V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */
204 ldprime = lprime + V; /* sec 61 #14 */
205 D = ldprime - LambdaSol; /* sec 63 #2 */
206 return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */
211 * convert degrees to radians
217 return(deg * PI / 180);
222 * adjust value so 0 <= deg <= 360
238 usage(char *progname)
241 fprintf(stderr, "Usage: %s [-p] [-d yyyy.mm.dd] [-t hh:mm:ss]\n",