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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static const char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * Phase of the Moon. Calculates the current phase of the moon.
48 * Based on routines from `Practical Astronomy with Your Calculator',
49 * by Duffett-Smith. Comments give the section from the book that
50 * particular piece of code was adapted from.
52 * -- Keith E. Brandt VIII 1984
65 #define PI 3.14159265358979323846
68 #define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */
69 #define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */
70 #define ECCEN 0.01671542 /* solar orbit eccentricity */
71 #define lzero 18.251907 /* lunar mean long at EPOCH */
72 #define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */
73 #define Nzero 55.204723 /* lunar mean long of node at EPOCH */
74 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
76 static void adj360(double *);
77 static double dtor(double);
78 static double potm(double);
79 static void usage(char *progname);
82 main(int argc, char **argv)
86 double days, today, tomorrow;
87 int ch, cnt, pflag = 0;
88 char *odate = NULL, *otime = NULL;
89 char *progname = argv[0];
91 while ((ch = getopt(argc, argv, "d:pt:")) != -1)
112 /* Adjust based on users preferences */
114 if (otime != NULL || odate != NULL) {
115 /* Save today in case -d isn't specified */
116 localtime_r(&tt, &tmd);
119 tmd.tm_year = strtol(odate, NULL, 10) - 1900;
120 tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1;
121 tmd.tm_mday = strtol(odate + 8, NULL, 10);
122 /* Use midnight as the middle of the night */
128 tmd.tm_hour = strtol(otime, NULL, 10);
129 tmd.tm_min = strtol(otime + 3, NULL, 10);
130 tmd.tm_sec = strtol(otime + 6, NULL, 10);
136 days = (GMT.tm_yday + 1) + ((GMT.tm_hour +
137 (GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0);
138 for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
139 days += isleap(1900 + cnt) ? 366 : 365;
140 today = potm(days) + .5;
142 (void)printf("%1.0f\n", today);
145 (void)printf("The Moon is ");
146 if ((int)today == 100)
147 (void)printf("Full\n");
148 else if (!(int)today)
149 (void)printf("New\n");
151 tomorrow = potm(days + 1);
152 if ((int)today == 50)
153 (void)printf("%s\n", tomorrow > today ?
154 "at the First Quarter" : "at the Last Quarter");
156 (void)printf("%s ", tomorrow > today ?
157 "Waxing" : "Waning");
159 (void)printf("Gibbous (%1.0f%% of Full)\n",
162 (void)printf("Crescent (%1.0f%% of Full)\n",
172 * return phase of the moon
177 double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
178 double A4, lprime, V, ldprime, D, Nm;
180 N = 360 * days / 365.2422; /* sec 42 #3 */
182 Msol = N + EPSILONg - RHOg; /* sec 42 #4 */
184 Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */
185 LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */
187 l = 13.1763966 * days + lzero; /* sec 61 #4 */
189 Mm = l - (0.1114041 * days) - Pzero; /* sec 61 #5 */
191 Nm = Nzero - (0.0529539 * days); /* sec 61 #6 */
193 Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */
194 Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */
195 A3 = 0.37 * sin(dtor(Msol));
196 Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */
197 Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */
198 A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */
199 lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */
200 V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */
201 ldprime = lprime + V; /* sec 61 #14 */
202 D = ldprime - LambdaSol; /* sec 63 #2 */
203 return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */
208 * convert degrees to radians
214 return(deg * PI / 180);
219 * adjust value so 0 <= deg <= 360
235 usage(char *progname)
238 fprintf(stderr, "Usage: %s [-p] [-d yyyy.mm.dd] [-t hh:mm:ss]\n",