pom.c 7.6 KB

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