]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - pom/pom.c
use err/warn instead of perror
[bsdgames-darwin.git] / pom / pom.c
1 /* $NetBSD: pom.c,v 1.8 1997/10/12 01:01:39 lukem Exp $ */
2
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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include <sys/cdefs.h>
39 #ifndef lint
40 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
41 The Regents of the University of California. All rights reserved.\n");
42 #endif /* not lint */
43
44 #ifndef lint
45 #if 0
46 static char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93";
47 #else
48 __RCSID("$NetBSD: pom.c,v 1.8 1997/10/12 01:01:39 lukem Exp $");
49 #endif
50 #endif /* not lint */
51
52 /*
53 * Phase of the Moon. Calculates the current phase of the moon.
54 * Based on routines from `Practical Astronomy with Your Calculator',
55 * by Duffett-Smith. Comments give the section from the book that
56 * particular piece of code was adapted from.
57 *
58 * -- Keith E. Brandt VIII 1984
59 *
60 */
61
62 #include <sys/time.h>
63 #include <err.h>
64 #include <errno.h>
65 #include <math.h>
66 #include <stdio.h>
67 #include <string.h>
68 #include <tzfile.h>
69
70 #define PI 3.141592654
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
79 void adj360 __P((double *));
80 double dtor __P((double));
81 int main __P((int, char *[]));
82 double potm __P((double));
83
84 int
85 main(argc, argv)
86 int argc;
87 char *argv[];
88 {
89 struct timeval tp;
90 struct timezone tzp;
91 struct tm *GMT;
92 time_t tmpt;
93 double days, today, tomorrow;
94 int cnt;
95
96 if (gettimeofday(&tp,&tzp))
97 err(1, "gettimeofday");
98 tmpt = tp.tv_sec;
99 GMT = gmtime(&tmpt);
100 days = (GMT->tm_yday + 1) + ((GMT->tm_hour +
101 (GMT->tm_min / 60.0) + (GMT->tm_sec / 3600.0)) / 24.0);
102 for (cnt = EPOCH; cnt < GMT->tm_year; ++cnt)
103 days += isleap(cnt) ? 366 : 365;
104 today = potm(days) + .5;
105 (void)printf("The Moon is ");
106 if ((int)today == 100)
107 (void)printf("Full\n");
108 else if (!(int)today)
109 (void)printf("New\n");
110 else {
111 tomorrow = potm(days + 1);
112 if ((int)today == 50)
113 (void)printf("%s\n", tomorrow > today ?
114 "at the First Quarter" : "at the Last Quarter");
115 else {
116 (void)printf("%s ", tomorrow > today ?
117 "Waxing" : "Waning");
118 if (today > 50)
119 (void)printf("Gibbous (%1.0f%% of Full)\n",
120 today);
121 else if (today < 50)
122 (void)printf("Crescent (%1.0f%% of Full)\n",
123 today);
124 }
125 }
126 exit(0);
127 }
128
129 /*
130 * potm --
131 * return phase of the moon
132 */
133 double
134 potm(days)
135 double days;
136 {
137 double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
138 double A4, lprime, V, ldprime, D, Nm;
139
140 N = 360 * days / 365.2422; /* sec 42 #3 */
141 adj360(&N);
142 Msol = N + EPSILONg - RHOg; /* sec 42 #4 */
143 adj360(&Msol);
144 Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */
145 LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */
146 adj360(&LambdaSol);
147 l = 13.1763966 * days + lzero; /* sec 61 #4 */
148 adj360(&l);
149 Mm = l - (0.1114041 * days) - Pzero; /* sec 61 #5 */
150 adj360(&Mm);
151 Nm = Nzero - (0.0529539 * days); /* sec 61 #6 */
152 adj360(&Nm);
153 Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */
154 Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */
155 A3 = 0.37 * sin(dtor(Msol));
156 Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */
157 Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */
158 A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */
159 lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */
160 V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */
161 ldprime = lprime + V; /* sec 61 #14 */
162 D = ldprime - LambdaSol; /* sec 63 #2 */
163 return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */
164 }
165
166 /*
167 * dtor --
168 * convert degrees to radians
169 */
170 double
171 dtor(deg)
172 double deg;
173 {
174 return(deg * PI / 180);
175 }
176
177 /*
178 * adj360 --
179 * adjust value so 0 <= deg <= 360
180 */
181 void
182 adj360(deg)
183 double *deg;
184 {
185 for (;;)
186 if (*deg < 0)
187 *deg += 360;
188 else if (*deg > 360)
189 *deg -= 360;
190 else
191 break;
192 }