xref: /netbsd-src/games/pom/pom.c (revision 1182a44c59cae4d586117d55eca24b4b8b173211)
1*1182a44cSrillig /*	$NetBSD: pom.c,v 1.21 2021/05/02 12:50:46 rillig Exp $	*/
242fb1b9dScgd 
361f28255Scgd /*
442fb1b9dScgd  * Copyright (c) 1989, 1993
542fb1b9dScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software posted to USENET.
861f28255Scgd  *
961f28255Scgd  * Redistribution and use in source and binary forms, with or without
1061f28255Scgd  * modification, are permitted provided that the following conditions
1161f28255Scgd  * are met:
1261f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1461f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1561f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1661f28255Scgd  *    documentation and/or other materials provided with the distribution.
17e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1861f28255Scgd  *    may be used to endorse or promote products derived from this software
1961f28255Scgd  *    without specific prior written permission.
2061f28255Scgd  *
2161f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2261f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2361f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2461f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2561f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2661f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2761f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2861f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2961f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3061f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3161f28255Scgd  * SUCH DAMAGE.
3261f28255Scgd  */
3361f28255Scgd 
345ab1a671Slukem #include <sys/cdefs.h>
3561f28255Scgd #ifndef lint
362fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
372fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd 
4061f28255Scgd #ifndef lint
4142fb1b9dScgd #if 0
4242fb1b9dScgd static char sccsid[] = "@(#)pom.c	8.1 (Berkeley) 5/31/93";
4342fb1b9dScgd #else
44*1182a44cSrillig __RCSID("$NetBSD: pom.c,v 1.21 2021/05/02 12:50:46 rillig Exp $");
4542fb1b9dScgd #endif
4661f28255Scgd #endif /* not lint */
4761f28255Scgd 
4861f28255Scgd /*
4961f28255Scgd  * Phase of the Moon.  Calculates the current phase of the moon.
5061f28255Scgd  * Based on routines from `Practical Astronomy with Your Calculator',
5161f28255Scgd  * by Duffett-Smith.  Comments give the section from the book that
5261f28255Scgd  * particular piece of code was adapted from.
5361f28255Scgd  *
5461f28255Scgd  * -- Keith E. Brandt  VIII 1984
5561f28255Scgd  *
564092f192Sjsm  * Updated to the Third Edition of Duffett-Smith's book, Paul Janzen, IX 1998
574092f192Sjsm  *
5861f28255Scgd  */
5961f28255Scgd 
604092f192Sjsm #include <ctype.h>
610ea52cbeSlukem #include <err.h>
625ab1a671Slukem #include <math.h>
6361f28255Scgd #include <stdio.h>
64afd5a260Sjtc #include <string.h>
654dc77192Sjeremy #include <stdlib.h>
664092f192Sjsm #include <time.h>
674092f192Sjsm #include <unistd.h>
6861f28255Scgd 
69d4741e5aShubertf #ifndef PI
70d4741e5aShubertf #define	PI	  3.14159265358979323846
71d4741e5aShubertf #endif
724092f192Sjsm 
734092f192Sjsm /*
744092f192Sjsm  * The EPOCH in the third edition of the book is 1990 Jan 0.0 TDT.
754092f192Sjsm  * In this program, we do not bother to correct for the differences
764092f192Sjsm  * between UTC (as shown by the UNIX clock) and TDT.  (TDT = TAI + 32.184s;
774092f192Sjsm  * TAI-UTC = 32s in Jan 1999.)
784092f192Sjsm  */
794092f192Sjsm #define EPOCH_MINUS_1970	(20 * 365 + 5 - 1) /* 20 years, 5 leaps, back 1 day to Jan 0 */
804092f192Sjsm #define	EPSILONg  279.403303	/* solar ecliptic long at EPOCH */
814092f192Sjsm #define	RHOg	  282.768422	/* solar ecliptic long of perigee at EPOCH */
824092f192Sjsm #define	ECCEN	  0.016713	/* solar orbit eccentricity */
834092f192Sjsm #define	lzero	  318.351648	/* lunar mean long at EPOCH */
844092f192Sjsm #define	Pzero	  36.340410	/* lunar mean long of perigee at EPOCH */
854092f192Sjsm #define	Nzero	  318.510107	/* lunar mean long of node at EPOCH */
8661f28255Scgd 
87cb5fd834Sjsm int	main(int, char *[]);
885305281bSdholland static void adj360(double *);
895305281bSdholland static double dtor(double);
905305281bSdholland static double potm(double);
915305281bSdholland static time_t parsetime(char *);
925305281bSdholland static void badformat(void) __dead;
9361f28255Scgd 
945ab1a671Slukem int
main(int argc,char * argv[])95b4df40b1Shubertf main(int argc, char *argv[])
9661f28255Scgd {
974092f192Sjsm 	time_t tmpt, now;
9861f28255Scgd 	double days, today, tomorrow;
994092f192Sjsm 	char buf[1024];
10061f28255Scgd 
1014092f192Sjsm 	if (time(&now) == (time_t)-1)
1024092f192Sjsm 		err(1, "time");
1034dc77192Sjeremy 	if (argc > 1) {
1044092f192Sjsm 		tmpt = parsetime(argv[1]);
1054092f192Sjsm 		strftime(buf, sizeof(buf), "%a %Y %b %e %H:%M:%S (%Z)",
1064092f192Sjsm 			localtime(&tmpt));
1074092f192Sjsm 		printf("%s:  ", buf);
1084dc77192Sjeremy 	} else {
1094092f192Sjsm 		tmpt = now;
1104dc77192Sjeremy 	}
1114092f192Sjsm 	days = (tmpt - EPOCH_MINUS_1970 * 86400) / 86400.0;
11261f28255Scgd 	today = potm(days) + .5;
1134092f192Sjsm 	if (tmpt < now)
1144092f192Sjsm 		(void)printf("The Moon was ");
1154092f192Sjsm 	else if (tmpt == now)
11661f28255Scgd 		(void)printf("The Moon is ");
1174092f192Sjsm 	else
1184092f192Sjsm 		(void)printf("The Moon will be ");
11961f28255Scgd 	if ((int)today == 100)
12061f28255Scgd 		(void)printf("Full\n");
12161f28255Scgd 	else if (!(int)today)
12261f28255Scgd 		(void)printf("New\n");
12361f28255Scgd 	else {
12461f28255Scgd 		tomorrow = potm(days + 1);
12561f28255Scgd 		if ((int)today == 50)
12661f28255Scgd 			(void)printf("%s\n", tomorrow > today ?
12761f28255Scgd 			    "at the First Quarter" : "at the Last Quarter");
1284092f192Sjsm 			/* today is 0.5 too big, but it doesn't matter here
1294092f192Sjsm 			 * since the phase is changing fast enough
1304092f192Sjsm 			 */
13161f28255Scgd 		else {
1324092f192Sjsm 			today -= 0.5;		/* Now it might matter */
13361f28255Scgd 			(void)printf("%s ", tomorrow > today ?
13461f28255Scgd 			    "Waxing" : "Waning");
13561f28255Scgd 			if (today > 50)
13661f28255Scgd 				(void)printf("Gibbous (%1.0f%% of Full)\n",
13761f28255Scgd 				    today);
13861f28255Scgd 			else if (today < 50)
13961f28255Scgd 				(void)printf("Crescent (%1.0f%% of Full)\n",
14061f28255Scgd 				    today);
14161f28255Scgd 		}
14261f28255Scgd 	}
143b4df40b1Shubertf 
144b4df40b1Shubertf 	return EXIT_SUCCESS;
14561f28255Scgd }
14661f28255Scgd 
14761f28255Scgd /*
14861f28255Scgd  * potm --
14961f28255Scgd  *	return phase of the moon
15061f28255Scgd  */
1515305281bSdholland static double
potm(double days)152b4df40b1Shubertf potm(double days)
15361f28255Scgd {
15461f28255Scgd 	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
15561f28255Scgd 	double A4, lprime, V, ldprime, D, Nm;
15661f28255Scgd 
1574092f192Sjsm 	N = 360 * days / 365.242191;				/* sec 46 #3 */
15861f28255Scgd 	adj360(&N);
1594092f192Sjsm 	Msol = N + EPSILONg - RHOg;				/* sec 46 #4 */
16061f28255Scgd 	adj360(&Msol);
1614092f192Sjsm 	Ec = 360 / PI * ECCEN * sin(dtor(Msol));		/* sec 46 #5 */
1624092f192Sjsm 	LambdaSol = N + Ec + EPSILONg;				/* sec 46 #6 */
16361f28255Scgd 	adj360(&LambdaSol);
1644092f192Sjsm 	l = 13.1763966 * days + lzero;				/* sec 65 #4 */
16561f28255Scgd 	adj360(&l);
1664092f192Sjsm 	Mm = l - (0.1114041 * days) - Pzero;			/* sec 65 #5 */
16761f28255Scgd 	adj360(&Mm);
1684092f192Sjsm 	Nm = Nzero - (0.0529539 * days);			/* sec 65 #6 */
16961f28255Scgd 	adj360(&Nm);
1704092f192Sjsm 	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 65 #7 */
1714092f192Sjsm 	Ac = 0.1858 * sin(dtor(Msol));				/* sec 65 #8 */
17261f28255Scgd 	A3 = 0.37 * sin(dtor(Msol));
1734092f192Sjsm 	Mmprime = Mm + Ev - Ac - A3;				/* sec 65 #9 */
1744092f192Sjsm 	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 65 #10 */
1754092f192Sjsm 	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 65 #11 */
1764092f192Sjsm 	lprime = l + Ev + Ec - Ac + A4;				/* sec 65 #12 */
1774092f192Sjsm 	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 65 #13 */
1784092f192Sjsm 	ldprime = lprime + V;					/* sec 65 #14 */
1794092f192Sjsm 	D = ldprime - LambdaSol;				/* sec 67 #2 */
1804092f192Sjsm 	return(50.0 * (1 - cos(dtor(D))));			/* sec 67 #3 */
18161f28255Scgd }
18261f28255Scgd 
18361f28255Scgd /*
18461f28255Scgd  * dtor --
18561f28255Scgd  *	convert degrees to radians
18661f28255Scgd  */
1875305281bSdholland static double
dtor(double deg)188b4df40b1Shubertf dtor(double deg)
18961f28255Scgd {
19061f28255Scgd 	return(deg * PI / 180);
19161f28255Scgd }
19261f28255Scgd 
19361f28255Scgd /*
19461f28255Scgd  * adj360 --
19561f28255Scgd  *	adjust value so 0 <= deg <= 360
19661f28255Scgd  */
1975305281bSdholland static void
adj360(double * deg)198b4df40b1Shubertf adj360(double *deg)
19961f28255Scgd {
20061f28255Scgd 	for (;;)
20161f28255Scgd 		if (*deg < 0)
20261f28255Scgd 			*deg += 360;
20361f28255Scgd 		else if (*deg > 360)
20461f28255Scgd 			*deg -= 360;
20561f28255Scgd 		else
20661f28255Scgd 			break;
20761f28255Scgd }
2084092f192Sjsm 
2094092f192Sjsm #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
2105305281bSdholland static time_t
parsetime(char * p)211b4df40b1Shubertf parsetime(char *p)
2124092f192Sjsm {
2134092f192Sjsm 	struct tm *lt;
2144092f192Sjsm 	int bigyear;
2154092f192Sjsm 	int yearset = 0;
2164092f192Sjsm 	time_t tval;
217bfaf2fa3Sdholland 	char *t;
2184092f192Sjsm 
2194092f192Sjsm 	for (t = p; *t; ++t) {
220bfaf2fa3Sdholland 		if (isdigit((unsigned char) *t))
2214092f192Sjsm 			continue;
2224092f192Sjsm 		badformat();
2234092f192Sjsm 	}
2244092f192Sjsm 
2254092f192Sjsm 	tval = time(NULL);
2264092f192Sjsm 	lt = localtime(&tval);
2274092f192Sjsm 	lt->tm_sec = 0;
2284092f192Sjsm 	lt->tm_min = 0;
2294092f192Sjsm 
2304092f192Sjsm 	switch (strlen(p)) {
2314092f192Sjsm 	case 10:				/* yyyy */
2324092f192Sjsm 		bigyear = ATOI2(p);
2334092f192Sjsm 		lt->tm_year = bigyear * 100 - 1900;
2344092f192Sjsm 		yearset = 1;
2354092f192Sjsm 		/* FALLTHROUGH */
2364092f192Sjsm 	case 8:					/* yy */
2374092f192Sjsm 		if (yearset) {
2384092f192Sjsm 			lt->tm_year += ATOI2(p);
2394092f192Sjsm 		} else {
2404092f192Sjsm 			lt->tm_year = ATOI2(p);
2414092f192Sjsm 			if (lt->tm_year < 69)		/* hack for 2000 */
2424092f192Sjsm 				lt->tm_year += 100;
2434092f192Sjsm 		}
2444092f192Sjsm 		/* FALLTHROUGH */
2454092f192Sjsm 	case 6:					/* mm */
2464092f192Sjsm 		lt->tm_mon = ATOI2(p);
2474092f192Sjsm 		if ((lt->tm_mon > 12) || !lt->tm_mon)
2484092f192Sjsm 			badformat();
2494092f192Sjsm 		--lt->tm_mon;			/* time struct is 0 - 11 */
2504092f192Sjsm 		/* FALLTHROUGH */
2514092f192Sjsm 	case 4:					/* dd */
2524092f192Sjsm 		lt->tm_mday = ATOI2(p);
2534092f192Sjsm 		if ((lt->tm_mday > 31) || !lt->tm_mday)
2544092f192Sjsm 			badformat();
2554092f192Sjsm 		/* FALLTHROUGH */
2564092f192Sjsm 	case 2:					/* HH */
2574092f192Sjsm 		lt->tm_hour = ATOI2(p);
2584092f192Sjsm 		if (lt->tm_hour > 23)
2594092f192Sjsm 			badformat();
2604092f192Sjsm 		break;
2614092f192Sjsm 	default:
2624092f192Sjsm 		badformat();
2634092f192Sjsm 	}
2644092f192Sjsm 	/* The calling code needs a valid tm_ydays and this is the easiest
2654092f192Sjsm 	 * way to get one */
2664092f192Sjsm 	if ((tval = mktime(lt)) == -1)
2674092f192Sjsm 		errx(1, "specified date is outside allowed range");
2684092f192Sjsm 	return (tval);
2694092f192Sjsm }
2704092f192Sjsm 
2715305281bSdholland static void
badformat(void)272b4df40b1Shubertf badformat(void)
2734092f192Sjsm {
2744092f192Sjsm 	warnx("illegal time format");
27566880467Spgoyette 	(void)fprintf(stderr, "usage: %s [[[[[cc]yy]mm]dd]HH]\n",
27666880467Spgoyette 	    getprogname());
277b4df40b1Shubertf 	exit(EXIT_FAILURE);
2784092f192Sjsm }
279