#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\
The Regents of the University of California. All rights reserved.");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)pom.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: pom.c,v 1.21 2021/05/02 12:50:46 rillig Exp $");
#endif
#endif
#include <ctype.h>
#include <err.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#ifndef PI
#define PI 3.14159265358979323846
#endif
#define EPOCH_MINUS_1970 (20 * 365 + 5 - 1)
#define EPSILONg 279.403303
#define RHOg 282.768422
#define ECCEN 0.016713
#define lzero 318.351648
#define Pzero 36.340410
#define Nzero 318.510107
int main(int, char *[]);
static void adj360(double *);
static double dtor(double);
static double potm(double);
static time_t parsetime(char *);
static void badformat(void) __dead;
int
main(int argc, char *argv[])
{
time_t tmpt, now;
double days, today, tomorrow;
char buf[1024];
if (time(&now) == (time_t)-1)
err(1, "time");
if (argc > 1) {
tmpt = parsetime(argv[1]);
strftime(buf, sizeof(buf), "%a %Y %b %e %H:%M:%S (%Z)",
localtime(&tmpt));
printf("%s: ", buf);
} else {
tmpt = now;
}
days = (tmpt - EPOCH_MINUS_1970 * 86400) / 86400.0;
today = potm(days) + .5;
if (tmpt < now)
(void)printf("The Moon was ");
else if (tmpt == now)
(void)printf("The Moon is ");
else
(void)printf("The Moon will be ");
if ((int)today == 100)
(void)printf("Full\n");
else if (!(int)today)
(void)printf("New\n");
else {
tomorrow = potm(days + 1);
if ((int)today == 50)
(void)printf("%s\n", tomorrow > today ?
"at the First Quarter" : "at the Last Quarter");
else {
today -= 0.5;
(void)printf("%s ", tomorrow > today ?
"Waxing" : "Waning");
if (today > 50)
(void)printf("Gibbous (%1.0f%% of Full)\n",
today);
else if (today < 50)
(void)printf("Crescent (%1.0f%% of Full)\n",
today);
}
}
return EXIT_SUCCESS;
}
static double
potm(double days)
{
double N, Msol, Ec, LambdaSol, l, Mm, Ev, Ac, A3, Mmprime;
double A4, lprime, V, ldprime, D, Nm;
N = 360 * days / 365.242191;
adj360(&N);
Msol = N + EPSILONg - RHOg;
adj360(&Msol);
Ec = 360 / PI * ECCEN * sin(dtor(Msol));
LambdaSol = N + Ec + EPSILONg;
adj360(&LambdaSol);
l = 13.1763966 * days + lzero;
adj360(&l);
Mm = l - (0.1114041 * days) - Pzero;
adj360(&Mm);
Nm = Nzero - (0.0529539 * days);
adj360(&Nm);
Ev = 1.2739 * sin(dtor(2*(l - LambdaSol) - Mm));
Ac = 0.1858 * sin(dtor(Msol));
A3 = 0.37 * sin(dtor(Msol));
Mmprime = Mm + Ev - Ac - A3;
Ec = 6.2886 * sin(dtor(Mmprime));
A4 = 0.214 * sin(dtor(2 * Mmprime));
lprime = l + Ev + Ec - Ac + A4;
V = 0.6583 * sin(dtor(2 * (lprime - LambdaSol)));
ldprime = lprime + V;
D = ldprime - LambdaSol;
return(50.0 * (1 - cos(dtor(D))));
}
static double
dtor(double deg)
{
return(deg * PI / 180);
}
static void
adj360(double *deg)
{
for (;;)
if (*deg < 0)
*deg += 360;
else if (*deg > 360)
*deg -= 360;
else
break;
}
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
static time_t
parsetime(char *p)
{
struct tm *lt;
int bigyear;
int yearset = 0;
time_t tval;
char *t;
for (t = p; *t; ++t) {
if (isdigit((unsigned char) *t))
continue;
badformat();
}
tval = time(NULL);
lt = localtime(&tval);
lt->tm_sec = 0;
lt->tm_min = 0;
switch (strlen(p)) {
case 10:
bigyear = ATOI2(p);
lt->tm_year = bigyear * 100 - 1900;
yearset = 1;
case 8:
if (yearset) {
lt->tm_year += ATOI2(p);
} else {
lt->tm_year = ATOI2(p);
if (lt->tm_year < 69)
lt->tm_year += 100;
}
case 6:
lt->tm_mon = ATOI2(p);
if ((lt->tm_mon > 12) || !lt->tm_mon)
badformat();
--lt->tm_mon;
case 4:
lt->tm_mday = ATOI2(p);
if ((lt->tm_mday > 31) || !lt->tm_mday)
badformat();
case 2:
lt->tm_hour = ATOI2(p);
if (lt->tm_hour > 23)
badformat();
break;
default:
badformat();
}
if ((tval = mktime(lt)) == -1)
errx(1, "specified date is outside allowed range");
return (tval);
}
static void
badformat(void)
{
warnx("illegal time format");
(void)fprintf(stderr, "usage: %s [[[[[cc]yy]mm]dd]HH]\n",
getprogname());
exit(EXIT_FAILURE);
}