xref: /freebsd/usr.bin/calendar/pom.c (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software posted to USENET.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if 0
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 
41 #ifndef lint
42 static const char sccsid[] = "@(#)pom.c       8.1 (Berkeley) 5/31/93";
43 #endif /* not lint */
44 #endif
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 /*
49  * Phase of the Moon.  Calculates the current phase of the moon.
50  * Based on routines from `Practical Astronomy with Your Calculator',
51  * by Duffett-Smith.  Comments give the section from the book that
52  * particular piece of code was adapted from.
53  *
54  * -- Keith E. Brandt  VIII 1984
55  *
56  */
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <math.h>
61 #include <string.h>
62 #include <sysexits.h>
63 #include <time.h>
64 #include <unistd.h>
65 
66 #include "calendar.h"
67 
68 #ifndef	PI
69 #define	PI	  3.14159265358979323846
70 #endif
71 #define	EPOCH	  85
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)
79 
80 static void	adj360(double *);
81 static double	dtor(double);
82 static double	potm(double onday);
83 static double	potm_minute(double onday, int olddir);
84 
85 void
86 pom(int year, double utcoffset, int *fms, int *nms)
87 {
88 	double ffms[MAXMOONS];
89 	double fnms[MAXMOONS];
90 	int i, j;
91 
92 	fpom(year, utcoffset, ffms, fnms);
93 
94 	j = 0;
95 	for (i = 0; ffms[i] != 0; i++)
96 		fms[j++] = round(ffms[i]);
97 	fms[i] = -1;
98 	for (i = 0; fnms[i] != 0; i++)
99 		nms[i] = round(fnms[i]);
100 	nms[i] = -1;
101 }
102 
103 void
104 fpom(int year, double utcoffset, double *ffms, double *fnms)
105 {
106 	time_t tt;
107 	struct tm GMT, tmd_today, tmd_tomorrow;
108 	double days_today, days_tomorrow, today, tomorrow;
109 	int cnt, d;
110 	int yeardays;
111 	int olddir, newdir;
112 	double *pfnms, *pffms, t;
113 
114 	pfnms = fnms;
115 	pffms = ffms;
116 
117 	/*
118 	 * We take the phase of the moon one second before and one second
119 	 * after midnight.
120 	 */
121 	memset(&tmd_today, 0, sizeof(tmd_today));
122 	tmd_today.tm_year = year - 1900;
123 	tmd_today.tm_mon = 0;
124 	tmd_today.tm_mday = -1;		/* 31 December */
125 	tmd_today.tm_hour = 23;
126 	tmd_today.tm_min = 59;
127 	tmd_today.tm_sec = 59;
128 	memset(&tmd_tomorrow, 0, sizeof(tmd_tomorrow));
129 	tmd_tomorrow.tm_year = year - 1900;
130 	tmd_tomorrow.tm_mon = 0;
131 	tmd_tomorrow.tm_mday = 0;	/* 01 January */
132 	tmd_tomorrow.tm_hour = 0;
133 	tmd_tomorrow.tm_min = 0;
134 	tmd_tomorrow.tm_sec = 1;
135 
136 	tt = mktime(&tmd_today);
137 	gmtime_r(&tt, &GMT);
138 	yeardays = 0;
139 	for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
140 		yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
141 	days_today = (GMT.tm_yday + 1) + ((GMT.tm_hour +
142 	    (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
143 	    FHOURSPERDAY);
144 	days_today += yeardays;
145 
146 	tt = mktime(&tmd_tomorrow);
147 	gmtime_r(&tt, &GMT);
148 	yeardays = 0;
149 	for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
150 		yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
151 	days_tomorrow = (GMT.tm_yday + 1) + ((GMT.tm_hour +
152 	    (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
153 	    FHOURSPERDAY);
154 	days_tomorrow += yeardays;
155 
156 	today = potm(days_today);		/* 30 December 23:59:59 */
157 	tomorrow = potm(days_tomorrow);		/* 31 December 00:00:01 */
158 	olddir = today > tomorrow ? -1 : +1;
159 
160 	yeardays = 1 + (isleap(year) ? DAYSPERLEAPYEAR : DAYSPERYEAR); /* reuse */
161 	for (d = 0; d <= yeardays; d++) {
162 		today = potm(days_today);
163 		tomorrow = potm(days_tomorrow);
164 		newdir = today > tomorrow ? -1 : +1;
165 		if (olddir != newdir) {
166 			t = potm_minute(days_today - 1, olddir) +
167 			     utcoffset / FHOURSPERDAY;
168 			if (olddir == -1 && newdir == +1) {
169 				*pfnms = d - 1 + t;
170 				pfnms++;
171 			} else if (olddir == +1 && newdir == -1) {
172 				*pffms = d - 1 + t;
173 				pffms++;
174 			}
175 		}
176 		olddir = newdir;
177 		days_today++;
178 		days_tomorrow++;
179 	}
180 	*pffms = -1;
181 	*pfnms = -1;
182 }
183 
184 static double
185 potm_minute(double onday, int olddir) {
186 	double period = FSECSPERDAY / 2.0;
187 	double p1, p2;
188 	double before, after;
189 	int newdir;
190 
191 //	printf("---> days:%g olddir:%d\n", days, olddir);
192 
193 	p1 = onday + (period / SECSPERDAY);
194 	period /= 2;
195 
196 	while (period > 30) {	/* half a minute */
197 //		printf("period:%g - p1:%g - ", period, p1);
198 		p2 = p1 + (2.0 / SECSPERDAY);
199 		before = potm(p1);
200 		after = potm(p2);
201 //		printf("before:%10.10g - after:%10.10g\n", before, after);
202 		newdir = before < after ? -1 : +1;
203 		if (olddir != newdir)
204 			p1 += (period / SECSPERDAY);
205 		else
206 			p1 -= (period / SECSPERDAY);
207 		period /= 2;
208 //		printf("newdir:%d - p1:%10.10f - period:%g\n",
209 //		    newdir, p1, period);
210 	}
211 	p1 -= floor(p1);
212 	//exit(0);
213 	return (p1);
214 }
215 
216 /*
217  * potm --
218  *	return phase of the moon, as a percentage [0 ... 100]
219  */
220 static double
221 potm(double onday)
222 {
223 	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
224 	double A4, lprime, V, ldprime, D, Nm;
225 
226 	N = 360 * onday / 365.2422;				/* sec 42 #3 */
227 	adj360(&N);
228 	Msol = N + EPSILONg - RHOg;				/* sec 42 #4 */
229 	adj360(&Msol);
230 	Ec = 360 / PI * ECCEN * sin(dtor(Msol));		/* sec 42 #5 */
231 	LambdaSol = N + Ec + EPSILONg;				/* sec 42 #6 */
232 	adj360(&LambdaSol);
233 	l = 13.1763966 * onday + lzero;				/* sec 61 #4 */
234 	adj360(&l);
235 	Mm = l - (0.1114041 * onday) - Pzero;			/* sec 61 #5 */
236 	adj360(&Mm);
237 	Nm = Nzero - (0.0529539 * onday);			/* sec 61 #6 */
238 	adj360(&Nm);
239 	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 61 #7 */
240 	Ac = 0.1858 * sin(dtor(Msol));				/* sec 61 #8 */
241 	A3 = 0.37 * sin(dtor(Msol));
242 	Mmprime = Mm + Ev - Ac - A3;				/* sec 61 #9 */
243 	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 61 #10 */
244 	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 61 #11 */
245 	lprime = l + Ev + Ec - Ac + A4;				/* sec 61 #12 */
246 	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 61 #13 */
247 	ldprime = lprime + V;					/* sec 61 #14 */
248 	D = ldprime - LambdaSol;				/* sec 63 #2 */
249 	return(50 * (1 - cos(dtor(D))));			/* sec 63 #3 */
250 }
251 
252 /*
253  * dtor --
254  *	convert degrees to radians
255  */
256 static double
257 dtor(double deg)
258 {
259 
260 	return(deg * PI / 180);
261 }
262 
263 /*
264  * adj360 --
265  *	adjust value so 0 <= deg <= 360
266  */
267 static void
268 adj360(double *deg)
269 {
270 
271 	for (;;)
272 		if (*deg < 0)
273 			*deg += 360;
274 		else if (*deg > 360)
275 			*deg -= 360;
276 		else
277 			break;
278 }
279