1 1.21 rillig /* $NetBSD: pom.c,v 1.21 2021/05/02 12:50:46 rillig Exp $ */ 2 1.4 cgd 3 1.1 cgd /* 4 1.4 cgd * Copyright (c) 1989, 1993 5 1.4 cgd * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * This code is derived from software posted to USENET. 8 1.1 cgd * 9 1.1 cgd * Redistribution and use in source and binary forms, with or without 10 1.1 cgd * modification, are permitted provided that the following conditions 11 1.1 cgd * are met: 12 1.1 cgd * 1. Redistributions of source code must retain the above copyright 13 1.1 cgd * notice, this list of conditions and the following disclaimer. 14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 cgd * notice, this list of conditions and the following disclaimer in the 16 1.1 cgd * documentation and/or other materials provided with the distribution. 17 1.13 agc * 3. Neither the name of the University nor the names of its contributors 18 1.1 cgd * may be used to endorse or promote products derived from this software 19 1.1 cgd * without specific prior written permission. 20 1.1 cgd * 21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 1.1 cgd * SUCH DAMAGE. 32 1.1 cgd */ 33 1.1 cgd 34 1.7 lukem #include <sys/cdefs.h> 35 1.1 cgd #ifndef lint 36 1.17 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ 37 1.17 lukem The Regents of the University of California. All rights reserved."); 38 1.1 cgd #endif /* not lint */ 39 1.1 cgd 40 1.1 cgd #ifndef lint 41 1.4 cgd #if 0 42 1.4 cgd static char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93"; 43 1.4 cgd #else 44 1.21 rillig __RCSID("$NetBSD: pom.c,v 1.21 2021/05/02 12:50:46 rillig Exp $"); 45 1.4 cgd #endif 46 1.1 cgd #endif /* not lint */ 47 1.1 cgd 48 1.1 cgd /* 49 1.1 cgd * Phase of the Moon. Calculates the current phase of the moon. 50 1.1 cgd * Based on routines from `Practical Astronomy with Your Calculator', 51 1.1 cgd * by Duffett-Smith. Comments give the section from the book that 52 1.1 cgd * particular piece of code was adapted from. 53 1.1 cgd * 54 1.1 cgd * -- Keith E. Brandt VIII 1984 55 1.1 cgd * 56 1.12 jsm * Updated to the Third Edition of Duffett-Smith's book, Paul Janzen, IX 1998 57 1.12 jsm * 58 1.1 cgd */ 59 1.1 cgd 60 1.12 jsm #include <ctype.h> 61 1.8 lukem #include <err.h> 62 1.7 lukem #include <math.h> 63 1.1 cgd #include <stdio.h> 64 1.3 jtc #include <string.h> 65 1.9 jeremy #include <stdlib.h> 66 1.12 jsm #include <time.h> 67 1.12 jsm #include <unistd.h> 68 1.1 cgd 69 1.11 hubertf #ifndef PI 70 1.11 hubertf #define PI 3.14159265358979323846 71 1.11 hubertf #endif 72 1.12 jsm 73 1.12 jsm /* 74 1.12 jsm * The EPOCH in the third edition of the book is 1990 Jan 0.0 TDT. 75 1.12 jsm * In this program, we do not bother to correct for the differences 76 1.12 jsm * between UTC (as shown by the UNIX clock) and TDT. (TDT = TAI + 32.184s; 77 1.12 jsm * TAI-UTC = 32s in Jan 1999.) 78 1.12 jsm */ 79 1.12 jsm #define EPOCH_MINUS_1970 (20 * 365 + 5 - 1) /* 20 years, 5 leaps, back 1 day to Jan 0 */ 80 1.12 jsm #define EPSILONg 279.403303 /* solar ecliptic long at EPOCH */ 81 1.12 jsm #define RHOg 282.768422 /* solar ecliptic long of perigee at EPOCH */ 82 1.12 jsm #define ECCEN 0.016713 /* solar orbit eccentricity */ 83 1.12 jsm #define lzero 318.351648 /* lunar mean long at EPOCH */ 84 1.12 jsm #define Pzero 36.340410 /* lunar mean long of perigee at EPOCH */ 85 1.12 jsm #define Nzero 318.510107 /* lunar mean long of node at EPOCH */ 86 1.1 cgd 87 1.14 jsm int main(int, char *[]); 88 1.19 dholland static void adj360(double *); 89 1.19 dholland static double dtor(double); 90 1.19 dholland static double potm(double); 91 1.19 dholland static time_t parsetime(char *); 92 1.19 dholland static void badformat(void) __dead; 93 1.7 lukem 94 1.7 lukem int 95 1.15 hubertf main(int argc, char *argv[]) 96 1.1 cgd { 97 1.12 jsm time_t tmpt, now; 98 1.1 cgd double days, today, tomorrow; 99 1.12 jsm char buf[1024]; 100 1.1 cgd 101 1.12 jsm if (time(&now) == (time_t)-1) 102 1.12 jsm err(1, "time"); 103 1.9 jeremy if (argc > 1) { 104 1.12 jsm tmpt = parsetime(argv[1]); 105 1.12 jsm strftime(buf, sizeof(buf), "%a %Y %b %e %H:%M:%S (%Z)", 106 1.12 jsm localtime(&tmpt)); 107 1.12 jsm printf("%s: ", buf); 108 1.9 jeremy } else { 109 1.12 jsm tmpt = now; 110 1.9 jeremy } 111 1.12 jsm days = (tmpt - EPOCH_MINUS_1970 * 86400) / 86400.0; 112 1.1 cgd today = potm(days) + .5; 113 1.12 jsm if (tmpt < now) 114 1.12 jsm (void)printf("The Moon was "); 115 1.12 jsm else if (tmpt == now) 116 1.12 jsm (void)printf("The Moon is "); 117 1.12 jsm else 118 1.12 jsm (void)printf("The Moon will be "); 119 1.1 cgd if ((int)today == 100) 120 1.1 cgd (void)printf("Full\n"); 121 1.1 cgd else if (!(int)today) 122 1.1 cgd (void)printf("New\n"); 123 1.1 cgd else { 124 1.1 cgd tomorrow = potm(days + 1); 125 1.1 cgd if ((int)today == 50) 126 1.1 cgd (void)printf("%s\n", tomorrow > today ? 127 1.1 cgd "at the First Quarter" : "at the Last Quarter"); 128 1.12 jsm /* today is 0.5 too big, but it doesn't matter here 129 1.12 jsm * since the phase is changing fast enough 130 1.12 jsm */ 131 1.1 cgd else { 132 1.12 jsm today -= 0.5; /* Now it might matter */ 133 1.1 cgd (void)printf("%s ", tomorrow > today ? 134 1.1 cgd "Waxing" : "Waning"); 135 1.1 cgd if (today > 50) 136 1.1 cgd (void)printf("Gibbous (%1.0f%% of Full)\n", 137 1.1 cgd today); 138 1.1 cgd else if (today < 50) 139 1.1 cgd (void)printf("Crescent (%1.0f%% of Full)\n", 140 1.1 cgd today); 141 1.1 cgd } 142 1.1 cgd } 143 1.15 hubertf 144 1.15 hubertf return EXIT_SUCCESS; 145 1.1 cgd } 146 1.1 cgd 147 1.1 cgd /* 148 1.1 cgd * potm -- 149 1.1 cgd * return phase of the moon 150 1.1 cgd */ 151 1.19 dholland static double 152 1.15 hubertf potm(double days) 153 1.1 cgd { 154 1.1 cgd double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime; 155 1.1 cgd double A4, lprime, V, ldprime, D, Nm; 156 1.1 cgd 157 1.12 jsm N = 360 * days / 365.242191; /* sec 46 #3 */ 158 1.1 cgd adj360(&N); 159 1.12 jsm Msol = N + EPSILONg - RHOg; /* sec 46 #4 */ 160 1.1 cgd adj360(&Msol); 161 1.12 jsm Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 46 #5 */ 162 1.12 jsm LambdaSol = N + Ec + EPSILONg; /* sec 46 #6 */ 163 1.1 cgd adj360(&LambdaSol); 164 1.12 jsm l = 13.1763966 * days + lzero; /* sec 65 #4 */ 165 1.1 cgd adj360(&l); 166 1.12 jsm Mm = l - (0.1114041 * days) - Pzero; /* sec 65 #5 */ 167 1.1 cgd adj360(&Mm); 168 1.12 jsm Nm = Nzero - (0.0529539 * days); /* sec 65 #6 */ 169 1.1 cgd adj360(&Nm); 170 1.12 jsm Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 65 #7 */ 171 1.12 jsm Ac = 0.1858 * sin(dtor(Msol)); /* sec 65 #8 */ 172 1.1 cgd A3 = 0.37 * sin(dtor(Msol)); 173 1.12 jsm Mmprime = Mm + Ev - Ac - A3; /* sec 65 #9 */ 174 1.12 jsm Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 65 #10 */ 175 1.12 jsm A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 65 #11 */ 176 1.12 jsm lprime = l + Ev + Ec - Ac + A4; /* sec 65 #12 */ 177 1.12 jsm V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 65 #13 */ 178 1.12 jsm ldprime = lprime + V; /* sec 65 #14 */ 179 1.12 jsm D = ldprime - LambdaSol; /* sec 67 #2 */ 180 1.12 jsm return(50.0 * (1 - cos(dtor(D)))); /* sec 67 #3 */ 181 1.1 cgd } 182 1.1 cgd 183 1.1 cgd /* 184 1.1 cgd * dtor -- 185 1.1 cgd * convert degrees to radians 186 1.1 cgd */ 187 1.19 dholland static double 188 1.15 hubertf dtor(double deg) 189 1.1 cgd { 190 1.1 cgd return(deg * PI / 180); 191 1.1 cgd } 192 1.1 cgd 193 1.1 cgd /* 194 1.1 cgd * adj360 -- 195 1.1 cgd * adjust value so 0 <= deg <= 360 196 1.1 cgd */ 197 1.19 dholland static void 198 1.15 hubertf adj360(double *deg) 199 1.1 cgd { 200 1.1 cgd for (;;) 201 1.1 cgd if (*deg < 0) 202 1.1 cgd *deg += 360; 203 1.1 cgd else if (*deg > 360) 204 1.1 cgd *deg -= 360; 205 1.1 cgd else 206 1.1 cgd break; 207 1.12 jsm } 208 1.12 jsm 209 1.12 jsm #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 210 1.19 dholland static time_t 211 1.15 hubertf parsetime(char *p) 212 1.12 jsm { 213 1.12 jsm struct tm *lt; 214 1.12 jsm int bigyear; 215 1.12 jsm int yearset = 0; 216 1.12 jsm time_t tval; 217 1.18 dholland char *t; 218 1.21 rillig 219 1.12 jsm for (t = p; *t; ++t) { 220 1.18 dholland if (isdigit((unsigned char) *t)) 221 1.12 jsm continue; 222 1.12 jsm badformat(); 223 1.12 jsm } 224 1.12 jsm 225 1.12 jsm tval = time(NULL); 226 1.12 jsm lt = localtime(&tval); 227 1.12 jsm lt->tm_sec = 0; 228 1.12 jsm lt->tm_min = 0; 229 1.12 jsm 230 1.12 jsm switch (strlen(p)) { 231 1.12 jsm case 10: /* yyyy */ 232 1.12 jsm bigyear = ATOI2(p); 233 1.12 jsm lt->tm_year = bigyear * 100 - 1900; 234 1.12 jsm yearset = 1; 235 1.12 jsm /* FALLTHROUGH */ 236 1.12 jsm case 8: /* yy */ 237 1.12 jsm if (yearset) { 238 1.12 jsm lt->tm_year += ATOI2(p); 239 1.12 jsm } else { 240 1.12 jsm lt->tm_year = ATOI2(p); 241 1.12 jsm if (lt->tm_year < 69) /* hack for 2000 */ 242 1.12 jsm lt->tm_year += 100; 243 1.12 jsm } 244 1.12 jsm /* FALLTHROUGH */ 245 1.12 jsm case 6: /* mm */ 246 1.12 jsm lt->tm_mon = ATOI2(p); 247 1.12 jsm if ((lt->tm_mon > 12) || !lt->tm_mon) 248 1.12 jsm badformat(); 249 1.12 jsm --lt->tm_mon; /* time struct is 0 - 11 */ 250 1.12 jsm /* FALLTHROUGH */ 251 1.12 jsm case 4: /* dd */ 252 1.12 jsm lt->tm_mday = ATOI2(p); 253 1.12 jsm if ((lt->tm_mday > 31) || !lt->tm_mday) 254 1.12 jsm badformat(); 255 1.12 jsm /* FALLTHROUGH */ 256 1.12 jsm case 2: /* HH */ 257 1.12 jsm lt->tm_hour = ATOI2(p); 258 1.12 jsm if (lt->tm_hour > 23) 259 1.12 jsm badformat(); 260 1.12 jsm break; 261 1.12 jsm default: 262 1.12 jsm badformat(); 263 1.12 jsm } 264 1.12 jsm /* The calling code needs a valid tm_ydays and this is the easiest 265 1.12 jsm * way to get one */ 266 1.12 jsm if ((tval = mktime(lt)) == -1) 267 1.12 jsm errx(1, "specified date is outside allowed range"); 268 1.12 jsm return (tval); 269 1.12 jsm } 270 1.12 jsm 271 1.19 dholland static void 272 1.15 hubertf badformat(void) 273 1.12 jsm { 274 1.12 jsm warnx("illegal time format"); 275 1.20 pgoyette (void)fprintf(stderr, "usage: %s [[[[[cc]yy]mm]dd]HH]\n", 276 1.20 pgoyette getprogname()); 277 1.15 hubertf exit(EXIT_FAILURE); 278 1.1 cgd } 279