xref: /freebsd/usr.bin/pom/pom.c (revision 6780e684d49034610f82bea5d3bfb04d42e91628)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software posted to USENET.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38 
39 #ifndef lint
40 static const char sccsid[] = "@(#)pom.c       8.1 (Berkeley) 5/31/93";
41 #endif /* not lint */
42 #endif
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 /*
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.
51  *
52  * -- Keith E. Brandt  VIII 1984
53  *
54  */
55 
56 #include <sys/capsicum.h>
57 #include <capsicum_helpers.h>
58 
59 #include <err.h>
60 #include <errno.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <math.h>
64 #include <string.h>
65 #include <sysexits.h>
66 #include <time.h>
67 #include <unistd.h>
68 
69 #ifndef	PI
70 #define	PI	  3.14159265358979323846
71 #endif
72 #define	EPOCH	  85
73 #define	EPSILONg  279.611371	/* solar ecliptic long at EPOCH */
74 #define	RHOg	  282.680403	/* solar ecliptic long of perigee at EPOCH */
75 #define	ECCEN	  0.01671542	/* solar orbit eccentricity */
76 #define	lzero	  18.251907	/* lunar mean long at EPOCH */
77 #define	Pzero	  192.917585	/* lunar mean long of perigee at EPOCH */
78 #define	Nzero	  55.204723	/* lunar mean long of node at EPOCH */
79 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
80 
81 static void	adj360(double *);
82 static double	dtor(double);
83 static double	potm(double);
84 static void	usage(char *progname);
85 
86 int
87 main(int argc, char **argv)
88 {
89 	time_t tt;
90 	struct tm GMT, tmd;
91 	double days, today, tomorrow;
92 	int ch, cnt, pflag = 0;
93 	char *odate = NULL, *otime = NULL;
94 	char *progname = argv[0];
95 
96 	if (caph_limit_stdio() < 0)
97 		err(1, "unable to limit capabitilities for stdio");
98 
99 	caph_cache_catpages();
100 	if (cap_enter() < 0 && errno != ENOSYS)
101 		err(1, "unable to enter capability mode");
102 
103 	while ((ch = getopt(argc, argv, "d:pt:")) != -1)
104 		switch (ch) {
105 		case 'd':
106 			odate = optarg;
107 			break;
108 		case 'p':
109 			pflag = 1;
110 			break;
111 		case 't':
112 			otime = optarg;
113 			break;
114 		default:
115 			usage(progname);
116 		}
117 
118         argc -= optind;
119 	argv += optind;
120 
121 	if (argc)
122 		usage(progname);
123 
124 	/* Adjust based on users preferences */
125 	time(&tt);
126 	if (otime != NULL || odate != NULL) {
127 		/* Save today in case -d isn't specified */
128 		localtime_r(&tt, &tmd);
129 
130 		if (odate != NULL) {
131 			tmd.tm_year = strtol(odate, NULL, 10) - 1900;
132 			tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1;
133 			tmd.tm_mday = strtol(odate + 8, NULL, 10);
134 			/* Use midnight as the middle of the night */
135 			tmd.tm_hour = 0;
136 			tmd.tm_min = 0;
137 			tmd.tm_sec = 0;
138 		}
139 		if (otime != NULL) {
140 			tmd.tm_hour = strtol(otime, NULL, 10);
141 			tmd.tm_min = strtol(otime + 3, NULL, 10);
142 			tmd.tm_sec = strtol(otime + 6, NULL, 10);
143 		}
144 		tt = mktime(&tmd);
145 	}
146 
147 	gmtime_r(&tt, &GMT);
148 	days = (GMT.tm_yday + 1) + ((GMT.tm_hour +
149 	    (GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0);
150 	for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
151 		days += isleap(1900 + cnt) ? 366 : 365;
152 	today = potm(days) + .5;
153 	if (pflag) {
154 		(void)printf("%1.0f\n", today);
155 		return (0);
156 	}
157 	(void)printf("The Moon is ");
158 	if ((int)today == 100)
159 		(void)printf("Full\n");
160 	else if (!(int)today)
161 		(void)printf("New\n");
162 	else {
163 		tomorrow = potm(days + 1);
164 		if ((int)today == 50)
165 			(void)printf("%s\n", tomorrow > today ?
166 			    "at the First Quarter" : "at the Last Quarter");
167 		else {
168 			(void)printf("%s ", tomorrow > today ?
169 			    "Waxing" : "Waning");
170 			if (today > 50)
171 				(void)printf("Gibbous (%1.0f%% of Full)\n",
172 				    today);
173 			else if (today < 50)
174 				(void)printf("Crescent (%1.0f%% of Full)\n",
175 				    today);
176 		}
177 	}
178 
179 	return 0;
180 }
181 
182 /*
183  * potm --
184  *	return phase of the moon
185  */
186 static double
187 potm(double days)
188 {
189 	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
190 	double A4, lprime, V, ldprime, D, Nm;
191 
192 	N = 360 * days / 365.2422;				/* sec 42 #3 */
193 	adj360(&N);
194 	Msol = N + EPSILONg - RHOg;				/* sec 42 #4 */
195 	adj360(&Msol);
196 	Ec = 360 / PI * ECCEN * sin(dtor(Msol));		/* sec 42 #5 */
197 	LambdaSol = N + Ec + EPSILONg;				/* sec 42 #6 */
198 	adj360(&LambdaSol);
199 	l = 13.1763966 * days + lzero;				/* sec 61 #4 */
200 	adj360(&l);
201 	Mm = l - (0.1114041 * days) - Pzero;			/* sec 61 #5 */
202 	adj360(&Mm);
203 	Nm = Nzero - (0.0529539 * days);			/* sec 61 #6 */
204 	adj360(&Nm);
205 	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 61 #7 */
206 	Ac = 0.1858 * sin(dtor(Msol));				/* sec 61 #8 */
207 	A3 = 0.37 * sin(dtor(Msol));
208 	Mmprime = Mm + Ev - Ac - A3;				/* sec 61 #9 */
209 	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 61 #10 */
210 	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 61 #11 */
211 	lprime = l + Ev + Ec - Ac + A4;				/* sec 61 #12 */
212 	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 61 #13 */
213 	ldprime = lprime + V;					/* sec 61 #14 */
214 	D = ldprime - LambdaSol;				/* sec 63 #2 */
215 	return(50 * (1 - cos(dtor(D))));			/* sec 63 #3 */
216 }
217 
218 /*
219  * dtor --
220  *	convert degrees to radians
221  */
222 static double
223 dtor(double deg)
224 {
225 
226 	return(deg * PI / 180);
227 }
228 
229 /*
230  * adj360 --
231  *	adjust value so 0 <= deg <= 360
232  */
233 static void
234 adj360(double *deg)
235 {
236 
237 	for (;;)
238 		if (*deg < 0)
239 			*deg += 360;
240 		else if (*deg > 360)
241 			*deg -= 360;
242 		else
243 			break;
244 }
245 
246 static void
247 usage(char *progname)
248 {
249 
250 	fprintf(stderr, "Usage: %s [-p] [-d yyyy.mm.dd] [-t hh:mm:ss]\n",
251 	    progname);
252 	exit(EX_USAGE);
253 }
254