1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software posted to USENET. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Phase of the Moon. Calculates the current phase of the moon. 34 * Based on routines from `Practical Astronomy with Your Calculator', 35 * by Duffett-Smith. Comments give the section from the book that 36 * particular piece of code was adapted from. 37 * 38 * -- Keith E. Brandt VIII 1984 39 * 40 */ 41 42 #include <sys/capsicum.h> 43 #include <capsicum_helpers.h> 44 45 #include <err.h> 46 #include <errno.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <math.h> 50 #include <string.h> 51 #include <sysexits.h> 52 #include <time.h> 53 #include <unistd.h> 54 55 #ifndef PI 56 #define PI 3.14159265358979323846 57 #endif 58 #define EPOCH 85 59 #define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */ 60 #define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */ 61 #define ECCEN 0.01671542 /* solar orbit eccentricity */ 62 #define lzero 18.251907 /* lunar mean long at EPOCH */ 63 #define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */ 64 #define Nzero 55.204723 /* lunar mean long of node at EPOCH */ 65 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 66 67 static void adj360(double *); 68 static double dtor(double); 69 static double potm(double); 70 static void usage(char *progname); 71 72 int 73 main(int argc, char **argv) 74 { 75 time_t tt; 76 struct tm GMT, tmd; 77 double days, today, tomorrow; 78 int ch, cnt, pflag = 0; 79 char *odate = NULL, *otime = NULL; 80 char *progname = argv[0]; 81 82 if (caph_limit_stdio() < 0) 83 err(1, "unable to limit capabitilities for stdio"); 84 85 caph_cache_catpages(); 86 caph_cache_tzdata(); 87 if (caph_enter() < 0) 88 err(1, "unable to enter capability mode"); 89 90 while ((ch = getopt(argc, argv, "d:pt:")) != -1) 91 switch (ch) { 92 case 'd': 93 odate = optarg; 94 break; 95 case 'p': 96 pflag = 1; 97 break; 98 case 't': 99 otime = optarg; 100 break; 101 default: 102 usage(progname); 103 } 104 105 argc -= optind; 106 argv += optind; 107 108 if (argc) 109 usage(progname); 110 111 /* Adjust based on users preferences */ 112 time(&tt); 113 if (otime != NULL || odate != NULL) { 114 /* Save today in case -d isn't specified */ 115 localtime_r(&tt, &tmd); 116 117 if (odate != NULL) { 118 tmd.tm_year = strtol(odate, NULL, 10) - 1900; 119 tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1; 120 tmd.tm_mday = strtol(odate + 8, NULL, 10); 121 /* Use midnight as the middle of the night */ 122 tmd.tm_hour = 0; 123 tmd.tm_min = 0; 124 tmd.tm_sec = 0; 125 tmd.tm_isdst = -1; 126 } 127 if (otime != NULL) { 128 tmd.tm_hour = strtol(otime, NULL, 10); 129 tmd.tm_min = strtol(otime + 3, NULL, 10); 130 tmd.tm_sec = strtol(otime + 6, NULL, 10); 131 tmd.tm_isdst = -1; 132 } 133 tt = mktime(&tmd); 134 } 135 136 gmtime_r(&tt, &GMT); 137 days = (GMT.tm_yday + 1) + ((GMT.tm_hour + 138 (GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0); 139 for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt) 140 days += isleap(1900 + cnt) ? 366 : 365; 141 today = potm(days); 142 if (pflag) { 143 (void)printf("%1.0f\n", today); 144 return (0); 145 } 146 (void)printf("The Moon is "); 147 if (today >= 99.5) 148 (void)printf("Full\n"); 149 else if (today < 0.5) 150 (void)printf("New\n"); 151 else { 152 tomorrow = potm(days + 1); 153 if (today >= 49.5 && today < 50.5) 154 (void)printf("%s\n", tomorrow > today ? 155 "at the First Quarter" : "at the Last Quarter"); 156 else { 157 (void)printf("%s ", tomorrow > today ? 158 "Waxing" : "Waning"); 159 if (today > 50) 160 (void)printf("Gibbous (%1.0f%% of Full)\n", 161 today); 162 else if (today < 50) 163 (void)printf("Crescent (%1.0f%% of Full)\n", 164 today); 165 } 166 } 167 168 return 0; 169 } 170 171 /* 172 * potm -- 173 * return phase of the moon 174 */ 175 static double 176 potm(double days) 177 { 178 double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime; 179 double A4, lprime, V, ldprime, D, Nm; 180 181 N = 360 * days / 365.2422; /* sec 42 #3 */ 182 adj360(&N); 183 Msol = N + EPSILONg - RHOg; /* sec 42 #4 */ 184 adj360(&Msol); 185 Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */ 186 LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */ 187 adj360(&LambdaSol); 188 l = 13.1763966 * days + lzero; /* sec 61 #4 */ 189 adj360(&l); 190 Mm = l - (0.1114041 * days) - Pzero; /* sec 61 #5 */ 191 adj360(&Mm); 192 Nm = Nzero - (0.0529539 * days); /* sec 61 #6 */ 193 adj360(&Nm); 194 Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */ 195 Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */ 196 A3 = 0.37 * sin(dtor(Msol)); 197 Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */ 198 Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */ 199 A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */ 200 lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */ 201 V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */ 202 ldprime = lprime + V; /* sec 61 #14 */ 203 D = ldprime - LambdaSol; /* sec 63 #2 */ 204 return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */ 205 } 206 207 /* 208 * dtor -- 209 * convert degrees to radians 210 */ 211 static double 212 dtor(double deg) 213 { 214 215 return(deg * PI / 180); 216 } 217 218 /* 219 * adj360 -- 220 * adjust value so 0 <= deg <= 360 221 */ 222 static void 223 adj360(double *deg) 224 { 225 226 for (;;) 227 if (*deg < 0) 228 *deg += 360; 229 else if (*deg > 360) 230 *deg -= 360; 231 else 232 break; 233 } 234 235 static void 236 usage(char *progname) 237 { 238 239 fprintf(stderr, "Usage: %s [-p] [-d yyyy.mm.dd] [-t hh:mm:ss]\n", 240 progname); 241 exit(EX_USAGE); 242 } 243