xref: /freebsd/usr.bin/calendar/pom.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
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 #endif
42 #include <sys/cdefs.h>
43 /*
44  * Phase of the Moon.  Calculates the current phase of the moon.
45  * Based on routines from `Practical Astronomy with Your Calculator',
46  * by Duffett-Smith.  Comments give the section from the book that
47  * particular piece of code was adapted from.
48  *
49  * -- Keith E. Brandt  VIII 1984
50  *
51  */
52 
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <math.h>
56 #include <string.h>
57 #include <sysexits.h>
58 #include <time.h>
59 #include <unistd.h>
60 
61 #include "calendar.h"
62 
63 #ifndef	PI
64 #define	PI	  3.14159265358979323846
65 #endif
66 #define	EPOCH	  85
67 #define	EPSILONg  279.611371	/* solar ecliptic long at EPOCH */
68 #define	RHOg	  282.680403	/* solar ecliptic long of perigee at EPOCH */
69 #define	ECCEN	  0.01671542	/* solar orbit eccentricity */
70 #define	lzero	  18.251907	/* lunar mean long at EPOCH */
71 #define	Pzero	  192.917585	/* lunar mean long of perigee at EPOCH */
72 #define	Nzero	  55.204723	/* lunar mean long of node at EPOCH */
73 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
74 
75 static void	adj360(double *);
76 static double	dtor(double);
77 static double	potm(double onday);
78 static double	potm_minute(double onday, int olddir);
79 
80 void
81 pom(int year, double utcoffset, int *fms, int *nms)
82 {
83 	double ffms[MAXMOONS];
84 	double fnms[MAXMOONS];
85 	int i, j;
86 
87 	fpom(year, utcoffset, ffms, fnms);
88 
89 	j = 0;
90 	for (i = 0; ffms[i] != 0; i++)
91 		fms[j++] = round(ffms[i]);
92 	fms[i] = -1;
93 	for (i = 0; fnms[i] != 0; i++)
94 		nms[i] = round(fnms[i]);
95 	nms[i] = -1;
96 }
97 
98 void
99 fpom(int year, double utcoffset, double *ffms, double *fnms)
100 {
101 	time_t tt;
102 	struct tm GMT, tmd_today, tmd_tomorrow;
103 	double days_today, days_tomorrow, today, tomorrow;
104 	int cnt, d;
105 	int yeardays;
106 	int olddir, newdir;
107 	double *pfnms, *pffms, t;
108 
109 	pfnms = fnms;
110 	pffms = ffms;
111 
112 	/*
113 	 * We take the phase of the moon one second before and one second
114 	 * after midnight.
115 	 */
116 	memset(&tmd_today, 0, sizeof(tmd_today));
117 	tmd_today.tm_year = year - 1900;
118 	tmd_today.tm_mon = 0;
119 	tmd_today.tm_mday = -1;		/* 31 December */
120 	tmd_today.tm_hour = 23;
121 	tmd_today.tm_min = 59;
122 	tmd_today.tm_sec = 59;
123 	memset(&tmd_tomorrow, 0, sizeof(tmd_tomorrow));
124 	tmd_tomorrow.tm_year = year - 1900;
125 	tmd_tomorrow.tm_mon = 0;
126 	tmd_tomorrow.tm_mday = 0;	/* 01 January */
127 	tmd_tomorrow.tm_hour = 0;
128 	tmd_tomorrow.tm_min = 0;
129 	tmd_tomorrow.tm_sec = 1;
130 
131 	tt = mktime(&tmd_today);
132 	gmtime_r(&tt, &GMT);
133 	yeardays = 0;
134 	for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
135 		yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
136 	days_today = (GMT.tm_yday + 1) + ((GMT.tm_hour +
137 	    (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
138 	    FHOURSPERDAY);
139 	days_today += yeardays;
140 
141 	tt = mktime(&tmd_tomorrow);
142 	gmtime_r(&tt, &GMT);
143 	yeardays = 0;
144 	for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
145 		yeardays += isleap(1900 + cnt) ? DAYSPERLEAPYEAR : DAYSPERYEAR;
146 	days_tomorrow = (GMT.tm_yday + 1) + ((GMT.tm_hour +
147 	    (GMT.tm_min / FSECSPERMINUTE) + (GMT.tm_sec / FSECSPERHOUR)) /
148 	    FHOURSPERDAY);
149 	days_tomorrow += yeardays;
150 
151 	today = potm(days_today);		/* 30 December 23:59:59 */
152 	tomorrow = potm(days_tomorrow);		/* 31 December 00:00:01 */
153 	olddir = today > tomorrow ? -1 : +1;
154 
155 	yeardays = 1 + (isleap(year) ? DAYSPERLEAPYEAR : DAYSPERYEAR); /* reuse */
156 	for (d = 0; d <= yeardays; d++) {
157 		today = potm(days_today);
158 		tomorrow = potm(days_tomorrow);
159 		newdir = today > tomorrow ? -1 : +1;
160 		if (olddir != newdir) {
161 			t = potm_minute(days_today - 1, olddir) +
162 			     utcoffset / FHOURSPERDAY;
163 			if (olddir == -1 && newdir == +1) {
164 				*pfnms = d - 1 + t;
165 				pfnms++;
166 			} else if (olddir == +1 && newdir == -1) {
167 				*pffms = d - 1 + t;
168 				pffms++;
169 			}
170 		}
171 		olddir = newdir;
172 		days_today++;
173 		days_tomorrow++;
174 	}
175 	*pffms = -1;
176 	*pfnms = -1;
177 }
178 
179 static double
180 potm_minute(double onday, int olddir) {
181 	double period = FSECSPERDAY / 2.0;
182 	double p1, p2;
183 	double before, after;
184 	int newdir;
185 
186 //	printf("---> days:%g olddir:%d\n", days, olddir);
187 
188 	p1 = onday + (period / SECSPERDAY);
189 	period /= 2;
190 
191 	while (period > 30) {	/* half a minute */
192 //		printf("period:%g - p1:%g - ", period, p1);
193 		p2 = p1 + (2.0 / SECSPERDAY);
194 		before = potm(p1);
195 		after = potm(p2);
196 //		printf("before:%10.10g - after:%10.10g\n", before, after);
197 		newdir = before < after ? -1 : +1;
198 		if (olddir != newdir)
199 			p1 += (period / SECSPERDAY);
200 		else
201 			p1 -= (period / SECSPERDAY);
202 		period /= 2;
203 //		printf("newdir:%d - p1:%10.10f - period:%g\n",
204 //		    newdir, p1, period);
205 	}
206 	p1 -= floor(p1);
207 	//exit(0);
208 	return (p1);
209 }
210 
211 /*
212  * potm --
213  *	return phase of the moon, as a percentage [0 ... 100]
214  */
215 static double
216 potm(double onday)
217 {
218 	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
219 	double A4, lprime, V, ldprime, D, Nm;
220 
221 	N = 360 * onday / 365.2422;				/* sec 42 #3 */
222 	adj360(&N);
223 	Msol = N + EPSILONg - RHOg;				/* sec 42 #4 */
224 	adj360(&Msol);
225 	Ec = 360 / PI * ECCEN * sin(dtor(Msol));		/* sec 42 #5 */
226 	LambdaSol = N + Ec + EPSILONg;				/* sec 42 #6 */
227 	adj360(&LambdaSol);
228 	l = 13.1763966 * onday + lzero;				/* sec 61 #4 */
229 	adj360(&l);
230 	Mm = l - (0.1114041 * onday) - Pzero;			/* sec 61 #5 */
231 	adj360(&Mm);
232 	Nm = Nzero - (0.0529539 * onday);			/* sec 61 #6 */
233 	adj360(&Nm);
234 	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 61 #7 */
235 	Ac = 0.1858 * sin(dtor(Msol));				/* sec 61 #8 */
236 	A3 = 0.37 * sin(dtor(Msol));
237 	Mmprime = Mm + Ev - Ac - A3;				/* sec 61 #9 */
238 	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 61 #10 */
239 	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 61 #11 */
240 	lprime = l + Ev + Ec - Ac + A4;				/* sec 61 #12 */
241 	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 61 #13 */
242 	ldprime = lprime + V;					/* sec 61 #14 */
243 	D = ldprime - LambdaSol;				/* sec 63 #2 */
244 	return(50 * (1 - cos(dtor(D))));			/* sec 63 #3 */
245 }
246 
247 /*
248  * dtor --
249  *	convert degrees to radians
250  */
251 static double
252 dtor(double deg)
253 {
254 
255 	return(deg * PI / 180);
256 }
257 
258 /*
259  * adj360 --
260  *	adjust value so 0 <= deg <= 360
261  */
262 static void
263 adj360(double *deg)
264 {
265 
266 	for (;;)
267 		if (*deg < 0)
268 			*deg += 360;
269 		else if (*deg > 360)
270 			*deg -= 360;
271 		else
272 			break;
273 }
274