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 #if 0 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1989, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #endif 40 #include <sys/cdefs.h> 41 /* 42 * Phase of the Moon. Calculates the current phase of the moon. 43 * Based on routines from `Practical Astronomy with Your Calculator', 44 * by Duffett-Smith. Comments give the section from the book that 45 * particular piece of code was adapted from. 46 * 47 * -- Keith E. Brandt VIII 1984 48 * 49 */ 50 51 #include <sys/capsicum.h> 52 #include <capsicum_helpers.h> 53 54 #include <err.h> 55 #include <errno.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <math.h> 59 #include <string.h> 60 #include <sysexits.h> 61 #include <time.h> 62 #include <unistd.h> 63 64 #ifndef PI 65 #define PI 3.14159265358979323846 66 #endif 67 #define EPOCH 85 68 #define EPSILONg 279.611371 /* solar ecliptic long at EPOCH */ 69 #define RHOg 282.680403 /* solar ecliptic long of perigee at EPOCH */ 70 #define ECCEN 0.01671542 /* solar orbit eccentricity */ 71 #define lzero 18.251907 /* lunar mean long at EPOCH */ 72 #define Pzero 192.917585 /* lunar mean long of perigee at EPOCH */ 73 #define Nzero 55.204723 /* lunar mean long of node at EPOCH */ 74 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 75 76 static void adj360(double *); 77 static double dtor(double); 78 static double potm(double); 79 static void usage(char *progname); 80 81 int 82 main(int argc, char **argv) 83 { 84 time_t tt; 85 struct tm GMT, tmd; 86 double days, today, tomorrow; 87 int ch, cnt, pflag = 0; 88 char *odate = NULL, *otime = NULL; 89 char *progname = argv[0]; 90 91 if (caph_limit_stdio() < 0) 92 err(1, "unable to limit capabitilities for stdio"); 93 94 caph_cache_catpages(); 95 if (caph_enter() < 0) 96 err(1, "unable to enter capability mode"); 97 98 while ((ch = getopt(argc, argv, "d:pt:")) != -1) 99 switch (ch) { 100 case 'd': 101 odate = optarg; 102 break; 103 case 'p': 104 pflag = 1; 105 break; 106 case 't': 107 otime = optarg; 108 break; 109 default: 110 usage(progname); 111 } 112 113 argc -= optind; 114 argv += optind; 115 116 if (argc) 117 usage(progname); 118 119 /* Adjust based on users preferences */ 120 time(&tt); 121 if (otime != NULL || odate != NULL) { 122 /* Save today in case -d isn't specified */ 123 localtime_r(&tt, &tmd); 124 125 if (odate != NULL) { 126 tmd.tm_year = strtol(odate, NULL, 10) - 1900; 127 tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1; 128 tmd.tm_mday = strtol(odate + 8, NULL, 10); 129 /* Use midnight as the middle of the night */ 130 tmd.tm_hour = 0; 131 tmd.tm_min = 0; 132 tmd.tm_sec = 0; 133 tmd.tm_isdst = -1; 134 } 135 if (otime != NULL) { 136 tmd.tm_hour = strtol(otime, NULL, 10); 137 tmd.tm_min = strtol(otime + 3, NULL, 10); 138 tmd.tm_sec = strtol(otime + 6, NULL, 10); 139 tmd.tm_isdst = -1; 140 } 141 tt = mktime(&tmd); 142 } 143 144 gmtime_r(&tt, &GMT); 145 days = (GMT.tm_yday + 1) + ((GMT.tm_hour + 146 (GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0); 147 for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt) 148 days += isleap(1900 + cnt) ? 366 : 365; 149 today = potm(days); 150 if (pflag) { 151 (void)printf("%1.0f\n", today); 152 return (0); 153 } 154 (void)printf("The Moon is "); 155 if (today >= 99.5) 156 (void)printf("Full\n"); 157 else if (today < 0.5) 158 (void)printf("New\n"); 159 else { 160 tomorrow = potm(days + 1); 161 if (today >= 49.5 && today < 50.5) 162 (void)printf("%s\n", tomorrow > today ? 163 "at the First Quarter" : "at the Last Quarter"); 164 else { 165 (void)printf("%s ", tomorrow > today ? 166 "Waxing" : "Waning"); 167 if (today > 50) 168 (void)printf("Gibbous (%1.0f%% of Full)\n", 169 today); 170 else if (today < 50) 171 (void)printf("Crescent (%1.0f%% of Full)\n", 172 today); 173 } 174 } 175 176 return 0; 177 } 178 179 /* 180 * potm -- 181 * return phase of the moon 182 */ 183 static double 184 potm(double days) 185 { 186 double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime; 187 double A4, lprime, V, ldprime, D, Nm; 188 189 N = 360 * days / 365.2422; /* sec 42 #3 */ 190 adj360(&N); 191 Msol = N + EPSILONg - RHOg; /* sec 42 #4 */ 192 adj360(&Msol); 193 Ec = 360 / PI * ECCEN * sin(dtor(Msol)); /* sec 42 #5 */ 194 LambdaSol = N + Ec + EPSILONg; /* sec 42 #6 */ 195 adj360(&LambdaSol); 196 l = 13.1763966 * days + lzero; /* sec 61 #4 */ 197 adj360(&l); 198 Mm = l - (0.1114041 * days) - Pzero; /* sec 61 #5 */ 199 adj360(&Mm); 200 Nm = Nzero - (0.0529539 * days); /* sec 61 #6 */ 201 adj360(&Nm); 202 Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm)); /* sec 61 #7 */ 203 Ac = 0.1858 * sin(dtor(Msol)); /* sec 61 #8 */ 204 A3 = 0.37 * sin(dtor(Msol)); 205 Mmprime = Mm + Ev - Ac - A3; /* sec 61 #9 */ 206 Ec = 6.2886 * sin(dtor(Mmprime)); /* sec 61 #10 */ 207 A4 = 0.214 * sin(dtor(2 * Mmprime)); /* sec 61 #11 */ 208 lprime = l + Ev + Ec - Ac + A4; /* sec 61 #12 */ 209 V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol))); /* sec 61 #13 */ 210 ldprime = lprime + V; /* sec 61 #14 */ 211 D = ldprime - LambdaSol; /* sec 63 #2 */ 212 return(50 * (1 - cos(dtor(D)))); /* sec 63 #3 */ 213 } 214 215 /* 216 * dtor -- 217 * convert degrees to radians 218 */ 219 static double 220 dtor(double deg) 221 { 222 223 return(deg * PI / 180); 224 } 225 226 /* 227 * adj360 -- 228 * adjust value so 0 <= deg <= 360 229 */ 230 static void 231 adj360(double *deg) 232 { 233 234 for (;;) 235 if (*deg < 0) 236 *deg += 360; 237 else if (*deg > 360) 238 *deg -= 360; 239 else 240 break; 241 } 242 243 static void 244 usage(char *progname) 245 { 246 247 fprintf(stderr, "Usage: %s [-p] [-d yyyy.mm.dd] [-t hh:mm:ss]\n", 248 progname); 249 exit(EX_USAGE); 250 } 251