Home | History | Annotate | Line # | Download | only in pom
pom.c revision 1.14
      1  1.14      jsm /*	$NetBSD: pom.c,v 1.14 2004/01/27 20:30:30 jsm 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.7    lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
     37   1.7    lukem 	The Regents of the University of California.  All rights reserved.\n");
     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.14      jsm __RCSID("$NetBSD: pom.c,v 1.14 2004/01/27 20:30:30 jsm 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 void	adj360(double *);
     88  1.14      jsm double	dtor(double);
     89  1.14      jsm int	main(int, char *[]);
     90  1.14      jsm double	potm(double);
     91  1.14      jsm time_t	parsetime(char *);
     92  1.14      jsm void	badformat(void) __attribute__((__noreturn__));
     93   1.7    lukem 
     94   1.7    lukem int
     95   1.7    lukem main(argc, argv)
     96   1.7    lukem 	int argc;
     97   1.7    lukem 	char *argv[];
     98   1.1      cgd {
     99  1.12      jsm 	time_t tmpt, now;
    100   1.1      cgd 	double days, today, tomorrow;
    101  1.12      jsm 	char buf[1024];
    102   1.1      cgd 
    103  1.12      jsm 	if (time(&now) == (time_t)-1)
    104  1.12      jsm 		err(1, "time");
    105   1.9   jeremy 	if (argc > 1) {
    106  1.12      jsm 		tmpt = parsetime(argv[1]);
    107  1.12      jsm 		strftime(buf, sizeof(buf), "%a %Y %b %e %H:%M:%S (%Z)",
    108  1.12      jsm 			localtime(&tmpt));
    109  1.12      jsm 		printf("%s:  ", buf);
    110   1.9   jeremy 	} else {
    111  1.12      jsm 		tmpt = now;
    112   1.9   jeremy 	}
    113  1.12      jsm 	days = (tmpt - EPOCH_MINUS_1970 * 86400) / 86400.0;
    114   1.1      cgd 	today = potm(days) + .5;
    115  1.12      jsm 	if (tmpt < now)
    116  1.12      jsm 		(void)printf("The Moon was ");
    117  1.12      jsm 	else if (tmpt == now)
    118  1.12      jsm 		(void)printf("The Moon is ");
    119  1.12      jsm 	else
    120  1.12      jsm 		(void)printf("The Moon will be ");
    121   1.1      cgd 	if ((int)today == 100)
    122   1.1      cgd 		(void)printf("Full\n");
    123   1.1      cgd 	else if (!(int)today)
    124   1.1      cgd 		(void)printf("New\n");
    125   1.1      cgd 	else {
    126   1.1      cgd 		tomorrow = potm(days + 1);
    127   1.1      cgd 		if ((int)today == 50)
    128   1.1      cgd 			(void)printf("%s\n", tomorrow > today ?
    129   1.1      cgd 			    "at the First Quarter" : "at the Last Quarter");
    130  1.12      jsm 			/* today is 0.5 too big, but it doesn't matter here
    131  1.12      jsm 			 * since the phase is changing fast enough
    132  1.12      jsm 			 */
    133   1.1      cgd 		else {
    134  1.12      jsm 			today -= 0.5;		/* Now it might matter */
    135   1.1      cgd 			(void)printf("%s ", tomorrow > today ?
    136   1.1      cgd 			    "Waxing" : "Waning");
    137   1.1      cgd 			if (today > 50)
    138   1.1      cgd 				(void)printf("Gibbous (%1.0f%% of Full)\n",
    139   1.1      cgd 				    today);
    140   1.1      cgd 			else if (today < 50)
    141   1.1      cgd 				(void)printf("Crescent (%1.0f%% of Full)\n",
    142   1.1      cgd 				    today);
    143   1.1      cgd 		}
    144   1.1      cgd 	}
    145   1.3      jtc 	exit(0);
    146   1.1      cgd }
    147   1.1      cgd 
    148   1.1      cgd /*
    149   1.1      cgd  * potm --
    150   1.1      cgd  *	return phase of the moon
    151   1.1      cgd  */
    152   1.1      cgd double
    153   1.1      cgd potm(days)
    154   1.1      cgd 	double days;
    155   1.1      cgd {
    156   1.1      cgd 	double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
    157   1.1      cgd 	double A4, lprime, V, ldprime, D, Nm;
    158   1.1      cgd 
    159  1.12      jsm 	N = 360 * days / 365.242191;				/* sec 46 #3 */
    160   1.1      cgd 	adj360(&N);
    161  1.12      jsm 	Msol = N + EPSILONg - RHOg;				/* sec 46 #4 */
    162   1.1      cgd 	adj360(&Msol);
    163  1.12      jsm 	Ec = 360 / PI * ECCEN * sin(dtor(Msol));		/* sec 46 #5 */
    164  1.12      jsm 	LambdaSol = N + Ec + EPSILONg;				/* sec 46 #6 */
    165   1.1      cgd 	adj360(&LambdaSol);
    166  1.12      jsm 	l = 13.1763966 * days + lzero;				/* sec 65 #4 */
    167   1.1      cgd 	adj360(&l);
    168  1.12      jsm 	Mm = l - (0.1114041 * days) - Pzero;			/* sec 65 #5 */
    169   1.1      cgd 	adj360(&Mm);
    170  1.12      jsm 	Nm = Nzero - (0.0529539 * days);			/* sec 65 #6 */
    171   1.1      cgd 	adj360(&Nm);
    172  1.12      jsm 	Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));	/* sec 65 #7 */
    173  1.12      jsm 	Ac = 0.1858 * sin(dtor(Msol));				/* sec 65 #8 */
    174   1.1      cgd 	A3 = 0.37 * sin(dtor(Msol));
    175  1.12      jsm 	Mmprime = Mm + Ev - Ac - A3;				/* sec 65 #9 */
    176  1.12      jsm 	Ec = 6.2886 * sin(dtor(Mmprime));			/* sec 65 #10 */
    177  1.12      jsm 	A4 = 0.214 * sin(dtor(2 * Mmprime));			/* sec 65 #11 */
    178  1.12      jsm 	lprime = l + Ev + Ec - Ac + A4;				/* sec 65 #12 */
    179  1.12      jsm 	V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));	/* sec 65 #13 */
    180  1.12      jsm 	ldprime = lprime + V;					/* sec 65 #14 */
    181  1.12      jsm 	D = ldprime - LambdaSol;				/* sec 67 #2 */
    182  1.12      jsm 	return(50.0 * (1 - cos(dtor(D))));			/* sec 67 #3 */
    183   1.1      cgd }
    184   1.1      cgd 
    185   1.1      cgd /*
    186   1.1      cgd  * dtor --
    187   1.1      cgd  *	convert degrees to radians
    188   1.1      cgd  */
    189   1.1      cgd double
    190   1.1      cgd dtor(deg)
    191   1.1      cgd 	double deg;
    192   1.1      cgd {
    193   1.1      cgd 	return(deg * PI / 180);
    194   1.1      cgd }
    195   1.1      cgd 
    196   1.1      cgd /*
    197   1.1      cgd  * adj360 --
    198   1.1      cgd  *	adjust value so 0 <= deg <= 360
    199   1.1      cgd  */
    200   1.7    lukem void
    201   1.1      cgd adj360(deg)
    202   1.1      cgd 	double *deg;
    203   1.1      cgd {
    204   1.1      cgd 	for (;;)
    205   1.1      cgd 		if (*deg < 0)
    206   1.1      cgd 			*deg += 360;
    207   1.1      cgd 		else if (*deg > 360)
    208   1.1      cgd 			*deg -= 360;
    209   1.1      cgd 		else
    210   1.1      cgd 			break;
    211  1.12      jsm }
    212  1.12      jsm 
    213  1.12      jsm #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
    214  1.12      jsm time_t
    215  1.12      jsm parsetime(p)
    216  1.12      jsm 	char *p;
    217  1.12      jsm {
    218  1.12      jsm 	struct tm *lt;
    219  1.12      jsm 	int bigyear;
    220  1.12      jsm 	int yearset = 0;
    221  1.12      jsm 	time_t tval;
    222  1.12      jsm 	unsigned char *t;
    223  1.12      jsm 
    224  1.12      jsm 	for (t = p; *t; ++t) {
    225  1.12      jsm 		if (isdigit(*t))
    226  1.12      jsm 			continue;
    227  1.12      jsm 		badformat();
    228  1.12      jsm 	}
    229  1.12      jsm 
    230  1.12      jsm 	tval = time(NULL);
    231  1.12      jsm 	lt = localtime(&tval);
    232  1.12      jsm 	lt->tm_sec = 0;
    233  1.12      jsm 	lt->tm_min = 0;
    234  1.12      jsm 
    235  1.12      jsm 	switch (strlen(p)) {
    236  1.12      jsm 	case 10:				/* yyyy */
    237  1.12      jsm 		bigyear = ATOI2(p);
    238  1.12      jsm 		lt->tm_year = bigyear * 100 - 1900;
    239  1.12      jsm 		yearset = 1;
    240  1.12      jsm 		/* FALLTHROUGH */
    241  1.12      jsm 	case 8:					/* yy */
    242  1.12      jsm 		if (yearset) {
    243  1.12      jsm 			lt->tm_year += ATOI2(p);
    244  1.12      jsm 		} else {
    245  1.12      jsm 			lt->tm_year = ATOI2(p);
    246  1.12      jsm 			if (lt->tm_year < 69)		/* hack for 2000 */
    247  1.12      jsm 				lt->tm_year += 100;
    248  1.12      jsm 		}
    249  1.12      jsm 		/* FALLTHROUGH */
    250  1.12      jsm 	case 6:					/* mm */
    251  1.12      jsm 		lt->tm_mon = ATOI2(p);
    252  1.12      jsm 		if ((lt->tm_mon > 12) || !lt->tm_mon)
    253  1.12      jsm 			badformat();
    254  1.12      jsm 		--lt->tm_mon;			/* time struct is 0 - 11 */
    255  1.12      jsm 		/* FALLTHROUGH */
    256  1.12      jsm 	case 4:					/* dd */
    257  1.12      jsm 		lt->tm_mday = ATOI2(p);
    258  1.12      jsm 		if ((lt->tm_mday > 31) || !lt->tm_mday)
    259  1.12      jsm 			badformat();
    260  1.12      jsm 		/* FALLTHROUGH */
    261  1.12      jsm 	case 2:					/* HH */
    262  1.12      jsm 		lt->tm_hour = ATOI2(p);
    263  1.12      jsm 		if (lt->tm_hour > 23)
    264  1.12      jsm 			badformat();
    265  1.12      jsm 		break;
    266  1.12      jsm 	default:
    267  1.12      jsm 		badformat();
    268  1.12      jsm 	}
    269  1.12      jsm 	/* The calling code needs a valid tm_ydays and this is the easiest
    270  1.12      jsm 	 * way to get one */
    271  1.12      jsm 	if ((tval = mktime(lt)) == -1)
    272  1.12      jsm 		errx(1, "specified date is outside allowed range");
    273  1.12      jsm 	return (tval);
    274  1.12      jsm }
    275  1.12      jsm 
    276  1.12      jsm void
    277  1.12      jsm badformat()
    278  1.12      jsm {
    279  1.12      jsm 	warnx("illegal time format");
    280  1.12      jsm 	(void)fprintf(stderr, "usage: pom [[[[[cc]yy]mm]dd]HH]\n");
    281  1.12      jsm 	exit(1);
    282   1.1      cgd }
    283