Be able to specify a certain date and/or time for which to calculate

the phase of the moon.
While not worlds best improvements, it will help calendar(1) later on.
This commit is contained in:
Edwin Groothuis 2010-01-05 21:14:48 +00:00
parent 77986b3db1
commit 6846bee277
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=201613
2 changed files with 90 additions and 15 deletions

View File

@ -32,15 +32,34 @@
.\" @(#)pom.6 8.1 (Berkeley) 5/31/93 .\" @(#)pom.6 8.1 (Berkeley) 5/31/93
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.TH POM 6 "May 31, 1993" .Dd May 31, 1993
.Dt POM 6
.UC 7 .UC 7
.SH NAME .Sh NAME
pom \- display the phase of the moon .Nm pom
.SH SYNOPSIS .Nd display the phase of the moon
.B pom .Sh SYNOPSIS
.SH DESCRIPTION .Nm
.Op Fl d Ar yyyy.mm.dd
.Op Fl t Ar hh:mm:ss
.Sh DESCRIPTION
The The
.I pom .Nm
utility displays the current phase of the moon. utility displays the current phase of the moon.
Useful for selecting software completion target dates and predicting Useful for selecting software completion target dates and predicting
managerial behavior. managerial behavior.
.Pp
Use the arguments
.Fl d
and
.Fl o
to specify a specific date and time for which the phase of the moon
has to be calculated.
If
.Fl d
but not
.Fl t
has been specified, it will calculate the phase of the moon on that
day at midnight.
.Sh SEE ALSO
`Practical Astronomy with Your Calculator' by Duffett-Smith.

View File

@ -57,9 +57,13 @@ __FBSDID("$FreeBSD$");
* *
*/ */
#include <time.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <math.h> #include <math.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#include <unistd.h>
#ifndef PI #ifndef PI
#define PI 3.14159265358979323846 #define PI 3.14159265358979323846
@ -76,20 +80,62 @@ __FBSDID("$FreeBSD$");
static void adj360(double *); static void adj360(double *);
static double dtor(double); static double dtor(double);
static double potm(double); static double potm(double);
static void usage(char *progname);
int int
main(void) main(int argc, char **argv)
{ {
time_t tt; time_t tt;
struct tm *GMT; struct tm GMT, tmd;
double days, today, tomorrow; double days, today, tomorrow;
int cnt; int cnt;
char *odate = NULL, *otime = NULL, ch;
(void) time(&tt); while ((ch = getopt(argc, argv, "d:t:")) != -1)
GMT = gmtime(&tt); switch (ch) {
days = (GMT->tm_yday + 1) + ((GMT->tm_hour + case 'd':
(GMT->tm_min / 60.0) + (GMT->tm_sec / 3600.0)) / 24.0); odate = optarg;
for (cnt = EPOCH; cnt < GMT->tm_year; ++cnt) break;
case 't':
otime = optarg;
break;
default:
usage(argv[0]);
}
argc -= optind;
argv += optind;
if (argc)
usage(argv[0]);
/* Adjust based on users preferences */
time(&tt);
if (otime != NULL || odate != NULL) {
/* Save today in case -d isn't specified */
localtime_r(&tt, &tmd);
if (odate != NULL) {
tmd.tm_year = strtol(odate, NULL, 10) - 1900;
tmd.tm_mon = strtol(odate + 5, NULL, 10) - 1;
tmd.tm_mday = strtol(odate + 8, NULL, 10);
/* Use midnight as the middle of the night */
tmd.tm_hour = 0;
tmd.tm_min = 0;
tmd.tm_sec = 0;
}
if (otime != NULL) {
tmd.tm_hour = strtol(otime, NULL, 10);
tmd.tm_min = strtol(otime + 3, NULL, 10);
tmd.tm_sec = strtol(otime + 6, NULL, 10);
}
tt = mktime(&tmd);
}
gmtime_r(&tt, &GMT);
days = (GMT.tm_yday + 1) + ((GMT.tm_hour +
(GMT.tm_min / 60.0) + (GMT.tm_sec / 3600.0)) / 24.0);
for (cnt = EPOCH; cnt < GMT.tm_year; ++cnt)
days += isleap(1900 + cnt) ? 366 : 365; days += isleap(1900 + cnt) ? 366 : 365;
today = potm(days) + .5; today = potm(days) + .5;
(void)printf("The Moon is "); (void)printf("The Moon is ");
@ -160,6 +206,7 @@ potm(double days)
static double static double
dtor(double deg) dtor(double deg)
{ {
return(deg * PI / 180); return(deg * PI / 180);
} }
@ -170,6 +217,7 @@ dtor(double deg)
static void static void
adj360(double *deg) adj360(double *deg)
{ {
for (;;) for (;;)
if (*deg < 0) if (*deg < 0)
*deg += 360; *deg += 360;
@ -178,3 +226,11 @@ adj360(double *deg)
else else
break; break;
} }
static void
usage(char *progname)
{
fprintf(stderr, "Usage: %s [-d yyyy.mm.dd] [-t hh:mm:ss]\n", progname);
exit(EX_USAGE);
}